using qtc_net_client_2.Services;
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 CurrencyJackpotSpinner : Form
    {
        public int TokensWon { get; private set; }

        private bool AllowClose = false;
        private AudioService _audioService = new();
        public CurrencyJackpotSpinner()
        {
            InitializeComponent();
        }

        private async void TokenJackpotSpinner_Load(object sender, EventArgs e)
        {
            btnClaim.Enabled = false;
            AllowClose = false;

            _audioService.OnSoundEnded += _audioService_OnSoundEnded;
            await StartSpinAnimation(lblTokensWon);
        }

        private void TokenJackpotSpinner_FormClosed(object sender, FormClosedEventArgs e)
        {
            DialogResult = DialogResult.OK;
            _audioService.Dispose();
            Close();
        }

        private void TokenJackpotSpinner_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!AllowClose) e.Cancel = true;
        }

        private void btnClaim_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void _audioService_OnSoundEnded(object? sender, SoundEndedEventArgs e)
        {
            switch(e.EventString)
            {
                case "sndTokenWin":
                    DetermineWinAnim();
                    break;
            }
        }

        public async Task StartSpinAnimation(Label label)
        {
            if (IsHandleCreated && label.IsHandleCreated)
            {
                Random rnd = new Random();
                _audioService.PlaySoundEffect("sndTokenSpinLoop");

                while (_audioService.OutputDevice?.PlaybackState == NAudio.Wave.PlaybackState.Playing)
                {
                    if (!IsHandleCreated) return;
                    label.BeginInvoke(delegate () { label.Text = $"{rnd.Next(0, 300)} Q's Won"; });
                    await Task.Delay(10);
                }

                var win = rnd.Next(0, 300);

                if (!IsHandleCreated) return;
                label.BeginInvoke(delegate ()
                {
                    label.Text = $"{win} Q's Won";
                    btnClaim.Enabled = true;
                    _audioService.PlaySoundEffectWithEventString("sndTokenWin", "sndTokenWin");
                });

                AllowClose = true;
                TokensWon = win;
            }
        }

        public void DetermineWinAnim()
        {
            switch(TokensWon)
            {
                case > 200:
                    pbHornLeft.BeginInvoke(delegate () { pbHornLeft.Visible = true; });
                    pbHornRight.BeginInvoke(delegate () { pbHornRight.Visible = true; });
                    _audioService.PlaySoundEffect("sndTokenJackpot");
                    break;
            }
        }
    }
}
