using QtCNETAPI.Services.ApiService;
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;

namespace qtc_net_client_2.Forms
{
    public partial class ResetPassword : Form
    {
        private IApiService _apiService;
        public ResetPassword(IApiService apiService)
        {
            _apiService = apiService;
            InitializeComponent();
        }

        private async void btnSend_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(tbEmail.Text))
            {
                tbEmail.Enabled = false;
                btnSend.Enabled = false;

                var result = await _apiService.SendPasswordResetEmail(tbEmail.Text);
                if (result != null && result.Success && result.Data)
                {
                    pEmailBox.Visible = false;
                    pResetPasswordBox.Visible = true;

                    MessageBox.Show("Got It! You should receive an email shortly.\nIf you do not receive an email, check your spam.");
                }
                else
                {
                    MessageBox.Show("Sorry, This Server Doesn't Have Email Features Enabled Or Something Went Wrong.\nIf you cannot login, you may need to contact the server admin.", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Close();
                }
            }
        }

        private async void btnResetPassword_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(tbToken.Text) && !string.IsNullOrEmpty(tbNewPassword.Text) && !string.IsNullOrEmpty(tbConfirmPassword.Text))
            {
                tbToken.Enabled = false;
                tbNewPassword.Enabled = false;
                tbConfirmPassword.Enabled = false;
                btnResetPassword.Enabled = false;

                var result = await _apiService.ResetPassword(new QtCNETAPI.Dtos.User.UserPasswordResetDto { Token = tbToken.Text, Password = tbNewPassword.Text });
                if(result != null && result.Success && result.Data)
                {
                    MessageBox.Show("Your Password Has Been Reset. You may now login with the new password.", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Close();
                } else
                {
                    MessageBox.Show(result?.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Close();
                }
            }
        }
    }
}
