using LevelCode.License.Business;
using LevelCode.License.DataAccess;
using Levelcode_licence.Business;
using Levelcode_licence.DataAccess;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace LevelCode.License.Forms
{
    public partial class FrmDashboard : Form
    {
        public FrmDashboard()
        {
            InitializeComponent();
            CarregarDashboard();
        }

        private void CarregarDashboard()
        {
            using (var db = new DbConexao())
            {
                var service = new DashboardService(new DashboardDAL(db));

                // Cards
                flpCards.Controls.Clear();
                flpCards.Controls.Add(CriarCard("📄 Total Licenças", service.TotalLicencas(), Color.FromArgb(52, 152, 219)));
                flpCards.Controls.Add(CriarCard("✅ Ativas", service.LicencasAtivas(), Color.FromArgb(46, 204, 113)));
                flpCards.Controls.Add(CriarCard("❌ Inativas", service.LicencasInativas(), Color.FromArgb(231, 76, 60)));
                flpCards.Controls.Add(CriarCard("🖥 Máquinas Ativas", service.MaquinasAtivas(), Color.FromArgb(155, 89, 182)));
                flpCards.Controls.Add(CriarCard("⚠ No Limite", service.LicencasNoLimite(), Color.FromArgb(241, 196, 15)));

                CriarGraficoLicencas(service.LicencasAtivas(), service.LicencasInativas());
                CriarGraficoMaquinas(service);
            }
        }

        // =========================
        // CARD
        // =========================
        private Panel CriarCard(string titulo, int valor, Color cor)
        {
            Panel card = new Panel
            {
                Width = 160,
                Height = 90,
                BackColor = cor,
                Margin = new Padding(10)
            };

            Label lblTitulo = new Label
            {
                Text = titulo,
                ForeColor = Color.White,
                Font = new Font("Segoe UI", 9),
                Location = new Point(10, 10),
                AutoSize = true
            };

            Label lblValor = new Label
            {
                Text = valor.ToString(),
                ForeColor = Color.White,
                Font = new Font("Segoe UI", 22, FontStyle.Bold),
                Location = new Point(10, 35),
                AutoSize = true
            };

            card.Controls.Add(lblTitulo);
            card.Controls.Add(lblValor);

            return card;
        }

        // =========================
        // GRÁFICO PIZZA
        // =========================
        private void CriarGraficoLicencas(int ativas, int inativas)
        {
            chartLicencas.Series.Clear();
            chartLicencas.ChartAreas.Clear();
            chartLicencas.Legends.Clear();

            chartLicencas.ChartAreas.Add(new ChartArea());

            Series serie = new Series
            {
                ChartType = SeriesChartType.Pie,
                IsValueShownAsLabel = true,
                Font = new Font("Segoe UI", 9, FontStyle.Bold)
            };

            serie.Points.AddXY("Ativas", ativas);
            serie.Points.AddXY("Inativas", inativas);

            chartLicencas.Series.Add(serie);
            chartLicencas.Legends.Add(new Legend());
        }

        // =========================
        // GRÁFICO BARRAS
        // =========================
        private void CriarGraficoMaquinas(DashboardService service)
        {
            chartMaquinas.Series.Clear();
            chartMaquinas.ChartAreas.Clear();

            ChartArea area = new ChartArea();
            area.AxisX.Interval = 1;
            area.AxisX.LabelStyle.Angle = -30;

            chartMaquinas.ChartAreas.Add(area);

            Series serie = new Series
            {
                ChartType = SeriesChartType.Column,
                IsValueShownAsLabel = true,
                Font = new Font("Segoe UI", 8)
            };

            // 🔧 Exemplo simples (pode evoluir depois)
            serie.Points.AddXY("Cliente A", 2);
            serie.Points.AddXY("Cliente B", 1);
            serie.Points.AddXY("Cliente C", 3);

            chartMaquinas.Series.Add(serie);
        }
    }
}
