using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Device.Location;
using System.Net.NetworkInformation;
using System.Diagnostics;
using System.IO;
using System.Globalization;

namespace Uyod4WEB
{
    public partial class FirstLogin : Form
    {

        private static string mSoftwareID="";
        public FirstLogin(string ctxt)
        {
            InitializeComponent();
            // this.BackColor = Color.White;
            // this.TransparencyKey = this.BackColor;
            this.Top = 0;this.Left = 0;
            this.mLine0.Left = 0;
            this.mLine0.Width = this.Width;
            this.mLine1.Left = 0;
            this.mLine1.Width = this.Width;
            this.mLine2.Left = 0;
            this.mLine2.Width = this.Width;
            this.ostext.Text = "64 BIT Is "+ Environment.Is64BitOperatingSystem;
            this.domaintext.Text = Environment.UserDomainName;
            this.computertext.Text = Environment.MachineName;
            this.usertext.Text = Environment.UserName;
            this.foldertext.Text = Environment.CurrentDirectory;
            string textfile = "UserLogin.txt";
            if (File.Exists(textfile))
            {
                string[] lines = File.ReadAllLines(textfile);
                this.mCompany.Text=lines[0];
                this.mUser.Text = lines[1];
                this.mAddress.Text = lines[2];
                this.mTel.Text = lines[3];
                mSoftwareID = lines[4];
                FirstRefresh();
                TimeSpan ts=(DateTime.Now - File.GetCreationTime(textfile));
                int nday = Convert.ToInt32(ts.TotalDays);
                if (nday > 5) {
                    this.mCompany.Enabled = false;
                    this.mUser.Enabled = false;
                    this.mAddress.Enabled = false;
                    this.mTel.Enabled = false;
                }
            }
            // Check Public IP
            string url = "http://checkip.dyndns.org";
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd().Trim();
            string[] a = response.Split(':');
            string a2 = a[1].Substring(1);
            string[] a3 = a2.Split('<');
            this.iptext.Text = a3[0];
            // หาค่า Location 
            GeoCoordinateWatcher watcher;
            watcher = new GeoCoordinateWatcher();
            watcher.PositionChanged += (sender, e) =>
            {
                var coordinate = e.Position.Location;
                this.mLatitude.Text = coordinate.Latitude.ToString();
                this.mLongitude.Text = coordinate.Longitude.ToString();
            };
            watcher.Start();
            System.Threading.Thread.Sleep(3000); // n*1000 = 1วินาที
			
			string cAddress = "";
            foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (networkInterface.OperationalStatus == OperationalStatus.Up)
                {
                    cAddress += networkInterface.GetPhysicalAddress().ToString();
                    break;
                }
            } 
            this.maccaddresstext.Text = cAddress;
        }




        private void FirstRefresh()
        {
           String cCompany=this.mCompany.Text.Trim();
           String cuser = this.mUser.Text.Trim();
           String cAddress = this.mAddress.Text.Trim();
           String cTel = this.mTel.Text.Trim();
            if (cCompany == "" || cuser == "" || cAddress == "" || cTel == "")
                { this.nfSAVE.Visible = false; }
            else {
                this.nfSAVE.Visible = true; this.mDescript.Text="Software Code : "+mSoftwareID;
            }
        }

        
        
        // สำหรับตัดคำที่ไม่ต้องการ 
        public static string Text2Replace(string mText)
        {
            string cWordTake, cWordFor;
            mText = mText.Trim();
            cWordFor = "";
            for (int i = 0; i < mText.Length; i++)
            {
                cWordTake = mText.Substring(i, 1);
                switch (cWordTake)
                {
                    case (" "):
                        cWordFor = cWordFor+"" ; 
                        break;
                    default:
                        cWordFor = cWordFor + cWordTake; break;
                }
            }
            return cWordFor;
        }



        private void FirstLogin_Load(object sender, EventArgs e)
        {
            this.TopMost = true;
        }

        private void label10_Click(object sender, EventArgs e)
        {

        }

        private void mCompany_Validated(object sender, EventArgs e)
        {
            this.Text = this.Text.Trim();
            FirstRefresh();
        }

        private void mCompany_TextChanged(object sender, EventArgs e)
        {

        }

        private void mUser_Validated(object sender, EventArgs e)
        {
            this.Text = this.Text.Trim();
            FirstRefresh();
        }

        private void mAddress_TextChanged(object sender, EventArgs e)
        {

        }

        private void mAddress_Validated(object sender, EventArgs e)
        {
            this.Text = this.Text.Trim();
            FirstRefresh();
        }

        private void mTel_Validated(object sender, EventArgs e)
        {
            this.Text = this.Text.Trim();
            FirstRefresh();
        }



        private void nfSAVE_Click(object sender, EventArgs e)
        {
            string ctemp, cDrive, cWord;
            // สร้างไฟล์ลงทะเบียนแล้ว 
            if (Directory.Exists("C:")) { cDrive = "C:"; } else { cDrive = ""; }
            cDrive = cDrive + "\\Temp";
            if (!Directory.Exists(cDrive)) { Directory.CreateDirectory(cDrive); }
            ctemp = cDrive + "\\FirstUSE.Prog";
            if (!File.Exists(ctemp)) { File.Copy("Temp4WEB.txt", ctemp); }
            // ทำเพื่อ D:\\BdPos\\ ด้วย 
            ctemp = cDrive + "\\FirstUSE.log";
            if (!File.Exists(ctemp)) { File.Copy("Temp4WEB.txt", ctemp); }
            ctemp = "FirstUSE.log";
            if (!File.Exists(ctemp)) { File.Copy("Temp4WEB.txt", ctemp); }
            // Save หน้าจอการลงทะเบียน  
            Uyod4WEB.Process2Arg("UtilPok3","1 C:\\Temp FirstLogin");
            // Save ฐานข้อมูล uyod_register
            // Uyod4SQL 12 666.999.999.999 LATITUDE LONGTITIDE  MACADRESS Company User Address TelePhone
            cWord = "12" + " ";
            ctemp = this.iptext.Text;ctemp = Text2Replace(ctemp);cWord = cWord + ctemp+" ";
            ctemp = this.mLatitude.Text; ctemp = Text2Replace(ctemp); cWord = cWord + ctemp+" ";
            ctemp = this.mLongitude.Text; ctemp = Text2Replace(ctemp); cWord = cWord + ctemp + " ";
            ctemp = this.maccaddresstext.Text; ctemp = Text2Replace(ctemp); cWord = cWord + ctemp + " ";
            ctemp = this.mCompany.Text; ctemp = Text2Replace(ctemp); cWord = cWord + ctemp + " ";
            ctemp = this.mUser.Text; ctemp = Text2Replace(ctemp); cWord = cWord + ctemp + " ";
            ctemp = this.mAddress.Text; ctemp = Text2Replace(ctemp); cWord = cWord + ctemp + " ";
            ctemp = this.mTel.Text; ctemp = Text2Replace(ctemp); cWord = cWord + ctemp + " ";
            Uyod4WEB.Process2Arg("bdposvfp4SQL",cWord);
            // ส่งข้อมูลไปไลน์ 
            cWord = "5 9s2uTS8ibhTC4dNpJEmM6FM3Qh7HKUGRcxpYi7CvkZn" + " ";
            ctemp = "*IP : " + this.iptext.Text.Trim();
            ctemp = ctemp+"*LATITUDE : " + this.mLatitude.Text.Trim();
            ctemp = ctemp + "*LONGTITUDE : " + this.mLongitude.Text.Trim();
            ctemp = ctemp + "**Company : " + this.mCompany.Text.Trim();
            ctemp = ctemp + "*User : " + this.mUser.Text.Trim();
            ctemp = ctemp + "*Address : " + this.mAddress.Text.Trim();
            ctemp = ctemp + "*Telephone : " + this.mTel.Text.Trim();
            Uyod4WEB.Process2Arg("Util_Pok", cWord + ctemp);
            ctemp = "*Google Map : " + "https://www.google.co.th/maps/@";
            ctemp = ctemp + this.mLatitude.Text.Trim() + "," + this.mLongitude.Text.Trim() + ",15z?hl=th";
            Uyod4WEB.Process2Arg("Util_Pok", cWord + ctemp);
            //จบ ส่งข้อมูลไปไลน์ 
            // Save ลง Text File 
            if (mSoftwareID=="")
            {
                CultureInfo Region4GB = new CultureInfo("en-GB");
                mSoftwareID='u'+ DateTime.Now.ToString("ddMMyyyy", Region4GB);
            }
            string textfile = "UserLogin.txt";
            TextWriter Uyod2Txt = new StreamWriter(textfile);
            Uyod2Txt.WriteLine(this.mCompany.Text);
            Uyod2Txt.WriteLine(this.mUser.Text);
            Uyod2Txt.WriteLine(this.mAddress.Text);
            Uyod2Txt.WriteLine(this.mTel.Text);
            Uyod2Txt.WriteLine(mSoftwareID);
            Uyod2Txt.Close();
            Uyod4WEB.ntime = -5;
            this.Close();
        }


        private void mCompany_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void mUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void mAddress_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void mTel_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
        }

        private void nfSAVE_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                nfSAVE_Click((object)sender, (EventArgs)e);
            }
        }

        private void nfCancel_Click(object sender, EventArgs e)
        {
            Uyod4WEB.ntime = -5;
            this.Close();
        }
    }
}
