package jguard.client.gui;

import cucky.jGuard.lib.LogFile;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import jguard.client.Client;
import jguard.client.gui.dialog.NewMapDialog;

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

        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("Ukončit");
                    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);
        

        JButton buttonAddMap = new JButton("Přidat mapu");
        buttonAddMap.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new NewMapDialog().setVisible(true);
            }
        });

        leftPanel.add(buttonAddMap, BorderLayout.PAGE_END);
        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 reloadOnlineClinets() {
        statusBar.setOnlineCount();
    }

    // prepnuti mapy 
    public static void setMapView(int index) {
        // nastaveni map id
        currentMapIndex = index;

        MapView mv = new MapView();
        JScrollPane scroll = new JScrollPane(mv);
        jsp.add(scroll, JSplitPane.RIGHT);

    }

    // prepnuti na stranku nastaveni
    public static void showSettings() {
        jsp.add(new SettingsPanel(), JSplitPane.RIGHT);
    }

    // prepnuti na stranku s nastavenim serveru
    public static void showServerSettings() {
        serverSettings = new ServerSettingsPanel();
        JPanel serverSet = new JPanel(new BorderLayout());
        serverSet.add(serverSettings, BorderLayout.CENTER);
        jsp.add(serverSet, JSplitPane.RIGHT);
    }

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

}
