using QtCNETAPI.Dtos.User;
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 ProfileEdit : Form
    {
        IApiService _apiService;
        public ProfileEdit(IApiService apiService)
        {
            _apiService = apiService;
            InitializeComponent();
        }

        private void frmProfileEdit_Load(object sender, EventArgs e)
        {
            tbUsername.Text = _apiService.CurrentUser!.Username;
            rtxtBio.Text = _apiService.CurrentUser!.Bio;
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(tbUsername.Text) && (tbUsername.Text != _apiService.CurrentUser!.Username || rtxtBio.Text != _apiService.CurrentUser!.Bio))
            {
                // update user info
                UserUpdateInformationDto userUpdateInformationDto = new UserUpdateInformationDto
                {
                    Id = _apiService.CurrentUser.Id,
                    Username = tbUsername.Text,
                    Bio = rtxtBio.Text,
                    DateOfBirth = _apiService.CurrentUser.DateOfBirth
                };

                var res = await _apiService.UpdateUserInformationAsync(userUpdateInformationDto);

                if (res.Success)
                {
                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    MessageBox.Show(res.Message, "Info Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = DialogResult.Cancel;
                    Close();
                }
            }
        }
    }
}
