using LevelCode.License.Business;
using LevelCode.License.DataAccess;
using LevelCode.License.Models;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Levelcode_licence.Forms
{
    public partial class FrmLicencaMaquinas : Form
    {
        private List<ModeloLicenca> _licencas;

        public FrmLicencaMaquinas()
        {
            InitializeComponent();
            CarregarLicencas();
        }
        private string MascararHwid(string hwid)
        {
            if (string.IsNullOrWhiteSpace(hwid))
                return "";

            if (hwid.Length <= 10)
                return hwid;

            string inicio = hwid.Substring(0, 6);
            string fim = hwid.Substring(hwid.Length - 4, 4);

            return $"{inicio}...{fim}";
        }


        private void CarregarLicencas()
        {
            using (var db = new DbConexao())
            {
                var licencaDal = new LicencaDAL(db);
                _licencas = licencaDal.BuscarTodas();

                cmbLicencas.DataSource = _licencas;
                cmbLicencas.DisplayMember = "Cliente";
                cmbLicencas.ValueMember = "IdLicenca";
            }
        }

        private void cmbLicencas_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbLicencas.SelectedItem == null)
                return;

            ModeloLicenca licenca = cmbLicencas.SelectedItem as ModeloLicenca;
            if (licenca == null)
                return;

            using (var db = new DbConexao())
            {
                var dal = new LicencaMaquinaDAL(db);
                var service = new LicencaMaquinaService(dal);

                dgvMaquinas.DataSource =
                    service.ListarMaquinas(licenca.IdLicenca);
                foreach (DataGridViewRow row in dgvMaquinas.Rows)
                {
                    if (row.DataBoundItem is ModeloLicencaMaquina maquina)
                    {
                        row.Cells["HWID"].Value =
                            MascararHwid(maquina.HWID);
                    }
                }
                dgvMaquinas.Columns["HWID"].HeaderText = "Identificador da Máquina";
            }
        }

        private void btnDesativar_Click(object sender, EventArgs e)
        {
            if (dgvMaquinas.CurrentRow == null)
            {
                MessageBox.Show(
                    "Selecione uma máquina.",
                    "Aviso",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Warning);
                return;
            }

            ModeloLicencaMaquina maquina =
                dgvMaquinas.CurrentRow.DataBoundItem as ModeloLicencaMaquina;

            if (maquina == null)
                return;

            DialogResult r = MessageBox.Show(
                "Deseja realmente desativar esta máquina?",
                "Confirmação",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);

            if (r != DialogResult.Yes)
                return;

            using (var db = new DbConexao())
            {
                var dal = new LicencaMaquinaDAL(db);
                var service = new LicencaMaquinaService(dal);

                service.Desativar(maquina.Id);
            }

            // Atualiza grid
            cmbLicencas_SelectedIndexChanged(null, null);

            MessageBox.Show(
                "Máquina desativada com sucesso.",
                "Sucesso",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }
    }
}
