package jnet.client.gui;

import jnet.client.Client;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import jnet.lib.LogFile;
import jnet.lib.object.Map;

public class Window extends JFrame {

    public static int currentMapIndex;

    private static StatusBar statusBar;
    private static JSplitPane jsp;
    private static JTabbedPane serverSettings;

    public Window() {
        super(Client.APP_NAME); //titulek okna

        Dimension d = new Dimension(1024, 800);
        this.setPreferredSize(d);
        this.setSize(d);
        this.setLocationRelativeTo(null);

        try {
            this.setIconImage(ImageIO.read(new File("img/app.png")));
        } catch (IOException ex) {
            LogFile.printErr("APP icon exception");
        }
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent windowEvent) {
                // vola se pri zavreni okna krizkem
                Object[] options = {
                    "Ukončit",
                    "Minimalizovat"
                };
                int n = JOptionPane.showOptionDialog(windowEvent.getWindow(),
                        "Ukončit nebo minimalozovat do lišty ?",
                        "Ukončit",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE,
                        null, //do not use a custom Icon
                        options, //the titles of buttons
                        options[1]); //default button title
                if (n == 0) {
                    LogFile.printInfo("Ukoncit");
                    System.exit(0);
                }
            }

            @Override
            public void windowDeiconified(WindowEvent e) {
                // vola se pri obnoveni z listy
            }

            @Override
            public void windowIconified(WindowEvent e) {
                // pri minimalizaci
                setVisible(false);
            }

        });

        this.setLayout(new BorderLayout());

        this.add(new MenuPanel(), BorderLayout.NORTH);

        // oddelovac leve a prave strany 
        jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        jsp.setDividerSize(5);

        // levy panel
        JPanel leftPanel = new JPanel();
        leftPanel.setBackground(Color.WHITE);
        leftPanel.setPreferredSize(new Dimension(200, 250));
        leftPanel.setLayout(new BorderLayout());
        leftPanel.add(new MapTree(), BorderLayout.PAGE_START);
        //leftPanel.add(new MapList(), BorderLayout.PAGE_START);
        jsp.add(leftPanel, JSplitPane.LEFT);
        jsp.add(new JPanel(), JSplitPane.RIGHT);

        this.add(jsp, BorderLayout.CENTER);

        statusBar = new StatusBar();
        this.add(statusBar, BorderLayout.SOUTH);

        this.pack();

    }

    public void blinkTaskBar() {
        toFront();
    }

    public static void setStatusBarMessage(String message) {
        statusBar.setMessage(message);
    }

    public static void setConectionStatus(String state) {
        statusBar.setConectionStatus(state);
    }

    // prepnuti na stranku nastaveni
    public static void showSettings() {
        SettingsPanel set = new SettingsPanel();
        JScrollPane scroll = new JScrollPane(set);
        JScrollBar verticalBar = scroll.getVerticalScrollBar();
        verticalBar.setUnitIncrement(20);
        jsp.add(scroll, JSplitPane.RIGHT);
    }

    // prepnuti mapy 
    public static void setMapView(Map map) {
        MapView mv = new MapView(map);
        JScrollPane scroll = new JScrollPane(mv);
        jsp.add(scroll, JSplitPane.RIGHT);
    }

    public static void showServerSettings(int table) {
        ServerSettingsPanel mv = new ServerSettingsPanel();
        mv.setTable(table);
        JScrollPane scroll = new JScrollPane(mv);
        jsp.add(scroll, JSplitPane.RIGHT);
    }

    public static void showEvents() {
        EventsPanel ep = new EventsPanel();
        jsp.add(ep, JSplitPane.RIGHT);
    }

//    public static void showMessage(String text){
//        // TODO co s tim ? nechat nebo vytvořit dialog ? 
//        //new ServerMessageDialog(text);
//        JOptionPane.showMessageDialog(null, text);
//    }
}
