using ManualSoftwareSystem.Operation_Manual;
using Microsoft.TeamFoundation.Common.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Management;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace ManualSoftwareSystem.lib
{
    internal class CheckSerialUSB
    {
        public static string strCheckSerialUSB = string.Empty;
        public static string SerialUSB = string.Empty;     
        public static List<string> SerialUSBLicense = new List<string>() 
        {
            "4C530000080312114350" //Flash Drive Kamol
            ,"0501781957f7affe116e" // Flash Drive Shodensha (Thailand)
            ,"01012cd7a6ac8b60f5dd" // Flash Drive Shodensha (Thailand)
            ,"125C20100726" //Card Reader Kamol
            ,"0123-4567" //SD Card Shodensha (Thailand)
            ,"DC21-8175" //SD Card Kamol


        };

        public static bool checkDrive(string drive)
        {
            //string CheckSerialUSB = string.Empty;

            strCheckSerialUSB = RunCommand("dir " + drive + ":", true).Trim().Replace("is ", "").Replace("/r", "").Replace("/n", "");

            foreach (var item in SerialUSBLicense)
            {
                if (strCheckSerialUSB == item)
                {
                    return true;
                }
            }
            return false;
        }   

        public static string CheckSerialUSBLicense()
        {
            string CheckSerialUSBStart = string.Empty;     //Use License
            //string CheckSerialUSB = string.Empty;     //Use License

            if (FrmOperationManualOperator.isTrial == true)
            {
                //CheckSerialUSBStart = "4C530000080312114350";
                CheckSerialUSBStart = "OK";
            }
            else
            {
                CheckSerialUSBStart = string.Empty;
            }

            //CheckSerialUSBStart = "4C530000080312114350";    //Test no License

            //MessageBox.Show(RunCommand("dir e:", true));

            for (int i = 0; i < 24; i++)
            {
                switch (i)
                {
                    case 0:                      
                        if (checkDrive("c") == true)
                        {
                            return strCheckSerialUSB;
                        }

                        break;
                     case 1:
                        if (checkDrive("d") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 2:
                        if (checkDrive("e") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 3:
                        if (checkDrive("f") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 4:
                        if (checkDrive("g") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 5:
                        if (checkDrive("h") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 6:
                        if (checkDrive("i") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 7:
                        if (checkDrive("j") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 8:
                        if (checkDrive("k") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 9:
                        if (checkDrive("l") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 10:
                        if (checkDrive("m") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 11:
                        if (checkDrive("n") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 12:
                        if (checkDrive("o") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 13:
                        if (checkDrive("p") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 14:
                        if (checkDrive("q") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 15:
                        if (checkDrive("r") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 16:
                        if (checkDrive("s") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 17:
                        if (checkDrive("t") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 18:
                        if (checkDrive("u") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 19:
                        if (checkDrive("v") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 20:
                        if (checkDrive("w") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 21:
                        if (checkDrive("x") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 22:
                        if (checkDrive("y") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                    case 23:
                        if (checkDrive("z") == true)
                        {
                            return strCheckSerialUSB;
                        }
                        break;
                        //default:
                }

            }

            //CheckSerialUSB = RunCommand("dir e:", true).Trim().Replace("is ","").Replace("/r","").Replace("/n","");

            //foreach (var item in SerialUSBLicense)
            //{
            //    if (CheckSerialUSB == item)
            //    {
            //        return CheckSerialUSB;
            //    }
            //}

            ManagementObjectSearcher theSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");
            foreach (ManagementObject currentObject in theSearcher.Get())
            {

                if (FrmOperationManualOperator.isTrial == true)
                {
                    //CheckSerialUSBStart = "4C530000080312114350";
                    //CheckSerialUSBStart = "Get";
                    CheckSerialUSBStart = "OK";
                }
                else
                {
                    CheckSerialUSBStart = "Get";
                }

                //CheckSerialUSBStart = "4C530000080312114350";    //Test no License

                ManagementObject theSerialNumberObjectQuery = new ManagementObject("Win32_PhysicalMedia.Tag='" + currentObject["DeviceID"] + "'");
                //MessageBox.Show(theSerialNumberObjectQuery["SerialNumber"].ToString());

                strCheckSerialUSB = theSerialNumberObjectQuery["SerialNumber"].ToString();
                
                foreach (var item in SerialUSBLicense)
                {
                    if (strCheckSerialUSB == item)
                    {
                        return strCheckSerialUSB;
                    }
                }

            }

            strCheckSerialUSB = CheckSerialUSBStart;

            return strCheckSerialUSB;
        }

        public static string RunCommand(string arguments, bool readOutput)
        {
            var output = string.Empty;
            try
            {
                var startInfo = new ProcessStartInfo
                {
                    Verb = "runas",
                    FileName = "cmd.exe",
                    Arguments = "/C " + arguments,
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = false
                };

                var proc = Process.Start(startInfo);

                if (readOutput)
                {
                    output = proc.StandardOutput.ReadToEnd();

                    output = getBetween(output, "Number", "Directory");
                    
                }

                proc.WaitForExit(60000);

                return output;
            }
            catch (Exception)
            {
                return output;
            }
        }

        public static string getBetween(string strSource, string strStart, string strEnd)
        {
            if (strSource.Contains(strStart) && strSource.Contains(strEnd))
            {
                int Start, End;
                Start = strSource.IndexOf(strStart, 0) + strStart.Length;
                End = strSource.IndexOf(strEnd, Start);
                return strSource.Substring(Start, End - Start);
            }

            return "";
        }

    }

    

}
