using Quasar.Server.Forms.DarkMode;
using Quasar.Server.Models;
using Quasar.Server.Licensing;
using Quasar.Server.Forms.DarkMode;
using Quasar.Server.Models;
using Quasar.Server.Utilities;
using System;
using System.IO;
using System.Windows.Forms;

namespace Quasar.Server.Forms
{
    public partial class FrmLicense : Form
    {
        private string _hardwareID;
        private const string LICENSE_FILE = "license.dat";

        public FrmLicense()
        {
            InitializeComponent();
            _hardwareID = LicenseManager.GenerateHardwareID();
        }

        private void FrmLicense_Load(object sender, EventArgs e)
        {
            if (Settings.DarkMode) { DarkModeManager.ApplyDarkMode(this); }
            ThemeManager.ApplyThemeToNewForm(this);
            txtHardwareID.Text = _hardwareID;
            txtHardwareID.ReadOnly = true;
            txtBIN.Focus();
        }

        private void btnActivate_Click(object sender, EventArgs e)
        {
            string enteredBIN = txtBIN.Text.Trim();

            if (string.IsNullOrEmpty(enteredBIN))
            {
                MessageBox.Show("Please enter a BIN code.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (LicenseManager.ValidateBIN(_hardwareID, enteredBIN))
            {
                try
                {
                    File.WriteAllText(LICENSE_FILE, $"{_hardwareID}|{enteredBIN}");
                    MessageBox.Show("License activated successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Failed to save license: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Invalid BIN code. Please check and try again.", "Authentication Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtBIN.Clear();
                txtBIN.Focus();
            }
        }

        private void txtBIN_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true;
                btnActivate_Click(sender, e);
            }
        }

        public static bool CheckLicense()
        {
            if (!File.Exists(LICENSE_FILE))
                return false;

            try
            {
                string[] parts = File.ReadAllText(LICENSE_FILE).Split('|');
                if (parts.Length != 2)
                    return false;

                string storedHardwareID = parts[0];
                string storedBIN = parts[1];

                return LicenseManager.ValidateBIN(storedHardwareID, storedBIN);
            }
            catch
            {
                return false;
            }
        }
    }
}
