package jguard.client;

import jguard.client.gui.Window;
import cucky.jGuard.lib.LogFile;
import cucky.jGuard.lib.object.ObjectType;
import cucky.jGuard.lib.object.Log;
import cucky.jGuard.lib.object.Map;
import cucky.jGuard.lib.object.OnlineClients;
import cucky.jGuard.lib.object.ServerSettings;
import cucky.jGuard.lib.object.User;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Properties;
import javax.imageio.ImageIO;
import jguard.client.gui.Tray;
import jguard.client.gui.dialog.LoginDialog;
import jguard.client.network.NetworkClient;


public class Client {
    
    // TODO nazev aplikace jNet
    
    public static final String APP_NAME = "jGuard Client";
    public static final int VERSION = 26;
    
    public static Properties config;
    // nastaveni serveru
    public static ServerSettings serverSettings;
    // podrobnosti o uzivateli, např opravnění
    public static User user;
    // seznam map
    public static ArrayList<Map> map = new ArrayList<Map>();
    // seznam pripojenych klientu
    public static ArrayList<OnlineClients> onlineClients  = new ArrayList<OnlineClients>();
    // log
    public static ArrayList<Log> log = new ArrayList<Log>();
    // pocet offline zarizeni
    public static int offline = 0;
    
    // seznam typu zarizeni
    public static ArrayList<ObjectType> deviceType;
    
    
    
    
    public static NetworkClient client = new NetworkClient("", 0);

    public static Window okno = new Window();
    


    
    public static void main(String[] args) {
        
        LogFile.clear();
        LogFile.printInfo("Client start");
        
        LogFile.printInfo("Loading config ...");
        config = new Config().loadAll(); // TODO toto postrada vyznam kdyz se pak pri dotazu znovu otevira soubor
        if (config != null) {
            LogFile.printInfo(" successfully");
        } else  {
            LogFile.printInfo(" fail");
            System.exit(0);
        }
        
        ///
        /// tray icon
        ///
        new Tray().createAndShowTray();
        
        ///
        /// okno
        ///
        okno.setSize(1024, 800);
        okno.setLocationRelativeTo(null);
        
        ///
        /// nastavit ikonu
        ///
        try {
            okno.setIconImage(ImageIO.read(new File("img/app.png")));
        } catch (IOException ex) {
            LogFile.printErr("APP icon exception");
        }
        
        ///
        /// spustit minimalozovane ? 
        ///
        if (config.getProperty("start_minimized").equals("1")) { 
            okno.setVisible(false);
        } else {
            okno.setVisible(true);
        }
        ///
        /// automatické připojení 
        ///
        if (config.getProperty("autoconnect").equals("1")) { 
            
            Thread t = new AutoConnect();
            t.run();
            /*
            Thread t = new Thread(){
                @Override
                public void run() {
                    super.run(); 
                    LogFile.printInfo("Automaticke pripojovani");
                    
                    client = new NetworkClient(config.getProperty("server"), Integer.valueOf(config.getProperty("port")));
                    
                    client.start();
                }
            };
            t.run();
*/
        } else {
            new LoginDialog().setVisible(true);
        }
    }
    
}
