using Krypton.Toolkit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Formats.Cbor;
using System.Text;
using System.Windows.Forms;

namespace qtcnet_client.Controls
{
    public partial class RegisterControl : UserControl
    {
        public string Username { get; private set; } = string.Empty;
        public string Email { get; private set; } = string.Empty;
        public string Password { get; private set; } = string.Empty;
        public DateTime DateOfBirth { get; private set; } = DateTime.MinValue;

        public event EventHandler? OnReshowLoginPressed;
        public event EventHandler? OnRegister;
        public RegisterControl()
        {
            InitializeComponent();
        }

        private void llblReshowLogin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ToggleControls(false);
            OnReshowLoginPressed?.Invoke(this, EventArgs.Empty);
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            if(ValidateForm())
            {
                ToggleControls(false);

                Username = txtUsername.Text;
                Email = txtEmail.Text;
                Password = txtPassword.Text;
                DateOfBirth = dtpDateOfBirth.Value;
                OnRegister?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                KryptonMessageBox.Show("A Required Field Is Missing. Please Complete The Form.", "Oops.");
                ToggleControls(true);
            }
        }

        public void ToggleControls(bool toggle)
        {
            txtUsername.Enabled = toggle;
            txtEmail.Enabled = toggle;
            txtPassword.Enabled = toggle;
            txtConfirmEmail.Enabled = toggle;
            txtConfirmPassword.Enabled = toggle;
            cbTOSAgree.Enabled = toggle;
            dtpDateOfBirth.Enabled = toggle;
            llblReshowLogin.Enabled = toggle;
            btnRegister.Enabled = toggle;
        }

        private bool ValidateForm()
        {
            return !string.IsNullOrEmpty(txtEmail.Text) && 
                !string.IsNullOrEmpty(txtPassword.Text) && 
                (txtConfirmEmail.Text == txtEmail.Text) && 
                (txtConfirmPassword.Text == txtPassword.Text) &&
                cbTOSAgree.Checked;
        }
    }
}
