using QtCNETAPI.Dtos.User;
using QtCNETAPI.Schema;
using QtCNETAPI.Services;
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 StoreItemDisplay : Form
    {
        private StoreItem StoreItem { get; set; }

        private LoggingService _loggingService;
        private IApiService _apiService;
        public StoreItemDisplay(StoreItem item, LoggingService loggingService, IApiService apiService)
        {
            StoreItem = item;
            _loggingService = loggingService;
            _apiService = apiService;
            InitializeComponent();
        }

        private async void StoreItemDisplay_Load(object sender, EventArgs e)
        {
            lblName.Text = StoreItem.Name;
            lblDescription.Text = StoreItem.Description;
            lblPrice.Text = StoreItem.Price.ToString("N0");

            // does the user already own this item?
            var ownedItem = await _apiService.GetOwnedStoreItem(StoreItem.Id);
            if (ownedItem != null && ownedItem.Success && ownedItem.Data != null)
            {
                btnBuy.Enabled = false;
                btnBuy.Text = "Bought";
            }

            try
            {
                using HttpClient client = new();
                var response = await client.GetAsync(StoreItem.ThumbnailUrl);
                if (response != null && response.IsSuccessStatusCode)
                {
                    using var stream = await response.Content.ReadAsStreamAsync();
                    Image image = Image.FromStream(stream);
                    stream.Dispose();
                    pbItemThumbnail.Image = image;
                }
                else if (response != null) _loggingService.LogString($"Store Item Thumbnail Could Not Be Loaded Due To Status Code {response.StatusCode}");
                else _loggingService.LogString("Store Item Thumbnail Could Not Be Loaded");
                client.Dispose();
            }
            catch (Exception ex)
            {
                _loggingService.LogString("Store Item Thumbnail Could Not Be Loaded\n" + ex.Message);
            }
        }

        private async void btnBuy_Click(object sender, EventArgs e)
        {
            Enabled = false;

            // attempt to buy item
            var ownedStoreItem = await _apiService.BuyStoreItem(StoreItem.Id);
            if (ownedStoreItem != null && ownedStoreItem.Success)
            {
                Enabled = true;

                var result = MessageBox.Show($"Successfully Bought '{StoreItem.Name}'! Would You Like To Wear It Now?", "Success!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    // send an update dto that only updates the current users cosmetic
                    UserUpdateInformationDto updateDto = new UserUpdateInformationDto
                    {
                        Id = _apiService.CurrentUser.Id,
                        Bio = _apiService.CurrentUser.Bio,
                        DateOfBirth = _apiService.CurrentUser.DateOfBirth,
                        Username = _apiService.CurrentUser.Username,
                        ProfileCosmeticId = StoreItem.Id
                    };

                    await _apiService.UpdateUserInformationAsync(updateDto);
                    DialogResult = DialogResult.Yes;
                }
            }
            else MessageBox.Show("We Weren't Able To Complete Your Purchase.\nYou May Not Have Enough Funds For This Item.", "Oops.", MessageBoxButtons.OK, MessageBoxIcon.Error);

            DialogResult = DialogResult.OK;
            Close();
        }
    }
}
