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

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

        public override String Name => "[MP] Rundy";

        public override String SystemName => "Turns";

        public override String Description => "Gracze odpowiadaja po kolei.";
        public override QuizGamemode CreateInstance(QuizContainer quiz, BaseQuizContext context) => new Turns(quiz, context);
        class TurnsStorage {
            public ConnectionHandler[] Order;
            public int CurrentPlayer = 0;
            public int Score = 0;
        }
        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);
            if (lobby.Storage == null) {
                TurnsStorage ts = new TurnsStorage();
                ts.Order = lobby.Players.ToArray();
                ts.CurrentPlayer = 0;
                lobby.Storage = ts;
            }
            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++) {
                TurnsStorage ts = lobby.Storage as TurnsStorage;
                int cp = ts.CurrentPlayer;
                if (Context.Handler != ts.Order[cp]) {
                    await Clear();
                    Context.Handler.PlayerReady = true;
                    await WriteLine("Oczekiwanie na " + ts.Order[cp].PlayerName);
                    await WaitForPlayersReady(lobby);
                } else {
                    Context.Handler.PlayerReady = false;
                    await Clear();
                    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! Zdobywacie punkt.");
                        ts.Score++;
                    } else {
                        await WriteLine("Zle! Poprawna odpowiedz to: {0}", correct);
                    }
                    await WriteLine();
                    await Pause();
                    ts.CurrentPlayer++;
                    if (ts.CurrentPlayer >= ts.Order.Length) ts.CurrentPlayer = 0;
                    Context.Handler.PlayerReady = true;
                    await Write("\r\n\r\n");
                }
            }


            await WaitForPlayersReady(lobby);
            await Clear();
            StringBuilder sb = new StringBuilder();
            await WriteLine("Wasz wynik: {0}/{1}\r\n", ((TurnsStorage)lobby.Storage).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);
        }

    }
}
