using QtCNETAPI.Services.ApiService;
using QtCNETAPI.Dtos.User;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using qtc_net_client_2.Services;

namespace qtc_net_client_2.Forms
{
    public partial class llblForgotPassword : Form
    {
        private IApiService _apiService;
        private CredentialService _credService = new();
        public llblForgotPassword(IApiService apiService)
        {
            _apiService = apiService;

            InitializeComponent();
        }

        private async void frmLogin_Load(object sender, EventArgs e)
        {
            string? accessToken = _credService.GetAccessToken();

            if (accessToken != null)
            {
                ToggleControls(false, false);

                // try logging in with the token in cred storage
                var result = await _apiService.RefreshLogin(accessToken);
                if (result.Success)
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                    ToggleControls(true, false);
            }
        }

        private async void btnLogin_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbEmail.Text) && !string.IsNullOrEmpty(tbPassword.Text))
            {
                ToggleControls(false, false);

                var result = await _apiService.LoginAsync(new UserLoginDto
                {
                    Email = tbEmail.Text,
                    Password = tbPassword.Text,
                    RememberMe = cbRememberMe.Checked
                });

                if (result.Success && result.Data != null)
                {
                    _credService.SaveAccessToken(_apiService.CurrentUser.Username, result.Data);
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    ToggleControls(true, true);
                    MessageBox.Show(result.Message, "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void llblRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Register frmRegister = new Register(_apiService);
            frmRegister.ShowDialog();
        }

        private void llblResendEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ResendVerificationEmail resendVerificationEmail = new ResendVerificationEmail(_apiService);
            resendVerificationEmail.ShowDialog();
        }

        private void llblForgotPasswor_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ResetPassword resetPassword = new ResetPassword(_apiService);
            resetPassword.ShowDialog();
        }

        private void ToggleControls(bool enable, bool clearText)
        {
            tbEmail.Enabled = enable;
            tbPassword.Enabled = enable;
            btnLogin.Enabled = enable;
            llblRegister.Enabled = enable;

            if (clearText)
            {
                tbEmail.Text = string.Empty;
                tbPassword.Text = string.Empty;
            }
        }
    }
}
