/*
        public static void initVLC()
        {
            if (Environment.Is64BitOperatingSystem)
            {
                // Set libvlc.dll and libvlccore.dll directory path
                VlcContext.LibVlcDllsPath = @"VLC\";

                // Set the vlc plugins directory path
                VlcContext.LibVlcPluginsPath = @"VLC\plugins";
            }
            else
            {
                // Set libvlc.dll and libvlccore.dll directory path
                VlcContext.LibVlcDllsPath = @"VLC\";

                // Set the vlc plugins directory path
                VlcContext.LibVlcPluginsPath = @"VLC\plugins";
            }

            // Ignore the VLC configuration file
            VlcContext.StartupOptions.IgnoreConfig = true;
#if DEBUG
            // Enable file based logging
            VlcContext.StartupOptions.LogOptions.LogInFile = true;

            // Shows the VLC log console (in addition to the applications window)
            VlcContext.StartupOptions.LogOptions.ShowLoggerConsole = true;
#else
            VlcContext.StartupOptions.LogOptions.ShowLoggerConsole = false;
            VlcContext.StartupOptions.LogOptions.LogInFile = false;
#endif
            // Set the log level for the VLC instance
            VlcContext.StartupOptions.LogOptions.Verbosity = VlcLogVerbosities.Debug;
            VlcContext.StartupOptions.AddOption("--ffmpeg-hw");
            // Disable showing the movie file name as an overlay
            VlcContext.StartupOptions.AddOption("--no-video-title-show");
            VlcContext.StartupOptions.AddOption("--rtsp-tcp");
            VlcContext.StartupOptions.AddOption("--rtsp-mcast");
            // VlcContext.StartupOptions.AddOption("--rtsp-host=192.168.10.35");
            // VlcContext.StartupOptions.AddOption("--sap-addr=192.168.10.35");
            VlcContext.StartupOptions.AddOption("--rtsp-port=8554");
            VlcContext.StartupOptions.AddOption("--rtp-client-port=8554");
            VlcContext.StartupOptions.AddOption("--sout-rtp-rtcp-mux");
            VlcContext.StartupOptions.AddOption("--rtsp-wmserver");


            VlcContext.StartupOptions.AddOption("--file-caching=18000");
            VlcContext.StartupOptions.AddOption("--sout-rtp-caching=18000");
            VlcContext.StartupOptions.AddOption("--sout-rtp-port=8554");
            VlcContext.StartupOptions.AddOption("--sout-rtp-proto=tcp");
            VlcContext.StartupOptions.AddOption("--network-caching=1000");

            VlcContext.StartupOptions.AddOption("--vout-filter=wall");
            VlcContext.StartupOptions.AddOption("--wall-cols=2");
            VlcContext.StartupOptions.AddOption("--wall-rows=2");

            // Pauses the playback of a movie on the last frame
            VlcContext.StartupOptions.AddOption("--play-and-pause");
            VlcContext.CloseAll();
            // Initialize the VlcContext
            VlcContext.Initialize();
        }
        */

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            bool login = true;
            Rows = new List<CsvRow>();
            RowsSend = new List<CsvRow>();
            Fonts = ExCss.ReadFile(@"Asset\Fonts\font-awesome.min.css");
            Timethread = new Thread(CheckTimeFunctionThread);
            Timethread.IsBackground = true;
            listSerialPort = new List<SerialPort>();
            QueueCMD = new Queue<string>();
            m_bInitSDK = CHCNetSDK.NET_DVR_Init();
            if (m_bInitSDK == false)
            {
                MessageBox.Show("NET_DVR_Init error!");
                return;
            }
            if (!File.Exists(_FILE_CSV_COMMAND))
            {
                File.Create(_FILE_CSV_COMMAND);
            }
            if (!File.Exists(_FILE_Send_COMMAND))
            {
                File.Create(_FILE_Send_COMMAND);
            }
            //  initVLC();
            DefineCommand = CommandDefine.Read(_FILE_DEFINE_COMMAND);
            setting = Config.Read(_FILE_Config);
            if (!Directory.Exists("Data"))
            {
                DirectoryInfo di = Directory.CreateDirectory("Data");
                di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
            }
            if (!Directory.Exists(setting.Folder))
            {
                Directory.CreateDirectory(setting.Folder);
            }
            curDate = DateTime.Now;
            Timethread.Start();
            curFolder = System.IO.Path.Combine(setting.Folder, this.curDate.ToString("dd-MM-yyyy"));
            if (!Directory.Exists(curFolder))
            {
                Directory.CreateDirectory(curFolder);
            }
            DataUser = Users.read(@"Data\User.mtc");
            if (DataUser == null)
            {
                DataUser = new Users();
                User root = new User("root", 1);
                root.Pass = "******".toMD5();
                root.type = 0;
                root.FullName = "Root";
                DataUser.Add(root);
                Users.write(_FILE_User_Data, DataUser);
            }
            DataCamera = Cameras.Read(_FILE_Camera_Data);
            DataPreset = Presets.Read(_FILE_PRESET_DATA);
            DataAlarm = Alarms.Read(_FILE_Alarm_Data);
            this.checkFile = new Thread(checkFileFunctionThread);
            checkFile.IsBackground = true;
            this.checkFile.Start();
#if DEBUG
            if (DataCamera.Count == 0)
            {
                Camera camera = new Camera("192.168.10.199");
                camera.name = "Camera Demo";
                camera.channel = 1;
                camera.port = 8000;
                camera.admin = "admin";
                camera.pass = "******";
                camera.icon = "fa-video-camera";
                DataCamera.Add(camera);
            }
            
            if (DataUser.Datas.Count < 2)
            {
                User root = new User("admin", 2);
                root.Pass = "******".toMD5();
                root.type = 1;
                root.FullName = "Admin";
                DataUser.Add(root);

                User root2 = new User("htdm",3);
                root2.Pass = "******".toMD5();
                root2.type = 2;
                root2.FullName = "Camera";
                DataUser.Add(root2);
                Users.write(_FILE_User_Data, DataUser);
            }
            
#endif
            var listCom = getListCOM();
            if (listCom.Length > 0)
            {
                foreach (string i in listCom)
                {
                    try
                    {
                        SerialPort serialPort = new SerialPort();
                        serialPort = new SerialPort();
                        serialPort.openCOM(i, BAUDRATE, DATABITS, StopBits.One);
                        serialPort.DataReceived += serialPort_DataReceived;
                        serialPort.sendCommand("#0$");
                        listSerialPort.Add(serialPort);
                    }
                    catch (Exception)
                    {

                    }
                }

            }
            Map = Map.Read(_FILE_Map_Data);
            for (int i = 0; i != e.Args.Length; i += 2)
            {
                if (e.Args[i] == "-u")
                {
                    string hash = e.Args[i + 1];
                    User u = App.DataUser.Login(hash);
                    if (u != null)
                    {
                        login = false;
                        App.User = u;
                    }
                }
                else if (e.Args[i] == "-mode")
                {
                    Mode = (Camera_Final.Mode)int.Parse(e.Args[i + 1]);
                }
            }
            if (login)
            {
                this.MainWindow = new Login();
            }
            else
            {
                this.MainWindow = new MainWindow();
            }
            this.MainWindow.Show();
        }
 public bool Remove(User u)
 {
     return this.Datas.Remove(u);
 }
 private void SaveUser(object sender, MouseButtonEventArgs e)
 {
     if (this.UIName.isEmpty())
     {
         MessageBox.Show("Tên User không được phép để trống", "Thông báo");
         return;
     }
     if (this.UIUserName.isEmpty())
     {
         MessageBox.Show("Tên đăng nhập không được phép để trống", "Thông báo");
         return;
     }
     if (this.UIType.isEmpty())
     {
         MessageBox.Show("Loại User không được phép để trống", "Thông báo");
         return;
     }
     if (this.u == null && this.UIPass.Password == string.Empty)
     {
         MessageBox.Show("Mật khẩu đăng nhập không được phép để trống", "Thông báo");
         return;
     }
     if (this.UIPass.Password != this.UIPassAgain.Password)
     {
         MessageBox.Show("Mật khẩu không khớp vui lòng kiểm tra lại", "Thông báo");
         return;
     }
     if (this.u != null && this.UIOldPass.Password.toMD5() != this.u.Pass)
     {
         MessageBox.Show("Mật khẩu cũ không đúng vui lòng kiểm tra lại", "Thông báo");
     }
     if (this.u == null)
     {
         User u = new User(0);
         u.FullName = this.UIName.Text;
         u.Pass = this.UIPass.Password.toMD5();
         u.type = Convert.ToInt32(this.UIType.Text);
         u.user_name = this.UIUserName.Text;
     }
     else
     {
         u.FullName = this.UIName.Text;
         if(this.UIPass.Password!=string.Empty)
         u.Pass = this.UIPass.Password.toMD5();
         u.type = Convert.ToInt32(this.UIType.Text);
     }
     if (this.CloseEvent != null)
     {
         this.CloseEvent(this, new EventArgs());
     }
 }
        public bool Add(User u)
        {
            if (this.Datas == null)
                this.Datas = new List<User>();
            if (u.id == 0 || string.IsNullOrEmpty(u.user_name))
                return false;
            foreach (User tmpU in this.Datas)
            {
                if (u.user_name == tmpU.user_name || u.id == tmpU.id)
                {
                    return false;
                }
            }

            this.Datas.Add(u);
            return true;
        }