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

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

        public override String Name => "Pisania";

        public override String SystemName => "Pisania";

        public override String Description => "Tryb pisania wymaga od cieie napisania pelnej odpowiedzi zamiast litery.";
        public override QuizGamemode CreateInstance(QuizContainer quiz, BaseQuizContext context) => new Pisania(quiz, context);
        public override async Task Begin() {
            int score = 0;
            await WriteLine("Tytul quizu: " + Quiz.Title);
            await WriteLine("Autor quizu: " + Quiz.Author);
            await Write("Rozpocznij... "); 
            await ReadKey();
            await Flush();
            for (int i = 0; i < Quiz.Questions.Length; i++) {
                await Clear();
                await WriteLine((i + 1) + ". " + Quiz.Questions[i].Content);

                await Write("Twoja odpowiedz: ");
                string ans = await ReadLine();
                await Flush();
                await WriteLine();
                ans = ans.Trim().ToUpper();
                bool wasCorrect = false;
                List<string> correct = new List<string>();
                for (int j = 0; j < Quiz.Questions[i].Answers.Length; j++) {
                    var item = Quiz.Questions[i].Answers[j];
                    if (item.Correct) correct.Add(item.Content);
                    if (item.Content.Trim().ToUpper() == ans && item.Correct) { wasCorrect = true; break; }
                }
                if (!wasCorrect && correct.Count == 0 && string.IsNullOrWhiteSpace(ans)) {
                    wasCorrect = true;
                }
                if (wasCorrect) {
                        if (correct.Count == 0)
                            await WriteLine("Dobrze! Nie bylo poprawnej odpowiedzi.");
                        else
                            await WriteLine("Dobrze! Zdobywasz punkt.");
                    score++;
                } else {
                    if (correct.Count == 1) {
                        await WriteLine("Zle! Poprawna odpowiedz to: {0}", correct[0]);
                    } else if (correct.Count == 0) {
                        await WriteLine("Zle! Nie ma poprawnej odpowiedzi.");
                    } else {
                        await WriteLine("Zle! Poprawne odpowiedzi to:\r\n - {0}", string.Join("\r\n - ", correct));
                    }
                }
                await Write("\r\n\r\n");
                await Pause();
            }
            await WriteLine("Twoj wynik: {0}", score, Quiz.Questions.Length);
            await Pause();

            await DoScoreSubmission(score);
        }

    }
}
