using qtcnet_client.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.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace qtcnet_client.Forms.Games
{
    public partial class GuessTheNumber : Form
    {
        private IApiService _apiService;
        private AudioService _audioService;
        private int _number;
        private int _guesses;
        public GuessTheNumber(IApiService apiService, AudioService audioService)
        {
            _apiService = apiService;
            _audioService = audioService;
            InitializeComponent();
        }

        private async void GuessTheNumber_Load(object sender, EventArgs e) => await InitializeGTNGame();

        private async void btnGuess_Click(object sender, EventArgs e)
        {
            if(nudNumberGuess.Value > 0)
            {
                btnGuess.Enabled = false;

                int guess = int.Parse(nudNumberGuess.Value.ToString());
                var result = await _apiService.GuessRandomNumber(_number, guess);

                if(result != null && result.Success)
                {
                    switch(result.Data)
                    {
                        case QtCNETAPI.Enums.NumberGuessResult.Higher:
                            lblResult.Text = "Your Guess Was Higher By 10! You've Earned A Jackpot Spin!";
                            btnClaimPrize.Text = "Spin";
                            btnClaimPrize.Click += BtnClaimPrize_Click_JackpotSpin;

                            lblResult.Visible = true;
                            btnClaimPrize.Visible = true;
                            break;
                        case QtCNETAPI.Enums.NumberGuessResult.Lower:
                            lblResult.Text = "Your Guess Was Lower By 10! You've Earned 200 Q's!";
                            btnClaimPrize.Text = "Claim";
                            btnClaimPrize.Click += BtnClaimPrize_Click_QClaim;

                            lblResult.Visible = true;
                            btnClaimPrize.Visible = true;
                            break;
                        case QtCNETAPI.Enums.NumberGuessResult.Correct:
                            lblResult.Text = "Your Correct! You've Earned The Jackpot!";
                            btnClaimPrize.Text = "Claim";
                            btnClaimPrize.Click += BtnClaimPrize_Click_QClaimJackpot;

                            lblResult.Visible = true;
                            btnClaimPrize.Visible = true;
                            break;
                        case QtCNETAPI.Enums.NumberGuessResult.Incorrect:
                            if (_guesses >= 2)
                            {
                                lblResult.Text = "Unfortunately you've used up all your guesses for this round.";
                                lblResult.Visible = true;
                                btnGuess.Visible = false;
                                break;
                            }
                            else _guesses++;

                            lblResult.Text = $"Incorrect. You Get Three Chances. You Guessed {_guesses} Time(s)";
                            lblResult.Visible = true;
                            btnGuess.Enabled = true;
                            break;
                    }
                }
            }
        }

        private void BtnClaimPrize_Click_QClaimJackpot(object? sender, EventArgs e)
        {
            throw new NotImplementedException("need to figure out how to do this, sorry :)");
        }

        private async void BtnClaimPrize_Click_QClaim(object? sender, EventArgs e)
        {
            var result = await _apiService.AddCurrencyToCurrentUser(200, false);
            if (result != null && result.Success) Close();
            else
            {
                MessageBox.Show("We Weren't Able To Claim Your Prize. You Can Try Again.", "Uh Oh...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private async void BtnClaimPrize_Click_JackpotSpin(object? sender, EventArgs e)
        {
            Random rnd = new();
            JackpotSpinForm tokenJackpotSpinner = new(_audioService)
            {
                CurrencyWon = rnd.Next(0, 200)
            };
            var formResult = tokenJackpotSpinner.ShowDialog();
            if (formResult == DialogResult.OK)
            {
                var result = await _apiService.AddCurrencyToCurrentUser(tokenJackpotSpinner.CurrencyWon, false);
                if (result != null && result.Success) Close();
                else
                {
                    MessageBox.Show("We Weren't Able To Claim Your Prize. You Can Try Again.", "Uh Oh...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private async Task InitializeGTNGame()
        {
            var result = await _apiService.GetRandomNumber();

            if (result != null && result.Success)
            {
                _number = result.Data;
                lblHeader.Text = "I've picked a number between 1 and 500.\nWhat Is It?";
            }
        }
    }
}
