using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TELTest.Gamemodes {
    public class ClassicMP : QuizGamemode {
        public ClassicMP() {
        }
        public ClassicMP(QuizContainer container, BaseQuizContext context) : base(container, context) {
        }

        public override String Name => "[MP] Klasyczny (multiplayer)";

        public override String SystemName => "Classic_Multiplayer";

        public override String Description => "Klasyczny quizhouse_poor, teraz w wersji multiplayer.";
        public override QuizGamemode CreateInstance(QuizContainer quiz, BaseQuizContext context) => new ClassicMP(quiz, context);
        public override async Task Begin() {
            var lobby = await CreateJoinLobbyStandard();
            if (lobby == null) return;
            await Clear();
            int score = 0;
            await WriteLine("Tytul quizu: " + Quiz.Title);
            await WriteLine("Autor quizu: " + Quiz.Author);
            await WriteLine();
            await WaitForPlayersReady(lobby);

            Context.Handler.PlayerReady = false;
            
            await Clear();
            await WriteLine("Tytul quizu: " + Quiz.Title);
            await WriteLine("Autor quizu: " + Quiz.Author);
            await WriteLine();
            for (int i = 0; i < Quiz.Questions.Length; i++) {
                await WriteLine((i + 1) + ". " + Quiz.Questions[i].Content);
                string correct = "";
                for (int j = 0; j < Quiz.Questions[i].Answers.Length; j++) {
                    var itm = Quiz.Questions[i].Answers[j];
                    await WriteLine("{0}. {1}", Utilities.NumberToLetter(j).ToLower(), itm.Content);
                    if (itm.Correct) correct += Utilities.NumberToLetter(j).ToUpper()[0];
                }
                await Write("Twoja odpowiedz: ");
                char ans = await ReadUppercaseKey();
                await Flush();
                await WriteLine();
                if (correct.Contains(ans)) {
                    await WriteLine("Dobrze! Zdobywasz punkt.");
                    score++;
                    Context.Handler.SharedTempUserObject = score;
                } else {
                    await WriteLine("Zle! Poprawna odpowiedz to: {0}", correct);
                }
                await Write("\r\n\r\n");
            }


            await WaitForPlayersReady(lobby);
            await Clear();
            StringBuilder sb = new StringBuilder();
            await WriteLine("Twoj wynik: {0}/{1}\r\n", score, Quiz.Questions.Length);
            foreach (var item in lobby.Players) {
                if (item == Context.Handler) continue;
                sb.AppendFormat("Wynik {0}: {1}/{2}\r\n", item.PlayerName, item.SharedTempUserObject, Quiz.Questions.Length);
            }
            await WriteLine(sb.ToString());
            await Pause();

            await DoScoreSubmission(score, true);
            await EndMP(lobby);
        }

    }
}
