package tmaestroplayer;

import edutainmentclient.Main;
import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GridBagConstraints;
import java.awt.Window;
import java.awt.event.KeyEvent;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.EndOfMediaEvent;
import javax.media.Player;
import javax.media.StartEvent;
import javax.media.StopEvent;
import javax.media.Time;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.KeyStroke;
import tmaestroplayer.actions.KeyboardAction;

/**
 *
 * @author Silvia
 */
public class MainWindow_portada extends JFrame implements ControllerListener {

    private static MainWindow_portada instance;
    public JDesktopPane panel;
    //protected GraphicalLoginFrame oGLF;
    protected PortadaLoader oPL;
    protected String rutaScorm;
    protected String nombreCurso;
    protected String option = "Op 1.xml";
    protected String userName;
    protected String fullScreen = "true";
    public Player bgMusicPlayer;
    protected Vector<String> listaCursos;

    public MainWindow_portada() {
        System.out.println("Entor en MainWindow_portada()");
    }

    public MainWindow_portada(String userId) {
        super("TMaestro Player");
        System.out.println("Entor en MainWindow_portada(userId)");
        this.userName = userId;
    }

    public MainWindow_portada(String userId, String courseId, String scoId, Float scoTimestamp) {
        System.out.println("Entor en MainWindow_portada(userId, courseId, scoid, socTimestamp)");
        this.userName = userId;
        this.nombreCurso = courseId;
    }

    public MainWindow_portada(String userId, Vector<String> listaCursos) {
        super("TMaestro Player");
        System.out.println("Entor en MainWindow_portada(userId, listaCursos)");
        this.userName = userId;
        this.listaCursos = listaCursos;
    }

    public void init() {
        System.out.println("init en MainWindow_portada");
        //Leemos la configuracion del fichero
        //String FICHERO_CONFIGURACION = System.getProperty("user.dir") + File.separator + "config" + File.separator + "tmaestroplayer" + File.separator + "options.properties";
       // System.out.println("Fichero de configuración " + FICHERO_CONFIGURACION);
        InputStream is = null;
       
            //is = new FileInputStream(new File(FICHERO_CONFIGURACION));
             is = Main.class.getResourceAsStream("/config/options.properties");
            //prop.load(is);
       
        Properties options = new Properties();
        try {
            options.load(is);
            //Leemos la configuracion del fichero
            //options.load( MainWindow.class.getResourceAsStream("/tmaestroplayer/config/options.properties"));

        } catch (IOException ex) {
            ex.printStackTrace();
        }

        option = options.getProperty("option");
        fullScreen = options.getProperty("fullscreen");

        this.setLayout(null);
        this.setUndecorated(true);
        this.setBounds(0, 0, 720, 576);//(1280-800)/2, (1024-800)/2, 800, 600);

        JLabel jLabelBackgroundTop = new JLabel();
        jLabelBackgroundTop.setBackground(Color.BLACK);
        jLabelBackgroundTop.setOpaque(true);
        jLabelBackgroundTop.setBounds(0, 0, 800, 12);

        JLabel jLabelBackgroundBottom = new JLabel();
        jLabelBackgroundBottom.setBackground(Color.BLACK);
        jLabelBackgroundBottom.setOpaque(true);
        jLabelBackgroundBottom.setBounds(0, 588, 800, 12);

        JLabel jLabelBackgroundLeft = new JLabel();
        jLabelBackgroundLeft.setBackground(Color.BLACK);
        jLabelBackgroundLeft.setOpaque(true);
        jLabelBackgroundLeft.setBounds(0, 0, 40, 600);

        JLabel jLabelBackgroundRight = new JLabel();
        jLabelBackgroundRight.setBackground(Color.BLACK);
        jLabelBackgroundRight.setOpaque(true);
        jLabelBackgroundRight.setBounds(760, 0, 40, 600);

        add(jLabelBackgroundTop);
        add(jLabelBackgroundBottom);
        add(jLabelBackgroundLeft);
        add(jLabelBackgroundRight);




        oPL = new PortadaLoader(this);
        panel = (JDesktopPane) oPL.getVisual();

        //joder como se hace la picha un lio...
        //solo hay q quitar los márgenes.
        //panel.setBounds (40,12,720,576)
        panel.setBounds(0, 0 / 2, 720, 576);
        panel.setVisible(true);

        add(panel);
        this.registerKeyboardActions();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);



        //Para pantalla completa
        if (fullScreen.equals("true")) {
            GraphicsDevice myDevice = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
            if (myDevice.isFullScreenSupported()) {

                System.out.println("Soporta pantalla completa");

                myDevice.setFullScreenWindow(this);

                myDevice.setDisplayMode(new DisplayMode(800, 600, 32, 60));


            } else {
                System.out.println("No soporta pantalla completa");
            }
        }


        panel.setSelectedFrame(oPL.vsu);

        requestFocus();

        this.setVisible(true);

        this.setExtendedState(ICONIFIED);
        this.setExtendedState(NORMAL);


        panel.requestFocusInWindow();


        JInternalFrame selectedJif = panel.getSelectedFrame();
        System.out.println("silvia " + selectedJif);
        if (selectedJif instanceof VentanaSeleccionUnidad) {
            System.out.println("@@@@@@@@@@@" + ((VentanaSeleccionUnidad) selectedJif).unidadPrincipal.getIdentifier());
        } else if (selectedJif instanceof VentanaSeleccionCurso) {
            System.out.println("@@@@@@@@@@ La JIF seleccionada es de seleccion de curso");
        } else if (selectedJif instanceof VentanaSCO) {
            System.out.println("@@@@@@@@@@" + ((VentanaSCO) selectedJif).getAtcx().nameSCO);

        } else {
            System.out.println("@@@@@@@@ La JIF seleccionada no es ni vsu ni vsco");
        }

        System.out.println("Numero de ventanas en MainWindow_portada: " + Window.getWindows().length);
        Window[] windows = Window.getWindows();
        for (Window window : windows) {
            System.out.println(window.getName() + ": " + window.getClass());
        }


    }

    public void controllerUpdate(ControllerEvent ce) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public static void main(String[] args) {

        instance = new MainWindow_portada();

        instance.setUndecorated(true);

        //String FICHERO_CONFIGURACION = System.getProperty("user.dir") + File.separator + "config" + File.separator + "tmaestroplayer" + File.separator + "options.properties";
       // System.out.println("Fichero de configuración " + FICHERO_CONFIGURACION);
        InputStream is = null;

             is = Main.class.getResourceAsStream("/config/options.properties");
           // is = new FileInputStream(new File(FICHERO_CONFIGURACION));
            //prop.load(is);
        

        Properties options = new Properties();
        try {
            options.load(is);
            //Leemos la configuracion del fichero
            //options.load( MainWindow.class.getResourceAsStream("/tmaestroplayer/config/options.properties"));

        } catch (IOException ex) {
            ex.printStackTrace();
        }

 
        instance.fullScreen = options.getProperty("fullscreen");
        System.out.println("fullscreen " + instance.fullScreen);

        //instance.rutaScorm = "file:///D:/Cursos/";
        //instance.nombreCurso = "Cocina/";

        instance.setLayout(null);
        instance.setBounds(0, 0, 800, 600);

        JLabel jLabelBackgroundTop = new JLabel();
        jLabelBackgroundTop.setBackground(Color.BLACK);
        jLabelBackgroundTop.setOpaque(true);
        jLabelBackgroundTop.setBounds(0, 0, 800, 12);

        JLabel jLabelBackgroundBottom = new JLabel();
        jLabelBackgroundBottom.setBackground(Color.BLACK);
        jLabelBackgroundBottom.setOpaque(true);
        jLabelBackgroundBottom.setBounds(0, 588, 800, 12);

        JLabel jLabelBackgroundLeft = new JLabel();
        jLabelBackgroundLeft.setBackground(Color.BLACK);
        jLabelBackgroundLeft.setOpaque(true);
        jLabelBackgroundLeft.setBounds(0, 0, 40, 600);

        JLabel jLabelBackgroundRight = new JLabel();
        jLabelBackgroundRight.setBackground(Color.BLACK);
        jLabelBackgroundRight.setOpaque(true);
        jLabelBackgroundRight.setBounds(760, 0, 40, 600);

        instance.add(jLabelBackgroundTop);
        instance.add(jLabelBackgroundBottom);
        instance.add(jLabelBackgroundLeft);
        instance.add(jLabelBackgroundRight);



        instance.add(instance.panel);


        instance.registerKeyboardActions();

        instance.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);



        instance.setVisible(true);



        //Para pantalla completa
        if (instance.fullScreen.equals("true")) {
            GraphicsDevice myDevice = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
            if (myDevice.isFullScreenSupported()) {

                System.out.println("Soporta pantalla completa");

                myDevice.setFullScreenWindow(instance);

                myDevice.setDisplayMode(new DisplayMode(800, 600, 32, 60));


            } else {
                System.out.println("No soporta pantalla completa");
            }
        }



    }

    private void registerKeyboardActions() {
        KeyboardAction upAction = new KeyboardAction(panel, "up");
        KeyboardAction downAction = new KeyboardAction(panel, "down");
        KeyboardAction leftAction = new KeyboardAction(panel, "left");
        KeyboardAction rightAction = new KeyboardAction(panel, "right");
        KeyboardAction enterAction = new KeyboardAction(panel, "enter");

        InputMap panelInputMap = panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        panelInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "upAction");
        panelInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "downAction");
        panelInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "leftAction");
        panelInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "rightAction");
        panelInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enterAction");

        ActionMap panelActionMap = panel.getActionMap();
        panelActionMap.put("upAction", upAction);
        panelActionMap.put("downAction", downAction);
        panelActionMap.put("leftAction", leftAction);
        panelActionMap.put("rightAction", rightAction);
        panelActionMap.put("enterAction", enterAction);
    }
}
