package jguard.client;

import cucky.jGuard.lib.LogFile;
import cucky.jGuard.lib.Message;
import cucky.jGuard.lib.Status;
import cucky.jGuard.lib.object.ObjectType;
import cucky.jGuard.lib.object.Map;
import cucky.jGuard.lib.object.MapObject;
import cucky.jGuard.lib.object.ObjectConnection;
import cucky.jGuard.lib.object.OnlineClients;
import cucky.jGuard.lib.object.ServerSettings;
import cucky.jGuard.lib.object.User;
import java.io.IOException;
import java.util.ArrayList;
import static jguard.client.Client.config;
import jguard.client.gui.MapTree;
import jguard.client.gui.MapView;
import jguard.client.gui.Tray;
import jguard.client.gui.Window;
import jguard.client.gui.dialog.LoginDialog;

public class ClientMessageParser {

    Map map;
    MapObject mo;

    public void parse(Message msg) {

        LogFile.printDebug("-> Message type: " + msg.getType());

        switch (msg.getType()) {

            case Message.CLIENT_VERSION:
                Client.client.send(Message.CLIENT_VERSION, Client.VERSION);
                break;

                
            case Message.UPDATE:
                LogFile.printInfo("Přijat požadavek na aktualizaci");
                String[] run = {"java", "-jar", "Updater.jar"};
                // TODO po přijetí pozadavku dialogove okno s informaci ze dojde k aktualizace a odpoctem k jejimu provedeni
                try {
                    // odpoji se 
                    Client.client.disconnect();
                    // spusti updater 
                    Runtime.getRuntime().exec(run);
                    // vypne program 
                    System.exit(0);
                } catch (IOException ex) {
                    LogFile.printErr("Client update exception: " + ex.getMessage());
                }
                break;

                
            case Message.AUTH_REQUEST:
                Window.setStatusBarMessage("Přihlašuji ...");
                String[] userPass = {config.getProperty("user"), config.getProperty("password"), String.valueOf(OnlineClients.PLATFORM_PC)};
                Client.client.send(Message.AUTH, userPass);
                break;

                
            case Message.AUTH_SUCEFULL:
                Window.setStatusBarMessage("Připojeno");
                LogFile.printInfo("Pripojeno");
                // smazání seznamu map
                Client.map.clear();
                break;

                
            case Message.AUTH_FAIL:
                Window.setStatusBarMessage("Chyba autorizace");
                new LoginDialog().setVisible(true);
                break;

                
            case Message.SERVER_MESSAGE:
                Window.showMessage((String) msg.getMsg());
                break;

                
            case Message.MAP:
                map = (Map) msg.getMsg();
                LogFile.printDebug("Prijata infomace o mape");
                // pridani do arraylistu
                Client.map.add(map);
                // pridani do stromu
                MapTree.add();
                // spocitani offline zarizeni
                for (MapObject obj : map.getObjects()) {
                    if (obj.getStatus() == Status.OFFLINE) {
                        Client.offline++;
                    }
                }
                // aktualizace tray icon
                Tray.setState();
                break;

                
            case Message.ONLINE_CLIENTS:
                // TODO pri kompilaci zpusobuje chybu uses unchecked or unsafe operations. ale nevim proc 
                Client.onlineClients = (ArrayList<OnlineClients>) msg.getMsg();
                Window.reloadOnlineClinets();
                break;

                
            case Message.SERVER_SETTINGS:
                Client.serverSettings = (ServerSettings) msg.getMsg();
                break;

                
            case Message.USER_INFO:
                Client.user = (User) msg.getMsg();
                break;

                
            case Message.SET_MAP_LOCK:
                int[] data_lock = (int[]) msg.getMsg();
                MapManager.setLock(data_lock[0], data_lock[1]);
                break;
                
                
            case Message.REMOVE_MAP:
                int map_deleted = (int) msg.getMsg();
                // odebrani z lokalniho seznamu
                for (Map m : Client.map) {
                    if (m.getId() == map_deleted) {
                        Client.map.remove(m);
                        break;
                    }
                }
                // aktualizace stromu map
                break;

                
            case Message.UPDATE_OBJECT:
                mo = (MapObject) msg.getMsg();
                // aktualizace v lokalnim seznamu
                for (Map map : Client.map) {
                    map.updateObject(mo);
                }
                break;

                
            case Message.STATE_UPDATE:
                int[] data = (int[]) msg.getMsg();
                // aktualizace stavu u objektu
                MapManager.setObjectStatus(data[0], data[1], data[2]);
                // aktualizace stromu map
                MapTree.reload();
                // aktualizace poctu offline zarizeni
                if (data[2] == Status.OFFLINE) {
                    Client.offline++;
                } else if (Client.offline < 1) {
                    Client.offline--;
                }
                // aktualizace tray icon
                Tray.setState();
                break;

                
            case Message.ADD_OBJECT_TYPE:
                Client.deviceType.add((ObjectType) msg.getMsg());
                System.out.println("jguard.client.ClientMessageParser.parse()");
                break;

                
            case Message.ADD_OBJECT:
                mo = (MapObject) msg.getMsg();
                // prida do lokalniho seznamu
                for (Map m : Client.map) {
                    if (m.getId() == mo.getMap()) {
                        m.addObject(mo);
                        break;
                    }
                }
                // je-li mapa otevrena prida ji i tam
                if (Client.map.get(Window.currentMapIndex).getId() == mo.getMap()) {
                    MapView.addObject(mo);
                }
                break;
                
                
            case Message.LIST_OBJECT_TYPE:
                Client.deviceType = (ArrayList<ObjectType>) msg.getMsg();
                break;

                
            case Message.NEW_CONNECTION:
                ObjectConnection oc = (ObjectConnection) msg.getMsg();
                    for (Map map : Client.map) {
                        if (map.getId() == oc.getMap()) {
                            // pridani do arraylistu
                            map.addObjectConnection(oc);
                            break;
                        }
                    }
                    // je-li mapa otevrena prida propojeni i tam
                    if (Client.map.get(Window.currentMapIndex).getId() == oc.getMap()) {
                        MapView.addObjectConnection(oc);
                    }
                break;

                
            default:
                LogFile.printErr("Neznamy typ zpravy");
        }

    }

}
