using FiestaBot.Configuration;
using FiestaBot.Instances.Networking;
using FiestaBot.Login.Handlers;
using FiestaBot.Login.Networking;
using FiestaBot.Manager.Handlers;
using FiestaBot.Manager.Networking;
using FiestaBot.Zone.Handlers;
using FiestaBot.Zone.Networking;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Timers;
using CommandLine;
using CommandLine.Parsing;
using FiestaBot.Enums;
using FiestaBot.Manager.Game;
using FiestaBot.Utilities;
using FiestaBot.Zone.Game;

namespace FiestaBot
{
    internal class Program
    {
        public static bool ZoneBreak;

        public static List<byte> CryptoTable = new List<byte>();

        public static Config Config;
        public static ResSystem ResSystem;
        public static CmdLine CmdLine;

        public static byte AvailableWorlds;
        public static string TransferKey;

        public static LoginHandlerLoader LoginHandlers;
        public static ManagerHandlerLoader ManagerHandlers;
        public static ZoneHandlerLoader ZoneHandlers;

        public static LoginClient LoginClient;
        public static ManagerClient ManagerClient;
        public static ZoneClient ZoneClient;

        public static Timer TitleTimer = new Timer
        {
            Interval = 1000,
            AutoReset = true
        };

        static void Main(string[] args)
        {
            try
            {
                CmdLine = new CmdLine();
                if(!Parser.Default.ParseArguments(args, CmdLine))
                {
                    CmdLine.ConfigFile = "Config.ini";
                }
                Config = new Config(CmdLine.ConfigFile);
                ResSystem = new ResSystem();

                LoginHandlers = new LoginHandlerLoader();
                ManagerHandlers = new ManagerHandlerLoader();
                ZoneHandlers = new ZoneHandlerLoader();
                
                TitleTimer.Elapsed += (s, e) => { Console.Title = string.Format("FiestaConsole (c) Saze - Name: {0} - Level: {1} - Adminlevel: {2} - Class: {3} - In party: {4} - Following: {5}", ManagerClient.Character.Name, ManagerClient.Character.Level, ManagerClient.Character.AdminLevel, ManagerClient.Character.Class, ManagerClient.InParty.ToString(), ZoneClient.FollowMode.ToString()); };

                new LoginClient(IPAddress.Parse(Config.IP), Config.Port);

                bool Active = true;
                while(Active)
                {
                    string c = Console.ReadLine();
                    Commands.HandleCommand(c);
                }
            }
            catch(Exception ex)
            {
                Logger.Log(LogType.Error, ex.Message);
                Console.ReadLine();
            }
        }
    }
}