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 Register : Form
    {
        IApiService _apiService;
        public Register(IApiService apiService)
        {
            _apiService = apiService;
            InitializeComponent();
        }

        private async void btnRegister_Click(object sender, EventArgs e)
        {
            if( !string.IsNullOrEmpty(tbUsername.Text) &&
                !string.IsNullOrEmpty(tbEmail.Text) &&
                !string.IsNullOrEmpty(tbPassword.Text) &&
                !string.IsNullOrEmpty(tbConEmail.Text) &&
                !string.IsNullOrEmpty(tbConPassword.Text) &&
                tbConEmail.Text == tbEmail.Text &&
                tbConPassword.Text == tbPassword.Text &&
                cbAgreement.Checked)
            {
                DisableControls();

                var registerResult = await _apiService.RegisterAsync(new QtCNETAPI.Dtos.User.UserDto
                {
                    Username = tbUsername.Text,
                    Email = tbEmail.Text,
                    Password = tbPassword.Text,
                    DateOfBirth = dtpDateOfBirth.Value
                });

                if(registerResult.Success)
                {
                    MessageBox.Show("Registration Complete. If the server has email verification on, you may need to check your email for a verification link.\nIf you do not receive one, try logging in.");
                    DialogResult = DialogResult.OK;
                    Close();
                } else
                {
                    MessageBox.Show(registerResult.Message, "Registration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = DialogResult.Cancel;
                    Close();
                }
            }
        }

        private void DisableControls()
        {
            tbUsername.Enabled = false;
            tbEmail.Enabled = false;
            tbConEmail.Enabled = false;
            tbPassword.Enabled = false;
            tbConPassword.Enabled = false;
            dtpDateOfBirth.Enabled = false;
            cbAgreement.Enabled = false;
        }
    }
}
