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 qtcnet_client.Forms.Games
{
    public partial class StockMarketGame : Form
    {
        private IApiService _apiService;
        public StockMarketGame(IApiService apiService)
        {
            _apiService = apiService;
            InitializeComponent();
        }

        private async void StockMarketGame_Load(object sender, EventArgs e) => await InitializeStockMarketGame();
        private async void btnRefresh_Click(object sender, EventArgs e) => await InitializeStockMarketGame();

        private async void btnBuy_Click(object sender, EventArgs e)
        {
            if (nudStockBuySellAmount.Value > 0)
            {
                nudStockBuySellAmount.Enabled = false;
                btnBuy.Enabled = false;
                btnSell.Enabled = false;
                btnRefresh.Enabled = false;

                // buy stock
                var amountToBuy = int.Parse(nudStockBuySellAmount.Value.ToString());
                var result = await _apiService.BuyStock(amountToBuy);

                if (result != null && result.Success && result.Data != null)
                {
                    lblStockCount.Text = $"Your Stock Count - {result.Data.StockAmount}";

                    _apiService.CurrentUser?.StockAmount = result.Data.StockAmount;
                    _apiService.CurrentUser?.CurrencyAmount = result.Data.CurrencyAmount;

                    nudStockBuySellAmount.Enabled = true;
                    nudStockBuySellAmount.Value = 0;
                    btnBuy.Enabled = true;
                    btnSell.Enabled = true;
                    btnRefresh.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Buy Failed. Either You Don't Have Enough Q's Or A Server Side Error Occured.", "Oops.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    nudStockBuySellAmount.Enabled = true;
                    nudStockBuySellAmount.Value = 0;
                    btnBuy.Enabled = true;
                    btnSell.Enabled = true;
                    btnRefresh.Enabled = true;
                }
            }
        }

        private async void btnSell_Click(object sender, EventArgs e)
        {
            if(nudStockBuySellAmount.Value > 0)
            {
                nudStockBuySellAmount.Enabled = false;
                btnBuy.Enabled = false;
                btnSell.Enabled = false;
                btnRefresh.Enabled = false;

                // sell stock
                var amountToSell = int.Parse(nudStockBuySellAmount.Value.ToString());
                var result = await _apiService.SellStock(amountToSell);

                if (result != null && result.Success && result.Data != null)
                {
                    lblStockCount.Text = $"Your Stock Count - {result.Data.StockAmount}";

                    _apiService.CurrentUser?.StockAmount = result.Data.StockAmount;
                    _apiService.CurrentUser?.CurrencyAmount = result.Data.CurrencyAmount;

                    nudStockBuySellAmount.Enabled = true;
                    nudStockBuySellAmount.Value = 0;
                    btnBuy.Enabled = true;
                    btnSell.Enabled = true;
                    btnRefresh.Enabled = true;
                } else
                {
                    MessageBox.Show("Sell Failed. Either You Don't Have Enough Stock Or A Server Side Error Occured.", "Oops.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    nudStockBuySellAmount.Enabled = true;
                    nudStockBuySellAmount.Value = 0;
                    btnBuy.Enabled = true;
                    btnSell.Enabled = true;
                    btnRefresh.Enabled = true;
                }
            }
        }

        private async Task InitializeStockMarketGame()
        {
            var currentStockPriceResult = await _apiService.GetCurrentStockPrice();

            if (currentStockPriceResult != null && currentStockPriceResult.Success)
            {
                lblCurrentStockPrice.Text = $"Current Price Per Stock Is {currentStockPriceResult.Data} Q's";
                lblStockCount.Text = $"Your Stock Count - {_apiService.CurrentUser?.StockAmount}";

                switch (currentStockPriceResult.Data)
                {
                    case < 49:
                        lblMarketStatus.Text = "not good cheif :(";
                        lblMarketStatus.ForeColor = Color.Red;
                        break;
                    case < 99:
                        lblMarketStatus.Text = "its aight :/";
                        lblMarketStatus.ForeColor = Color.DarkGreen;
                        break;
                    case < 149:
                        lblMarketStatus.Text = "its good :)";
                        lblMarketStatus.ForeColor = Color.Green;
                        break;
                    case < 199:
                        lblMarketStatus.Text = "very good :D";
                        lblMarketStatus.ForeColor = Color.Blue;
                        break;
                    case > 200:
                        lblMarketStatus.Text = "$ AWESOME $";
                        lblMarketStatus.ForeColor = Color.LightGreen;
                        break;
                    default:
                        lblMarketStatus.Text = "Cannot Determine";
                        lblMarketStatus.ForeColor = Color.White;
                        break;
                }

                nudStockBuySellAmount.Enabled = true;
                btnBuy.Enabled = true;
                btnSell.Enabled = true;
                btnRefresh.Enabled = true;
            }
            else
            {
                MessageBox.Show("The Stock Market Endpoint Could Not Be Reached. Please Check Your Internet Connection", "Uh Oh.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }
    }
}
