using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Timers;

namespace Layout
{
    class Program
    {
        private static int nReceive;
        // public static void ???(string ??)
        public class LayOut : Form
        {
            public Button buttonExit;
            public Label ctext1, ctext2, ctext3, ctext4, ctext5;
            private static System.Timers.Timer WinTimer;
            // private static System.Timers.Timer AmphanTime;
            private static string cTemp;
            private PictureBox cPicture;
            public LayOut()
            {
                this.Size = new System.Drawing.Size(500, 140);
                this.StartPosition = FormStartPosition.CenterScreen;
                this.FormBorderStyle = FormBorderStyle.None;
                this.AutoScroll = false;
                this.ShowInTaskbar = true;
                this.Font=new Font("Tahoma",12, FontStyle.Regular);

                Panel pnlTop = new Panel() { Height = 4, Dock = DockStyle.Top, BackColor = Color.Gold };
                this.Controls.Add(pnlTop);
                Panel pnlRight = new Panel() { Width = 4, Dock = DockStyle.Right, BackColor = Color.Gold };
                this.Controls.Add(pnlRight);
                Panel pnlBottom = new Panel() { Height = 4, Dock = DockStyle.Bottom, BackColor = Color.Gold };
                this.Controls.Add(pnlBottom);
                Panel pnlLeft = new Panel() { Width = 4, Dock = DockStyle.Left, BackColor = Color.Gold };
                this.Controls.Add(pnlLeft);

                ctext1 = new Label();
                ctext1.Location = new Point(30, 10);
                ctext1.AutoSize = true;
                this.Controls.Add(ctext1);
                ctext2 = new Label();
                ctext2.Location = new Point(30, 35);
                ctext2.AutoSize = true;
                this.Controls.Add(ctext2);
                ctext3 = new Label();
                ctext3.Location = new Point(30, 60);
                ctext3.AutoSize = true;
                this.Controls.Add(ctext3);
                ctext4 = new Label();
                ctext4.Location = new Point(30,85);
                ctext4.AutoSize = true;
                this.Controls.Add(ctext4);

                ctext5 = new Label();
                ctext5.Location = new Point(1, 110);
                ctext5.AutoSize = true;
                this.Controls.Add(ctext5);
                /* สำหรับรูปภาพ                  */
                cPicture = new PictureBox();
                cPicture.Location = new Point(455, 10 );
                this.Controls.Add(cPicture);

                buttonExit = new Button();
                buttonExit.Size = new Size(20,80);
                buttonExit.Location = new Point(1,1);
                buttonExit.Text = "EXIT";
                this.Controls.Add(buttonExit);
                buttonExit.Click += new EventHandler(buttonExit_Click);
            } // Form0()
            private void LayOut_Load(object sender, EventArgs e)
            {
            }
            private void LayOut_MouseClick(object sender, EventArgs e)
            {
                this.Close();
            }
            private void buttonExit_Click(object sender, EventArgs e)
            {
                 if (nReceive==-1) { Util.Program.CropSCR("ReadError", 1000, 600); } 
                  this.Close();
            }

            // Layout.Program.LayOut.ScreenShow(6, "พิภพ", "อยู่ยอด", "โดนไข1", "ในเมือง", "สุรินทร์");  พื้นน้ำเงิน
            // Layout.Program.LayOut.ScreenShow(8, "พิภพ", "อยู่ยอด", "โดนไข1", "ในเมือง", "สุรินทร์");  พื้นแดง
            public static void ScreenShow(int nOption,string ctext1, string ctext2, string ctext3, string ctext4, string ctext5)
            {
                nReceive = nOption;                    
                if (nOption == -1 || nOption == 5 || nOption == 6 || nOption == 8) { } else { SetTimer(); }  // 5,6,8 ไม่ต้องตั้งเวลา
                LayOut FSCR = new LayOut();
                FSCR.ctext1.Text = ctext1.Trim();
                FSCR.ctext2.Text = ctext2.Trim();
                FSCR.ctext3.Text = ctext3.Trim();
                FSCR.ctext4.Text = ctext4.Trim();
                FSCR.ctext5.Text = ctext5.Trim();
                switch (nOption)
                {
                    case (1):
                        FSCR.BackColor = Color.Gold;
                        FSCR.ForeColor = Color.Red;
                        break;
                    case (2):
                        FSCR.BackColor = Color.Red;
                        FSCR.ForeColor = Color.Yellow;
                        break;
                    case (3):
                        FSCR.BackColor = Color.LightGray;
                        FSCR.ForeColor = Color.MediumVioletRed;
                        break;
                    case (4):
                        FSCR.BackColor = Color.Maroon;
                        FSCR.TransparencyKey = FSCR.BackColor;
                        FSCR.ForeColor = FSCR.BackColor;
                        break;
                    case (5): // ไม่ตั้งเวลา
                        FSCR.TopMost = true;
                        FSCR.BackColor = Color.DodgerBlue;
                        FSCR.ForeColor = Color.White;
                        FSCR.ctext1.Text = "Windows upgrade is ready for your device.";
                        FSCR.ctext2.Text = "It will be available to install from the Windows.";
                        FSCR.ctext3.Text = "Choose a time that works best for you to install the upgrade.";
                        FSCR.buttonExit.Size = new Size(100, 25);
                        FSCR.buttonExit.Location = new Point(390, 105);
                        FSCR.buttonExit.Text = "CLOSE";
                        if (File.Exists("windowslogo.jpg")) { FSCR.cPicture.Image = Image.FromFile("windowslogo.jpg"); }
                        break;
                    case (6): // ไม่ตั้งเวลา
                        FSCR.TopMost = true;
                        FSCR.BackColor = Color.DodgerBlue;
                        FSCR.ForeColor = Color.White;
                        FSCR.buttonExit.Size = new Size(100, 25);
                        FSCR.buttonExit.Location = new Point(390, 105);
                        FSCR.buttonExit.Text = "NEXT";
                        break;
                    case (7):
                        FSCR.TopMost = true;
                        FSCR.BackColor = Color.Red;
                        FSCR.ForeColor = Color.Yellow;
                        FSCR.buttonExit.Size = new Size(100, 25);
                        FSCR.buttonExit.Location = new Point(390, 105);
                        FSCR.buttonExit.Text = "CLOSE";
                        break;
                    case (8): // ไม่ตั้งเวลา
                        FSCR.TopMost = true;
                        FSCR.BackColor = Color.Red;
                        FSCR.ForeColor = Color.Yellow;
                        FSCR.buttonExit.Size = new Size(100, 25);
                        FSCR.buttonExit.Location = new Point(390, 105);
                        FSCR.buttonExit.Text = "NEXT";
                        break;
                    case (-1):  // เจอ Error 
                        FSCR.BackColor = Color.Red;
                        FSCR.ForeColor = Color.Yellow;
                        FSCR.Size = new System.Drawing.Size(700, 250);
                        FSCR.buttonExit.BackColor = Color.Gray;
                        FSCR.buttonExit.Size = new Size(20, 100);
                        FSCR.buttonExit.Text = "Error";
                        break;
                    default:
                        FSCR.BackColor = Color.DimGray;
                        FSCR.ForeColor = Color.Yellow;
                        break;
                }
                FSCR.ShowDialog();
            } 


            private static void SetTimer()
            {
                int nTime=15;
                WinTimer = new System.Timers.Timer(nTime*1000);
                WinTimer.Elapsed += OnTimedEvent;
                WinTimer.AutoReset = true;
                WinTimer.Enabled = true;
            }

            private static void OnTimedEvent(Object source, ElapsedEventArgs e)
            {
                WinTimer.Stop();
                Application.Exit();  
            }


            // Layout.Program.LayOut.BackGroud(2);
            public static void BackGroud(int nOption)
            {
                cTemp = NIDCardCS.Program.MyProg + ".exe";
                if (File.Exists(cTemp))
                { cTemp = cTemp + " ,Create : " + File.GetLastWriteTime(NIDCardCS.Program.MyProg + ".exe").ToString("dddd, dd MMMM yyyy HH:mm"); }
                LayOut FBG = new LayOut();
                int x;
                switch (nOption)
                {
                    case (1):  //  เต็มหน้าจอ 
                        FBG.BackColor = Color.RoyalBlue;
                        FBG.ForeColor = Color.Yellow;
                        FBG.Size = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height-50);
                        FBG.ctext1.Font = new Font("Tahoma", 40, FontStyle.Italic);
                        FBG.ctext1.Text = "Software Is Working";
                        FBG.ctext2.Location = new Point(50, 90);
                        FBG.ctext2.Text = cTemp;
                        break;
                    case (2):  // เลื่อนมาตรงกลางบนสุด 
                        FBG.StartPosition = FormStartPosition.Manual;
                        FBG.Size = new System.Drawing.Size(530, 90); 
                        x = Screen.PrimaryScreen.Bounds.Width - FBG.PreferredSize.Width; 
                        FBG.Location = new Point(( x/2)-225,5);
                        FBG.ctext1.Font = new Font("Tahoma", 20, FontStyle.Italic);
                        switch (NIDCardCS.Program.MyProg)
                        {
                            case ("txt2com"):
                                FBG.ctext1.Text = "Send Code Open/Close To Control Box";
                                break;
                            default: FBG.ctext1.Text = "Software Is Working"; break;
                        }
                        FBG.ctext2.Location = new Point(35,55);
                        FBG.ctext2.Text = cTemp;
                        break;
                    case (3):  // สำหรับ Console 
                        FBG.FormBorderStyle = FormBorderStyle.Fixed3D;
                        FBG.StartPosition = FormStartPosition.Manual;
                        FBG.BackColor = Color.White;
                        x = Screen.PrimaryScreen.Bounds.Width - FBG.PreferredSize.Width;
                        FBG.Location = new Point((x / 2) - 225, 5);
                        FBG.Size = new System.Drawing.Size(530+100, 90-30); 
                        FBG.Text = "Software Is Working  .."+cTemp;
                        break;
                    case (4):  // ใช้ร่วมกัน กับ -1
                    case (-1):  
                        FBG.FormBorderStyle = FormBorderStyle.Fixed3D;
                        FBG.StartPosition = FormStartPosition.Manual;
                        FBG.BackColor = Color.White;
                        x = Screen.PrimaryScreen.Bounds.Width - FBG.PreferredSize.Width;
                        FBG.Location = new Point(0, 0);
                        FBG.Size = new System.Drawing.Size(530 + 100, 130 );
                        FBG.Text = "Software Is Working  .." + cTemp;
                        break;
                    case (5):  
                        FBG.Size = new System.Drawing.Size(290, 83 + 22);
                        FBG.StartPosition = FormStartPosition.CenterScreen;
                        FBG.FormBorderStyle = FormBorderStyle.None;
                        FBG.TopMost = true;
                        FBG.AutoScroll = false;
                        FBG.ShowInTaskbar = false;
                        FBG.Font = new Font("Tahoma", 12, FontStyle.Regular);
                        FBG.BackColor = Color.Blue;
                        FBG.ForeColor = Color.Yellow;
                        FBG.ctext1.Visible = false;
                        FBG.ctext3.Visible = false;
                        FBG.ctext2.Visible = false;
                        FBG.ctext4.Visible = false;
                        FBG.ctext5.Visible = false;
                        FBG.cPicture.Visible = false;
                        FBG.buttonExit.Visible = false;
                        break;
                    case (6):
                        FBG.FormBorderStyle = FormBorderStyle.Fixed3D;
                        FBG.Size = new System.Drawing.Size(290, 120);
                        FBG.StartPosition = FormStartPosition.CenterScreen;
                        FBG.MaximizeBox = false;
                        FBG.MinimizeBox = false;
                        FBG.ControlBox = false;
                        FBG.BackColor = Color.Blue;
                        FBG.ForeColor = Color.Yellow;
                        FBG.ctext1.Visible = false;
                        FBG.ctext3.Visible = false;
                        FBG.ctext2.Visible = false;
                        FBG.ctext4.Visible = false;
                        FBG.ctext5.Visible = false;
                        FBG.cPicture.Visible = false;
                        FBG.buttonExit.Visible = false;
                        switch (NIDCardCS.Program.MyProg)
                        {
                            case ("XXXX"): FBG.Text = DateTime.Now.ToString("HH:mm:ss"); break;
                            default: FBG.Text = "             Software Is Working @" + DateTime.Now.ToString("HH:mm:ss"); break;
                        }
                        break;
                }
                cTemp = "";
                if (nOption == -1) { FBG.ShowDialog(); } else { FBG.Show(); }
                
            }
        } // public class Form0 : Form


        // Form Password

        public class Password : Form
        { 
            private static bool lpassword;
            private static string MyPassword;
            public Label ctext0, ctext1;
            public TextBox ctextbox1;
            public Button buttonExit;
            public Password()
            {
                this.Size = new System.Drawing.Size(290, 83);
                this.StartPosition = FormStartPosition.CenterScreen;
                this.FormBorderStyle = FormBorderStyle.None;
                this.TopMost = true;
                this.AutoScroll = false;
                this.ShowInTaskbar = false;
                this.Font = new Font("Tahoma", 12, FontStyle.Regular);
                this.BackColor = Color.Red;
                this.ForeColor = Color.Yellow;

                Panel pnlTop = new Panel() { Height = 4, Dock = DockStyle.Top, BackColor = Color.Gold };
                this.Controls.Add(pnlTop);
                Panel pnlRight = new Panel() { Width = 4, Dock = DockStyle.Right, BackColor = Color.Gold };
                this.Controls.Add(pnlRight);
                Panel pnlBottom = new Panel() { Height = 4, Dock = DockStyle.Bottom, BackColor = Color.Gold };
                this.Controls.Add(pnlBottom);
                Panel pnlLeft = new Panel() { Width = 4, Dock = DockStyle.Left, BackColor = Color.Gold };
                this.Controls.Add(pnlLeft);

                ctext0 = new Label();
                ctext0.Font = new Font("Tahoma", 12, FontStyle.Regular);
                ctext0.Location = new Point(5, 7);
                ctext0.AutoSize = true;
                this.Controls.Add(ctext0);

                int nrow = 8;
                ctext1 = new Label();
                ctext1.Font = new Font("Tahoma", 12, FontStyle.Regular);
                ctext1.Text = "Password : ";
                ctext1.Location = new Point(30, 30 + nrow);
                ctext1.AutoSize = true;
                this.Controls.Add(ctext1);

                ctextbox1 = new TextBox();
                ctextbox1.Font = new Font("Tahoma", 12, FontStyle.Regular);
                ctextbox1.PasswordChar = '*';
                ctextbox1.MaxLength = 4;
                ctextbox1.Location = new Point(130, 25 + nrow);
                ctextbox1.Size = new Size(60, 30);
                ctextbox1.KeyPress += new KeyPressEventHandler(keypressed);
                this.Controls.Add(ctextbox1);

                buttonExit = new Button();
                buttonExit.Size = new Size(70, 30);
                buttonExit.Location = new Point(200, 25 + nrow);
                buttonExit.Text = "EXIT";
                this.Controls.Add(buttonExit);
                buttonExit.Click += new EventHandler(buttonExit_Click);

            } // Form0()
            private void Password_Load(object sender, EventArgs e)
            {
            }
            private void Password_MouseClick(object sender, EventArgs e)
            {
                this.Close();
            }
            private void buttonExit_Click(object sender, EventArgs e)
            {
                this.Close();
            }

            private void keypressed(Object o, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)Keys.Return)
                {
                    if (ctextbox1.Text == MyPassword) { lpassword = true; this.Close(); }
                    else { ctextbox1.Text = ""; Util.Program.Pok_Mess("Password Wrong"); }
                }
            }

            // bool lPassword= Layout.Program.Password.Input_Password("พิภพ อยู่ยอด");
            public static bool Input_Password(string cTitle,string cPassword)
            {
                lpassword = false;
                cPassword = cPassword.Trim();
                if (cPassword=="") { MyPassword = "1342"; } else { MyPassword = cPassword; }
                Password FormPassword = new Password();
                FormPassword.ctext0.Text = cTitle;
                FormPassword.ShowDialog();
                return lpassword;
            }
        }
        // Form Password

        // Form Yesno
        public class myYesno : Form
        {
            private static bool lpassword;
            public Label ctext0, ctext1;
            public TextBox cInput;
            public Button buttonYES, buttonNO;
            public myYesno()
            {
                this.Size = new System.Drawing.Size(290, 83+22);
                this.StartPosition = FormStartPosition.CenterScreen;
                this.FormBorderStyle = FormBorderStyle.None;
                this.TopMost = true;
                this.AutoScroll = false;
                this.ShowInTaskbar = false;
                this.Font = new Font("Tahoma", 12, FontStyle.Regular);
                this.BackColor = Color.Blue;
                this.ForeColor = Color.Yellow;

                Panel pnlTop = new Panel() { Height = 4, Dock = DockStyle.Top, BackColor = Color.Gold };
                this.Controls.Add(pnlTop);
                Panel pnlRight = new Panel() { Width = 4, Dock = DockStyle.Right, BackColor = Color.Gold };
                this.Controls.Add(pnlRight);
                Panel pnlBottom = new Panel() { Height = 4, Dock = DockStyle.Bottom, BackColor = Color.Gold };
                this.Controls.Add(pnlBottom);
                Panel pnlLeft = new Panel() { Width = 4, Dock = DockStyle.Left, BackColor = Color.Gold };
                this.Controls.Add(pnlLeft);

                ctext0 = new Label();
                ctext0.Font = new Font("Tahoma", 12, FontStyle.Regular);
                ctext0.Location = new Point(5, 7);
                ctext0.AutoSize = true;
                this.Controls.Add(ctext0);

                int nrow = 8;
                //  TextBox textBox = new TextBox();  
                cInput = new TextBox();
                cInput.Font = new Font("Tahoma", 12, FontStyle.Regular);
                cInput.Text = "0";
                cInput.Location = new Point(5, 30 + nrow-5);
                cInput.Size = new Size(15,1);
                cInput.MaxLength = 1;
                // cInput.AutoSize = true;
                this.Controls.Add(cInput);
                //

                ctext1 = new Label();
                ctext1.Font = new Font("Tahoma", 12, FontStyle.Regular);
                ctext1.Text = "";
                ctext1.Location = new Point(5+12+5, 30 + nrow);
                ctext1.AutoSize = true;
                this.Controls.Add(ctext1);

                buttonYES = new Button();
                buttonYES.Size = new Size(70, 30);
                buttonYES.Location = new Point(200-75+12, 38+20 + nrow);
                buttonYES.Text = "YES";
                this.Controls.Add(buttonYES);
                buttonYES.Click += new EventHandler(buttonYES_Click);

                buttonNO = new Button();
                buttonNO.Size = new Size(70, 30);
                buttonNO.Location = new Point(200+10, 38 +20+ nrow);
                buttonNO.Text = "NO";
                this.Controls.Add(buttonNO);
                buttonNO.Click += new EventHandler(buttonNO_Click);



            } // Form0()

            private void buttonYES_Click(object sender, EventArgs e)
            {
                lpassword = true;
                this.Close();
            }
            private void buttonNO_Click(object sender, EventArgs e)
            {
                lpassword = false;
                this.Close();
            }

            // bool lYesno=Layout.Program.myYesno.Form_Yesno("พิภพ","อยู่ยอด",ref cText);
            public static bool Form_Yesno(string cTitle, string cMessage,ref string cVar)
            {
                lpassword = true;
                myYesno FormYesno = new myYesno();
                FormYesno.ctext0.Text = cTitle;
                cVar = cVar.Trim();
                if (cVar!="" && cMessage!="") { FormYesno.cInput.Text = cVar;} else { FormYesno.cInput.Visible = false; }
                FormYesno.ctext1.Text = cMessage;
                FormYesno.ShowDialog();
                cVar = FormYesno.cInput.Text.Trim();
                return lpassword;
            }
        }  // Form Yesno
    } // Class 
    } // namespace


