using Krypton.Toolkit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Windows.Forms;

namespace qtcnet_client.Controls
{
    public partial class LoginControl : UserControl
    {
        public string Email { get; private set; } = string.Empty;
        public string Password { get; private set; } = string.Empty;
        public bool RememberMe { get; private set; } = false;

        public event EventHandler? OnSuccessfulLogin;
        public event EventHandler? OnRegisterPressed;
        public LoginControl()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            ToggleControls(false);

            bool formComplete = !string.IsNullOrWhiteSpace(txtEmail.Text) || !string.IsNullOrWhiteSpace(txtPassword.Text);
            if (formComplete)
            {
                Email = txtEmail.Text;
                Password = txtPassword.Text;
                RememberMe = cbRememberMe.Checked;

                OnSuccessfulLogin?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                KryptonMessageBox.Show("A Required Field Is Missing. Please Complete The Form.", "Oops.");
                ToggleControls(true);
            }
        }

        private void llblRegister_LinkClicked(object sender, EventArgs e)
        {
            ToggleControls(false);
            OnRegisterPressed?.Invoke(this, EventArgs.Empty);
        }

        public void ToggleControls(bool toggle)
        {
            SuspendLayout();

            txtEmail.Enabled = toggle;
            txtPassword.Enabled = toggle;
            cbRememberMe.Enabled = toggle;
            llblForgotPassword.Enabled = toggle;
            llblRegister.Enabled = toggle;
            btnLogin.Enabled = toggle;

            ResumeLayout();
        }

        public void ClearControls()
        {
            SuspendLayout();

            txtEmail.Clear();
            txtPassword.Clear();
            cbRememberMe.Checked = false;

            ResumeLayout();
        }
    }
}
