/*
 * 00/08/01 @(#)SvcDispXlet.java   1.3
 *
 * Copyright (c) 2000 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to Sun.
 */

import javax.tv.xlet.*;
import javax.tv.graphics.*;
import javax.tv.service.*;
import javax.tv.service.guide.*;
import javax.tv.service.navigation.*;

import java.awt.*;
import java.awt.event.*;

import java.util.Date;
import java.text.DateFormat;

public class SvcDispXlet implements Xlet, ActionListener {
    private Container root_container = null;
    private Panel panel = null;
    private List slist = null;
    private List plist = null;
    private Button button = null;
    private SIManager si_manager = null;
    private Retriever retriever = new Retriever();

    // init method
    public void initXlet(XletContext ctx){
   
       root_container = TVContainer.getRootContainer(ctx);
   
       panel = new Panel();
       panel.setBackground(Color.black);

       //root_container.setLayout(new BorderLayout());
       //root_container.add(panel);

       root_container.setLayout(null);
       root_container.setBounds(0,0,400,300);
       root_container.add(panel);

       panel.setBounds(0,0,root_container.getSize().height,root_container.getSize().width - 50);
       panel.setLayout(new GridLayout(2,1));

       slist = new List();
       slist.setBackground(Color.lightGray);
       slist.addActionListener(this);
       panel.add(slist);
       plist = new List();
       plist.setBackground(Color.lightGray);
       panel.add(plist);

       button = new Button("Refresh");
       button.setBackground(Color.darkGray);
       button.setForeground(Color.white);
       button.addActionListener(this);
       root_container.add(button);
       button.setBounds(0,root_container.getSize().width-50,root_container.getSize().height,50);
        
       root_container.validate();
       root_container.setVisible(true);

       si_manager = SIManager.createInstance();
    }

    // start method
    // when we start, do an initial update of the slist
    public void startXlet(){
       panel.validate();
       updateList(slist);
    }

    // pause
    public void pauseXlet(){}

    // destroy
    public void destroyXlet(boolean unconditional){}
    
    // called when the refresh button is pressed
    // or service is selected in the list
    public void actionPerformed(ActionEvent evt){
       if(evt.getSource() == button){
          this.updateList(slist);
          plist.removeAll();
       } else if (evt.getSource() == slist) {
          this.updateList(plist);
       } 
    }

    private void updateList(List list){
   // clear out the old list
       list.removeAll();
       ServiceList collection = si_manager.filterServices(null);
       ServiceIterator si = collection.createServiceIterator();

       si.toEnd();
       if (list == slist) {
         while(si.hasPrevious()){
            slist.addItem(si.previousService().getName() , 0);
         }
       } else {
         while(si.hasPrevious()){ 
            Service s = si.previousService();
            if (slist.getSelectedItem().equals(s.getName())) {
               retriever.getPrograms(s);
               break;
            }
         } 
       }
    }

    class Retriever implements SIRequestor {
       DateFormat dfmt = DateFormat.getDateInstance(DateFormat.SHORT);
       DateFormat tfmt = DateFormat.getTimeInstance(DateFormat.SHORT);

       void getPrograms(Service s) {
          try {
             SIManager.createInstance().retrieveServiceDetails(
                s.getLocator(), this);
          } catch (Exception e) { e.printStackTrace(); }
       } 

       public void notifySuccess(SIRetrievable[] result) {
          if (result[0] instanceof ServiceDetails) {
             try {
                ((ServiceDetails)result[0]).getProgramSchedule().
                   retrieveFutureProgramEvents(new Date(), new Date(System.currentTimeMillis() + 600000000), this);
             } catch (SIException e) {}
          } else if (result[0] instanceof ProgramEvent) {
             for (int i = 0; i < result.length; i++ ) { 
                ProgramEvent e = (ProgramEvent)result[i];
                plist.addItem(e.getName() + " : " 
                              + dfmt.format(e.getStartTime()) + " " 
                              + tfmt.format(e.getStartTime()));
             }
          }
       }

       public void notifyFailure(SIRequestFailureType reason) {}
    }
 }
