using System;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Media;
using System.Drawing;
using System.Drawing.Imaging;

namespace Util
{
    class Program
    {
        // เริ่มฟังก์ชันการทำงาน
        private static string cTemp, cTemp2;
        public static void Pok_Mess(string cfWord)
        {   MessageBox.Show(cfWord + System.Environment.NewLine
            + "" + System.Environment.NewLine
            , "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        // ถาม OK หรือ NO แบบที่ 1
        public static bool Pok_YesNO(string cSayText)
        {
            DialogResult dialogResult = MessageBox.Show("Warning", cSayText, MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes) { return true; } else { return false; }
        }

        // ถาม OK หรือ NO แบบที่ 2
        public static bool Pok_YesNO2(string cSayText)
        {
            DialogResult dialogResult = MessageBox.Show(cSayText, "Information", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes) { return true; } else { return false; }
        }

        // ส่งเสียงเตือน
        public static void SoundOpen(string file4song)
        {
            if (!File.Exists(file4song)) { return; }
            SoundPlayer SoundContinue = new SoundPlayer(file4song);
            SoundContinue.Play();
        }

        public static void ReaderError(string Err1,string Err2,string Err3, string Err4, string Err5)
        {
            Layout.Program.LayOut.ScreenShow(-1,Err1, Err2, Err3, Err4, Err5); // พื้นแดง
        }


        public static void instep(string cLine,string cMess)
        {
            if (!NIDCardCS.Program.instep) {return;}
            MessageBox.Show(cLine+" ,"+cMess);
            // Layout.Program.LayOut.ScreenShow(6, "Rdcardstep.hav", cLine, "", cMess, ""); // พื้นแดง
        }


        // สำหรับเปิด  Website 
        public static void OpenWebSite(String URLname)  // Pipop
        {
            if (URLname=="") { return; }
            string oldFolder = System.IO.Directory.GetCurrentDirectory();
            URLname = (char)34 + URLname.Trim() + (char)34;
            URLname = URLname.Trim();
        

        cTemp = @"C:\Program Files (x86)\Microsoft\Edge\Application"; // Edge \msedge.exe
            if (Directory.Exists(cTemp)) {Directory.SetCurrentDirectory(cTemp); cTemp2="msedge.exe"; }
            else {
                cTemp = @"C:\Program Files\Google\Chrome\Application"; // หาChrome ก่อน 
                if (Directory.Exists(cTemp)) {Directory.SetCurrentDirectory(cTemp); cTemp2="chrome.exe";} 
                else {
                    cTemp = @"C:\Program Files\Mozilla Firefox";
                    if (Directory.Exists(cTemp)) { Directory.SetCurrentDirectory(cTemp); cTemp2 = "firefox.exe"; }
                }
            }
            var opentwebthis = new ProcessStartInfo(cTemp2);
            opentwebthis.Arguments = URLname;
            Process.Start(opentwebthis);
            Directory.SetCurrentDirectory(oldFolder);
            cTemp = ""; cTemp2 = "";
        }


        
        // เรียงไฟล์ใหม่จากท้าย ขึ้นบน
        public static void ReverseTextFile(string FleSource, string FleTarget, int counter)
        {
            if (!File.Exists(FleSource)) { Util.Program.Pok_Mess("ไม่พบไฟล์ " + FleSource); return; }
            if (FleTarget.Length == 0) { Util.Program.Pok_Mess("ไม่มีไฟล์ปลายทาง"); return; }
            if (counter == 0) { counter = 200; }
            try
            {
                if (File.Exists(FleTarget)) { File.Delete(FleTarget); }
            }
            catch { return; }
            TextWriter TxtTarget = new StreamWriter(FleTarget);
            var FleRead = File.ReadAllLines(FleSource);
            int icounter = 0;
            for (int iLine = FleRead.Length; iLine >= 1; iLine--)
            {
                icounter++; TxtTarget.WriteLine(FleRead[iLine - 1]);
                if (icounter >= counter) { TxtTarget.WriteLine("**แสดง " + icounter.ToString().Trim() + " บรรทัดสุดท้าย"); break; }
            }
            TxtTarget.Close();
        }


        // เปลี่ยนรุปแบบวันที่ปัจจุบัน 
        public static string DTN(int nOption)
        {
            string cDate = "";
            switch (nOption)
            {
                case 2: cDate = DateTime.Now.ToString("dd MMMM yyyy HH:mm"); break; // 01 May 2015 05:50
                case 3: cDate = DateTime.Now.ToString("dd/MM/yy HH:mm:ss"); break; // 01/02/15 05:50:05
                case 4: cDate = DateTime.Now.ToString("ddd, dd MMM yyy HH:mm:ss"); break; // Fri, 16 May 2015 05:50:06
                case 5: cDate = DateTime.Now.ToString("ddMMyy_HHmmss"); break; // 010215_055005
                case 6: cDate = DateTime.Now.ToString("ddMMyyyy_HHmmss"); break; // 010215_055005
                default: cDate = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"); break; // 01/02/2015 05:50:05
            }
            return cDate;
        }

        public static void CropSCR(string Flename,Int32 nWidth,Int32 nHeight)
        {
            Flename = @"_Card\" + Flename.Trim()+ ".jpg";
            if (nWidth <= 0) { nWidth = Screen.PrimaryScreen.Bounds.Width; }
            if (nHeight<= 0) { nHeight= Screen.PrimaryScreen.Bounds.Height; }
            Bitmap printscreen = new Bitmap(nWidth, nHeight);  /* กว้างXยาว ให้เปิดด้วย Paint Brash ปรับ แล้ว ดู ตัวเลข Properties แล้ว  */
            Graphics graphics = Graphics.FromImage(printscreen as Image);
            graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
            printscreen.Save(Flename, ImageFormat.Jpeg);
            printscreen.Dispose();
            NIDCardCS.Form1.Write2Jpeg(Flename,3);
        }

        public static void CallWEB(string cOption)
        {
            string cfCompany;
            cfCompany = "";
            if (File.Exists("RdCardInfo.txt"))
            {
                string[] lines = File.ReadAllLines("RdCardInfo.txt");
                foreach (string line in lines) { cfCompany = cfCompany + line.Trim(); }
            }
            cOption = cOption.Trim();
            // OpenWebSite("https://uyod.net/l.php?o="+cOption+"&m="+cfCompany); 
        }
        //  สิ้นสุด ฟังก์ชันการทำงาน
    }
}


