using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LC_ConfigCamer
{
    public partial class Form1 : Form
    {
        public static string[] arr_ip;
        public static string old_pass, new_pass;
        public static TextBox tb;
        public static bool Web_Conn = true, GetOrCheck = true;
        
        public Form1()
        {
            InitializeComponent();
            tb = tb_log;
#if DEBUG
            tb_list_old_pass.Text = "vg5-_kcc";
            tb_new_pass.Text = "vg5-_kcc";
            var a1 = Environment.CurrentDirectory;
            System.Windows.Forms.MessageBox.Show(a1);
#endif
        }

        private void btn_opent_file_Click(object sender, EventArgs e)
        {
            Array.Clear(arr_ip, 0, arr_ip.Length);
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
                return;
            string filename = openFileDialog1.FileName;
            var fileStream = openFileDialog1.OpenFile();
            var fileText = string.Empty;
            using (StreamReader reader = new StreamReader(fileStream))
            {
                fileText = reader.ReadToEnd();
            }
            arr_ip = fileText.Replace("\r", "").Split('\n');// new char[] { '\r', '\n' });
            lbl_count_cams_in_list.Text = arr_ip.Count().ToString();
            Log_add($"Получен список ip. {arr_ip.Count()} шт.");
            groupBox2.Enabled = true;
        }

        private void tb_list_old_pass_TextChanged(object sender, EventArgs e)
        {
            groupBox3.Enabled = true;
            
        }

        private void tb_new_pass_TextChanged(object sender, EventArgs e)
        {
            if (tb_new_pass.Text.Length < 8)
            {
                lbl_valid_new_pass.Text = "ПЛОХОЙ ПАРОЛЬ";
                lbl_valid_new_pass.ForeColor = System.Drawing.Color.Crimson;
                btn_check_params.Enabled = false;
            }
            else
            {
                lbl_valid_new_pass.Text = "ОК";
                lbl_valid_new_pass.ForeColor = System.Drawing.Color.Green;
                btn_check_params.Enabled = true;
            }
        }

        private void btn_check_params_Click(object sender, EventArgs e)
        {
            try
            {
                old_pass = tb_list_old_pass.Text;
                new_pass = tb_new_pass.Text;
                btn_start.Enabled = true;
                Log_add($"Получен старый пароль: {old_pass}");
                Log_add($"Получен новый пароль: {new_pass}");
            }
            catch (Exception)
            {
                MessageBox.Show("Что-то не так! Перепроверь все!");
            }
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            var Set_New_Pass = new Selenium_Work(arr_ip, old_pass, new_pass);
            Set_New_Pass.Start();
        }

        private void btn_check_params1_Click(object sender, EventArgs e)
        {
            try
            {
                new_pass = tb_new_pass.Text;
                btn_start.Enabled = true;
                Log_add($"Получен новый пароль: {new_pass}");
                if(rb_Conn_Ssh.Checked) Web_Conn=false;
                else Web_Conn=true;
                Log_add($"Получен параметр соединения");
                if (rb_SetSync_Time.Checked) GetOrCheck = false;
                else GetOrCheck = true;
                Log_add($"Получен параметр действия");
            }
            catch (Exception)
            {
                MessageBox.Show("Что-то не так! Перепроверь все!");
            }
        }

        
        
        
        
        
        public static Task Log_add(string txt, bool err = false)
        {
            var new_txt = string.Empty;
            if (err)
                new_txt = "  !   | " + txt;
            else
                new_txt = "  -   | " + txt;
            var temp_txt = tb.Text;
            tb.Text = temp_txt + $"{DateTime.Now:T} | {new_txt}" + Environment.NewLine;
            return Task.CompletedTask;
        }
    }
}
