using ScaBox30.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScaBox30
{
    public partial class Configuracion : Form
    {
        List<string> printers = new List<string>();


        public Configuracion()
        {
            InitializeComponent();
            InitializeItems();
            InitializeButtons();
        }


        private void Configuracion_Load(object sender, EventArgs e)
        {
            ListPrinters();
            LoadConfigJson(Globals.config_json);

            switch (Globals.cfg.printertype)
            {
                case "usb":
                    radioButtonUsb.Checked = true;
                    break;
                case "eth":
                    radioButtonEthernet.Checked = true;
                    break;
            }

            switch (Globals.cfg.resultado)
            {
                case "label":
                    radioButton1.Checked = true;
                    break;
                case "number":
                    radioButton2.Checked = true;
                    break;
                case "numberlabel":
                    radioButton3.Checked = true;
                    break;
            }


        }


        private void ListPrinters()
        {


            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                printers.Add(printer);
            }
            comboPrinters.Items.AddRange(printers.ToArray());
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            Globals.cfg.resultado = "label";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            Globals.cfg.resultado = "number";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            Globals.cfg.resultado = "numberlabel";
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Ayuda help = new Ayuda();
            help.ShowDialog();
        }

        private void radioButtonUsb_CheckedChanged(object sender, EventArgs e)
        {
            Globals.cfg.printertype = "usb";
            comboPrinters.Enabled = true;
            TextBoxImpresoraIp.Enabled = false;
        }

        private void radioButtonEthernet_CheckedChanged(object sender, EventArgs e)
        {
            Globals.cfg.printertype = "eth";
            comboPrinters.Enabled = false;
            TextBoxImpresoraIp.Enabled = true;
        }

        private void LoadConfigJson(string jsonfile)
        {
            if (File.Exists(jsonfile))
            {
                string jsoncontent = File.ReadAllText(jsonfile);
                Globals.config = JsonSerializer.Deserialize<List<Config>>(jsoncontent);

                numericLimpieza.Value = Globals.config[0].cleardelay;
                numericRespuesta.Value = Globals.config[0].interval;
                TextBoxScannerIp.Text = Globals.config[0].ipscanner;
                comboPrinters.SelectedItem = Globals.config[0].usbprinter;
                TextBoxImpresoraIp.Text = Globals.config[0].ipprinter;
                textBoxHostIp.Text = Globals.config[0].iphost;
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Globals.cfg.cleardelay = (int)numericLimpieza.Value;
            Globals.cfg.interval = (int)numericRespuesta.Value;
            Globals.cfg.ipscanner = TextBoxScannerIp.Text;
            Globals.cfg.usbprinter = comboPrinters.SelectedItem == null?"none":comboPrinters.SelectedItem.ToString();
            Globals.cfg.ipprinter = TextBoxImpresoraIp.Text;
            Globals.cfg.iphost = textBoxHostIp.Text;

            Globals.config.Clear();
            Globals.config.Add(Globals.cfg);
            var json = JsonSerializer.Serialize<List<Config>>(Globals.config);
            File.WriteAllText(Globals.config_json, json);

            RJMessageBox.Show("Configuración actualizada correctamente", "ScanBox", MessageBoxButtons.OK, MessageBoxIcon.Information);


        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void Configuracion_FormClosing(object sender, FormClosingEventArgs e)
        {
            Globals.valid_user = false;
        }

        private void InitializeItems()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.Padding = new Padding(Globals.borderSize);//Set border size
            this.btnClose.DialogResult = DialogResult.Cancel;
            this.labelCaption.Text = this.Text;
            this.panelTitleBar.BackColor = Globals.primaryColor;
            this.panelTitleBar.Padding = new Padding(0, 0, 0, Globals.borderSize);
            this.BackColor = Globals.primaryColor;
        }

        private void InitializeButtons()
        {
            int xCenter = (this.panelButtons.Width - button1.Width) / 2;
            int yCenter = (this.panelButtons.Height - button1.Height) / 2;

            //OK Button
            button1.Visible = true;
            //button1.Location = new Point(xCenter - (button1.Width / 2) - 5, yCenter);
            button1.Text = "Guardar";
            //button1.DialogResult = DialogResult.OK;//Set DialogResult

            //Cancel Button
            button2.Visible = true;
            //button2.Location = new Point(xCenter + (button2.Width / 2) + 5, yCenter);
            button2.Text = "Salir";
            button2.DialogResult = DialogResult.Cancel;//Set DialogResult
            button2.BackColor = Globals.colors.retry_cancell;
        }

        #region -> Drag Form
        [DllImport("user32.DLL", EntryPoint = "SendMessage")]
        private extern static void SendMessage(System.IntPtr hWnd, int wMsg, int wParam, int lParam);
        [DllImport("user32.DLL", EntryPoint = "ReleaseCapture")]
        private extern static void ReleaseCapture();
        private void panelTitleBar_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, 0x112, 0xf012, 0);
        }
        #endregion
    }

}
