﻿using System.Collections.Generic;
using System.IO;
using System.Windows.Input;
using Judge_Jeopardy.Helpers;
using Judge_Jeopardy.ViewModels;
using Newtonsoft.Json;

// ReSharper disable once CheckNamespace

namespace Judge_Jeopardy.Models
{
    internal partial class Game
    {
        private ICommand _correctAnswerCommand;
        private ICommand _incorrectAnswerCommand;
        private ICommand _selectPlayer1Command;
        private ICommand _selectPlayer2Command;
        private ICommand _selectPlayer3Command;
        private bool _shouldPlayMusic = true;

        static Game()
        {
            MainGame = new Game();
        }

        public static Game MainGame { get; set; }

        public ICommand SelectPlayer1Command
            => _selectPlayer1Command ?? (_selectPlayer1Command = new DelegateCommand(SelectPlayer1));

        public ICommand SelectPlayer2Command
            => _selectPlayer2Command ?? (_selectPlayer2Command = new DelegateCommand(SelectPlayer2));

        public ICommand SelectPlayer3Command
            => _selectPlayer3Command ?? (_selectPlayer3Command = new DelegateCommand(SelectPlayer3));

        public ICommand CorrectAnswerCommand
            => _correctAnswerCommand ?? (_correctAnswerCommand = new DelegateCommand(CorrectAnswer));

        public ICommand IncorrectAnswerCommand
            => _incorrectAnswerCommand ?? (_incorrectAnswerCommand = new DelegateCommand(IncorrectAnswer));

        public bool ShouldPlayMusic
        {
            get { return _shouldPlayMusic; }
            set { SetProperty(ref _shouldPlayMusic, value); }
        }

        public static void Save()
        {
            using (var writer = new StreamWriter(ControllerViewModel.ConfigFilePath))
            {
                using (var jsonWriter = new JsonTextWriter(writer))
                {
                    var serializer = new JsonSerializer();
                    serializer.Serialize(jsonWriter, MainGame);
                }
            }
        }

        public void SelectPlayer1()
        {
            if (CurrentTile.AnsweredPlayers.ContainsKey(Player1) && CurrentTile.AnsweredPlayers[Player1] &&
                !Dialog.ShowYesNo($"{Player1.Name} has already answered this tile. Select again?"))
            {
                return;
            }

            CurrentlyAnsweringPlayer = Player1;
        }

        public void SelectPlayer2()
        {
            if (CurrentTile.AnsweredPlayers.ContainsKey(Player2) && CurrentTile.AnsweredPlayers[Player2] &&
                !Dialog.ShowYesNo($"{Player2.Name} has already answered this tile. Select again?"))
            {
                return;
            }

            CurrentlyAnsweringPlayer = Player2;
        }

        public void SelectPlayer3()
        {
            if (CurrentTile.AnsweredPlayers.ContainsKey(Player3) && CurrentTile.AnsweredPlayers[Player3] &&
                !Dialog.ShowYesNo($"{Player3.Name} has already answered this tile. Select again?"))
            {
                return;
            }

            CurrentlyAnsweringPlayer = Player3;
        }

        public void CorrectAnswer()
        {
            if (!CurrentTile.AnsweredPlayers.ContainsKey(CurrentlyAnsweringPlayer))
            {
                CurrentTile.AnsweredPlayers.Add(CurrentlyAnsweringPlayer, true);
            }
            else
            {
                CurrentTile.AnsweredPlayers[CurrentlyAnsweringPlayer] = true;
            }

            CurrentlyAnsweringPlayer.Points += CurrentTile.Points;

            CurrentlyAnsweringPlayer = null;
        }

        public void IncorrectAnswer()
        {
            if (!CurrentTile.AnsweredPlayers.ContainsKey(CurrentlyAnsweringPlayer))
            {
                CurrentTile.AnsweredPlayers.Add(CurrentlyAnsweringPlayer, true);
            }
            else
            {
                CurrentTile.AnsweredPlayers[CurrentlyAnsweringPlayer] = true;
            }

            CurrentlyAnsweringPlayer.Points -= CurrentTile.Points;

            CurrentlyAnsweringPlayer = null;
        }
    }
}