package jnet.client;

import jnet.client.gui.Window;
import jnet.client.network.NettyClient;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import jnet.client.gui.Tray;
import jnet.lib.BuilddDate;
import jnet.lib.LogFile;
import jnet.lib.PropertiesManager;
import jnet.lib.object.Event;
import jnet.lib.object.Map;
import jnet.lib.object.ObjectType;
import jnet.lib.object.OnlineClients;
import jnet.lib.object.ServerConfig;
import jnet.lib.object.SnmpProfile;
import jnet.lib.object.User;


public class Client {

    // verze serveru
    public static final int CLIENT_VERSION = 1;
    // nazev aplikace 
    public static final String APP_NAME = "jNet";

    public static List<Map> maps = new CopyOnWriteArrayList<>();
    //public static List<MapObject> mapObject = new ArrayList<>();
    public static List<OnlineClients> onlineClients = new ArrayList<>();
    public static List<ObjectType> objectType = new ArrayList<>();
    public static List<SnmpProfile> snmpProfile = new ArrayList<>();
    public static List<User> users = new ArrayList<>();
    public static List<Event> events = new ArrayList<>();
    public static User user;
    public static boolean connected = false;
    public static Window okno = new Window();
    public static Thread clientThread;
    public static PropertiesManager config;
    public static ServerConfig serverConfig;

    public static void main(String[] args) throws InterruptedException {
        

        LogFile.clear();
        LogFile.printInfo("---------------------------------------------");
        LogFile.printInfo("                jNet Client            ");
        LogFile.printInfo(" Version: " + CLIENT_VERSION + " (" + BuilddDate.get() + ")");
        LogFile.printInfo("---------------------------------------------");
        LogFile.printInfo("");

        ///
        /// load config
        ///
        LogFile.printInfo("Loading config ...");
        try {
            // Načtení existující konfigurace, pokud soubor existuje
            config = new PropertiesManager("config.properties");
            config.load();
            LogFile.printInfo("     successfully");
        } catch (PropertiesManager.FileException | PropertiesManager.ConfigException ex) {
            LogFile.printErr("     fail. " + ex.getMessage());
            System.exit(0);
        }

        ///
        /// debug rezim ?
        ///
        if (config.getBoolean("debug")) {
            LogFile.setDebug(true);
            LogFile.printDebug("Debug mode on");
        }

        ///
        /// okno
        ///
        ///
        /// tray icon
        ///
        new Tray().createAndShowTray();

        ///
        /// spustit minimalozovane ? 
        ///
        if (config.getBoolean("start_minimalized")) {
            okno.setVisible(false);
        } else {
            okno.setVisible(true);
        }

        // Spuštění klienta v novém vlákně
        NettyClient client = new NettyClient(config.getString("server"), config.getInt("port"));
        clientThread = new Thread(client);
        clientThread.start();
        
    }

    
    //TODO TODO TODO TODO TODO TODO 
    static void setConnected() {
        // nastavení serveru jako pripojeny 
        connected = true;
        // smazání seznamu map
        maps.clear();
        // smazani object typu
        objectType.clear();
        // smazani uzivatelu
        users.clear();
    }

}
