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

namespace TELTest.Gamemodes {
    public class Kbmio : QuizGamemode {
        public Kbmio() {
        }
        public Kbmio(QuizContainer container, BaseQuizContext context) : base(container, context) {
        }
        public override QuizGamemode CreateInstance(QuizContainer quiz, BaseQuizContext context) => new Kbmio(quiz, context);

        public override String Name => "Kbmio";

        public override String SystemName => "Kbmio";

        public override String Description => "Tryb kbmio za kazda serie poprawnych odpowiedzi dodaje 2 razy wiecej punktow po kazdym pytaniu, ale zabiera po 2 razy wiecej punktow za niepoprawne odpowiedzi, t.j. 2, 4, 8, 16, itd.";

        public override async Task Begin() {
            int score = 0;
            await WriteLine("Tytul quizu: " + Quiz.Title);
            await WriteLine("Autor quizu: " + Quiz.Author);
            await WriteLine("Rozpocznij...");
            await ReadKey();
            await Flush();
            await Clear();
            int streak = 1;
            int wrong = 1;
            for (int i = 0; i < Quiz.Questions.Length; i++) {
                await Clear();
                await WriteLine(score + " " + Utilities.OdmianaPL(score) +
                    " [ +" + streak + " / -" + wrong + " ]\r\n"
                    );
                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 " + (streak != 1 ? streak + " " : "") + Utilities.OdmianaPL(streak));
                    score+= streak;
                    streak <<= 1;
                } else {
                    await WriteLine("Zle! Poprawna odpowiedz to: {0}. Tracisz {1}", correct, (wrong == 1? "": wrong + " ") + Utilities.OdmianaPL(wrong));
                    wrong <<= 1;
                    score -= wrong;
                    streak = 1;
                }
                await Write("\r\n\r\n");
                await Pause();
            }
            await WriteLine("Twoj wynik: {0}/{1}", score, Quiz.Questions.Length);
            await Pause();

            await DoScoreSubmission(score);
        }

    }
}
