using System;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
using System.Data.Odbc;
using System.Net;

namespace GameServer
{
    class cWRClient
    {
        public int FuckPacket=15000;
        public int FuckPacket2 = 165000;
        public int Second=0;
        public struct tItem
        {
            public string itemCode;
            public long expireDate;
            public int SlotEngineer;
            public int SlotMedic;
            public int SlotSniper;
            public int SlotAssault;
            public int SlotHeavyTrooper;
         }
        public struct bItem
        {
            
            public string WaffenName;
            public int Level;
            public int Tage3;
            public int Tage7;
            public int Tage15;
            public int Tage30;
            public int Premium;
           
        }
       
       
             public string[] Weapons;
          
             public string slot1E = "DA02";
             public string slot2E = "DB01";
             public string slot3E = "DF01";
             public string slot4E = "DR01";
             public string slot5E = "^";
             public string slot6E = "^";

             public string slot1H = "DA02";
             public string slot2H = "DB01";
             public string slot3H = "DJ01";
             public string slot4H = "DL01";
             public string slot5H = "^";
             public string slot6H = "^";

             public string slot1M = "DA02";
             public string slot2M = "DB01";
             public string slot3M = "DF01";
             public string slot4M = "DQ01";
             public string slot5M = "^";
             public string slot6M = "^";

             public string slot1S = "DA02";
             public string slot2S = "DB01";
             public string slot3S = "DG05";
             public string slot4S = "DN01";
             public string slot5S = "^";
             public string slot6S = "^";

             public string slot1A = "DA02";
             public string slot2A = "DB01";
             public string slot3A = "DC02";
             public string slot4A = "DN01";
             public string slot5A = "^";
             public string slot6A = "^";
        
        
      
        public cClientPacketHandler pHandler = new cClientPacketHandler();
        private Socket cSocket;
        private long UserID = 0;
        private int SessionID = 0;
        private string Username = "";
        private string Nickname = "";
        private string Password = "";
        private int Accesslevel = 0;
        private long EXP = 0;
        private int Level = 1;
        private long Dinar = 0;
        private long Kills = 0;
        private long Deaths = 0;
        private int Premium = 0;
        private long PremiumTime = 0;
        private long PremiumDate = 0;
        private string Mac = "";
        private int ChannelID = 0;
        private long PremiumLeftTime = 0;
        private int RoomPage = 0;
        private cWRRoom ctRoom = null;
        private int ReadyState = 0;
        //////////////////////
        private long nIP;
        private long nPort;
        private long lIP;
        private long lPort;
        /////////////////////
        private ArrayList Items = new ArrayList();
        private ArrayList bItems = new ArrayList();
        ////////////////////////
        private DateTime tLeft;
        private TimeSpan tSpan;
        public System.Timers.Timer tTimer = new System.Timers.Timer();
        ////////////////////////

        public cWRClient(Socket tSocket, int sID)
        {
            cSocket = tSocket;
            SessionID = sID;
        }
        public string TimeAdd(int Add)
        {
            string Date="";
            string Day = "";
            string Month = "";
            if (DateTime.Now.AddDays(Add).Day < 10) Day = "0" + DateTime.Now.AddDays(Add).Day.ToString();

            else Day = DateTime.Now.AddDays(Add).Day.ToString();
            

            if (DateTime.Now.AddDays(Add).Month < 10)Month = "0" + (DateTime.Now.AddDays(Add).Month+1).ToString();
            
            else Month = (DateTime.Now.AddDays(Add).Month+1).ToString();


            Date = (DateTime.Now.Year-2000).ToString() + Month + (Day) + (DateTime.Now.Hour+2).ToString();
           
            return Date;
        }
        private void PremiumLeft(Object myObject, EventArgs myEventArgs)
        {
            if (Premium > 0)
            {
                tSpan = DateTime.Now - tLeft;
                PremiumLeftTime = PremiumTime - tSpan.Minutes;
                if (PremiumLeftTime <= 0) { PremiumLeftTime = -1; }
            }
            else { PremiumLeftTime = -1; }

            Send(new SP_PREMIUM_LEFT(this));
        }
        public void GetAllWeapon()
        {
            try
            {
                
                Weapons = new string[10];
                OdbcDataReader SQLReader;
                OdbcCommand SQLCommand = new OdbcCommand("SELECT itemCode FROM INVENTORY_TBL WHERE userID = '" + UserID + "' AND class_0 > '0' OR class_1 > '0' OR class_2  > '0' OR class_3  > '0' OR class_4  > '0' ", Program.SQLClass.SQLConn);
                SQLReader = SQLCommand.ExecuteReader();
                
                while (SQLReader.Read())
                {
                    for (int i = 0; i <= 10; i++)
                    {
                        Weapons[i] = SQLReader.GetString(i);
                         
                    }
                }
                SQLReader.Close();
            }
            catch (OdbcException ex)
            {
                new cLog(C.MSG_ERROR, "MySQL Error: " + ex.Message);
            }
        }
        public void getUserData()
        {
            try
            {
                OdbcDataReader SQLReader;
                OdbcCommand SQLCommand = new OdbcCommand("SELECT username, nickname, password, accesslevel FROM ACCOUNT_TBL WHERE userID = '" + UserID + "'", Program.SQLClass.SQLConn);
                SQLReader = SQLCommand.ExecuteReader();
                while(SQLReader.Read())
                {
                    Username = SQLReader.GetString(0);
                    Nickname = SQLReader.GetString(1);
                    Password = SQLReader.GetString(2);
                    Accesslevel = SQLReader.GetInt32(3);
                } SQLReader.Close();

                SQLCommand = new OdbcCommand("SELECT exp, level, dinar, kills, deaths, premium, premiumTime, premiumDate FROM ACCOUNT_TBL_DETAIL WHERE userID = '" + UserID + "'", Program.SQLClass.SQLConn);
                SQLReader = SQLCommand.ExecuteReader();
                while (SQLReader.Read())
                {
                    EXP = SQLReader.GetInt64(0);
                    Level = SQLReader.GetInt32(1);
                    Dinar = SQLReader.GetInt64(2);
                    Kills = SQLReader.GetInt64(3);
                    Deaths = SQLReader.GetInt64(4);
                    Premium = SQLReader.GetInt32(5);
                    PremiumTime = SQLReader.GetInt64(6);
                    PremiumDate = SQLReader.GetInt64(7);
                } SQLReader.Close();

                if (Premium > 0 && PremiumDate.ToString().Length == 12 && PremiumTime > 0)
                {
                    string sTime = PremiumDate.ToString();
                    tLeft = new DateTime(int.Parse(sTime.Substring(sTime.Length - 4, 4)),
                        int.Parse(sTime.Substring(sTime.Length - 6, 2)),
                        int.Parse(sTime.Substring(sTime.Length - 8, 2)),
                        int.Parse(sTime.Substring(sTime.Length - 10, 2)),
                        int.Parse(sTime.Substring(sTime.Length - 12, 2)), 00);

                    tSpan = DateTime.Now - tLeft;
                    PremiumLeftTime = PremiumTime - tSpan.Minutes;
                    if (PremiumLeftTime <= 0)
                    { 
                        PremiumLeftTime = -1;
                        Premium = 0;
                        Program.SQLClass.runSQL("UPDATE ACCOUNT_TBL_DETAIL SET premium = '0', premiumTime = '0', premiumDate = '0' WHERE userID = '" + UserID + "'");
                    }
                }
                else
                {
                    tLeft = new DateTime();
                }

                tTimer.Interval = 5000;
                tTimer.Elapsed += new System.Timers.ElapsedEventHandler(PremiumLeft);
                tTimer.Start();
            }
            catch(OdbcException ex)
            {
                new cLog(C.MSG_ERROR, "MySQL Error: " + ex.Message);
            }
        }

        public void loadItems()
        {
            try
            {
                tItem pItem = new tItem();
                Items.Clear();
                OdbcDataReader SQLReader;
                OdbcCommand SQLCommand = new OdbcCommand("SELECT itemCode, expireDate, class_0, class_1, class_2, class_3, class_4 FROM INVENTORY_TBL WHERE userID = '" + UserID + "'", Program.SQLClass.SQLConn);
                SQLReader = SQLCommand.ExecuteReader();
                while (SQLReader.Read())
                {
                    pItem.itemCode = SQLReader.GetString(0);
                    pItem.expireDate = SQLReader.GetInt64(1);
                    pItem.SlotEngineer = SQLReader.GetInt32(2);
                    pItem.SlotMedic = SQLReader.GetInt32(3);
                    pItem.SlotSniper = SQLReader.GetInt32(4);
                    pItem.SlotAssault = SQLReader.GetInt32(5);
                    pItem.SlotHeavyTrooper = SQLReader.GetInt32(6);
                    Items.Add(pItem);
                }
                SQLReader.Close();
            }
            catch (OdbcException ex)
            {
                new cLog(C.MSG_ERROR, "MySQL Error: " + ex.Message);
            }
        }

        public void GetShopItems()
        {
            try
            {
                bItem lItem = new bItem();
                OdbcDataReader SQLReader;
                OdbcCommand SQLCommand = new OdbcCommand("SELECT WaffenName, WaffenLevel, TageDrei, TageSieben, TageFünfzehn, TageDreisig, Premium FROM SHOP_TBL", Program.SQLClass.SQLConn);
                SQLReader = SQLCommand.ExecuteReader();
                while (SQLReader.Read())
                {
                    

                 lItem.WaffenName = SQLReader.GetString(0);
                 lItem.Level = SQLReader.GetInt32(1);
                 lItem.Tage3 = SQLReader.GetInt32(2);
                 lItem.Tage7 = SQLReader.GetInt32(3);
                 lItem.Tage15 = SQLReader.GetInt32(4);
                 lItem.Tage30 = SQLReader.GetInt32(5);
                 lItem.Premium = SQLReader.GetInt32(6);

                 bItems.Add(lItem);
                }
                SQLReader.Close();
                
            }
            catch (OdbcException ex)
            {
                new cLog(C.MSG_ERROR, "MySQL Error: " + ex.Message);
            }
            

        }

        public void Send(cServerPacketHandler fPacket)
        {
            byte[] bPacket = fPacket.getPacket();
            cSocket.Send(bPacket);
        }

        public void setSessionID(int sID) { SessionID = sID; }
        public int getSessionID() { return SessionID; }

        public void setUserID(long sID) { UserID = sID; }
        public long getUserID() { return UserID; }

        public string getNickname() { return Nickname; }
        public long getExp() { return EXP; }
        public int getLevel() { return Level; }
        public long getDinar() { return Dinar; }
        public long getKills() { return Kills; }
        public long getDeaths() { return Deaths; }
        public long getPremium() { return Premium; }
        public ArrayList getItemList() { return bItems; }
        public void setMac(string sMac) { Mac = sMac; }
        public string getMac() { return Mac; }
        
        public int getChannelID() { return ChannelID; }
        public void setChannelID(int cID) { ChannelID = cID; }

        public long getPremiumLeftTime() { return PremiumLeftTime; }
        public ArrayList getItems() { return Items; }

        public void setRoom(cWRRoom Room) { ctRoom = Room; }
        public cWRRoom getRoom() { return ctRoom; }

        public void setRoomPage(int Page) { RoomPage = Page; }
        public int getRoomPage() { return RoomPage; }

        public int getAccesslevel() { return Accesslevel; }

        public void setNetwork(IPEndPoint IPeo)
        {
            nIP = Program.WRServer.IPToInt(IPeo.Address.ToString());
            nPort = IPeo.Port;
            lIP =  Program.WRServer.IPToInt(IPeo.Address.ToString()); // Muss noch geändert werden, kA wo der server die interne IP herbekommt?!
            lPort = IPeo.Port;
        }

        public long getNetworkIP() { return nIP; }
        public long getNetworkPort() { return nPort; }
        public long getLocalIP() { return lIP; }
        public long getLocalPort() { return lPort; }

        public void setReadyState(int State) { ReadyState = State; }
        public int getReadyState() { return ReadyState; }
    }
}