using Microsoft.AspNetCore.Mvc.TagHelpers;
using qtc_net_client_2.ClientModel;
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;
        private int currentCosmetic = 0;
        public ProfileEdit(IApiService apiService)
        {
            _apiService = apiService;
            InitializeComponent();
        }

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

            // get all owned cosmetics
            var boughtItems = await _apiService.GetOwnedStoreItems();
            List<ComboBoxItem> items = new List<ComboBoxItem>();

            if(boughtItems != null && boughtItems.Success && boughtItems.Data != null)
            {
                items.Add(new ComboBoxItem
                {
                    Name = "(None)",
                    Value = 0
                });

                foreach (var item in boughtItems.Data)
                {
                    // get item from the store
                    var storeItem = await _apiService.GetStoreItem(item.StoreItemId);
                    if(storeItem != null && storeItem.Success && storeItem.Data != null)
                    {
                        var cbi = new ComboBoxItem
                        {
                            Name = storeItem.Data.Name,
                            Value = storeItem.Data.Id
                        };
                        items.Add(cbi);
                    }
                }
            } else
            {
                items.Add(new ComboBoxItem
                {
                    Name = "(None)",
                    Value = 0
                });
            }

            cbCosmetic.DataSource = items;
            cbCosmetic.SelectedIndex = cbCosmetic.Items.IndexOf(items.FirstOrDefault(e => (int?)e.Value == _apiService.CurrentUser.ActiveProfileCosmetic));
            currentCosmetic = cbCosmetic.Items.IndexOf(items.FirstOrDefault(e => (int?)e.Value == _apiService.CurrentUser.ActiveProfileCosmetic));
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            ComboBoxItem? selectedItem = (ComboBoxItem?)cbCosmetic.SelectedItem;
            int selectedItemId = 0;

            // update user info
            UserUpdateInformationDto userUpdateInformationDto = new UserUpdateInformationDto
            {
                Id = _apiService.CurrentUser.Id,
                Username = tbUsername.Text,
                Bio = rtxtBio.Text,
                DateOfBirth = _apiService.CurrentUser.DateOfBirth
            };

            if (selectedItem != null)
            {
                selectedItemId = (int?)selectedItem.Value ?? 0;
                userUpdateInformationDto.ProfileCosmeticId = selectedItemId;
            }

            var res = await _apiService.UpdateUserInformationAsync(userUpdateInformationDto);

            if (res.Success)
            {
                if (currentCosmetic != selectedItemId) DialogResult = DialogResult.Yes; // trigger ui refresh
                else DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                MessageBox.Show(res.Message, "Info Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                DialogResult = DialogResult.Cancel;
                Close();
            }
        }
    }
}
