using qtcnet_client.Properties;
using QtCNETAPI.Services.ApiService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace qtcnet_client.Forms
{
    public partial class StoreItemForm : Form
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int ItemId { get; set; } = 0;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string ItemName { get; set; } = "Store Item";
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int Price { get; set; } = 0;
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public Image ItemThumbnail { get; set; } = Resources.QtCNETIcon;

        public event EventHandler? OnBuyClicked;

        private IApiService _apiService;
        public StoreItemForm(IApiService apiService)
        {
            _apiService = apiService;
            InitializeComponent();
        }

        private async void StoreItemForm_Load(object sender, EventArgs e)
        {
            lblName.Text = ItemName;
            lblPrice.Text = Price.ToString();
            pbThumbnail.Image = ItemThumbnail;

            // check if user has item
            var _res = await _apiService.GetOwnedStoreItem(ItemId);
            if(_res.Success)
            {
                btnBuy.Enabled = false;
                btnBuy.Text = "Bought";
            }
        }

        private void btnBuy_Click(object sender, EventArgs e) => OnBuyClicked?.Invoke(this, EventArgs.Empty);

        private void StoreItemForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            DialogResult = DialogResult.OK;
        }
    }
}
