using System;
using System.Collections.Generic;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Millionenquiz
{
    class Question
    {
        private string question;
        private string option1;
        private string option2;
        private string option3;
        private string option4;
        private int solution;

        public Question(System.String f, System.String o1, System.String o2, System.String o3, System.String o4, int l)
        {
            question = f;
            option1 = o1;
            option2 = o2;
            option3 = o3;
            option4 = o4;
            solution = l;
        }

        public virtual void Display(MainPage page)
        {
            page.question.Text = question;
            page.answer1.Content = option1;
            page.answer2.Content = option2;
            page.answer3.Content = option3;
            page.answer4.Content = option4;
        }

        public virtual bool IsCorrect(int chosen)
        {
            if (chosen == this.solution)
                return true;
            else
                return false;
        }
    }

    class GameLogic
    {
        internal const int NUM_QUESTIONS = 8;
        internal const int CONTROLS_BIAS = 7; 

        internal Question[] questions;
        internal sbyte currQuestion = 0;
        internal int winlevel = 0;

        internal MainPage page;

        internal GameLogic(MainPage page)
        {
            this.page = page;

            // Fragen erzeugen
            questions = new Question[NUM_QUESTIONS];

            questions[0] = new Question("Ist C# ...", "desorientiert", "objektorientiert", "blamiert", "pervertiert", 2);
            questions[1] = new Question("Welche Sprache kann als Ahn von C# angesehen werden?", "Pascal", "Java", "Simula", "Smalltalk", 4);
            questions[2] = new Question("Mit welchem Schlüsselwort werden in C# Objekte erzeugt?", "new", "create", "erzeuge", "old", 1);
            questions[3] = new Question("Mit welchem Schlüsselwort werden in C# konstante Variablen deklariert?", "konstant", "final", "const", "immutable", 3);
            questions[4] = new Question("Wie lautet der Wert folgenden Ausdrucks: \n2d + 3 / 4?", "2", "2.75", "3", "1.25", 1);
            questions[5] = new Question("Nach welchem Algorithmus sortiert Array.Sort() Objekte?", "Bubblesort", "Shellsort", "Mergesort", "Quicksort", 4);
            questions[6] = new Question("Seit welcher Version gibt es C#-Generika?", "1.0", "1.1", "2.0", "3.0", 3);
            questions[7] = new Question("Seit wann gibt es die Firma Microsoft?", "1843", "1945", "1975", "1985", 3);
        }

        internal async virtual void  Evaluate(int schalter)
        {
            TextBlock tmp;

            if (!questions[currQuestion].IsCorrect(schalter))
            {
                // falsch beantwortet
                if (winlevel == 0)
                {
                    var messageDialog = new MessageDialog("Leider nichts gewonnen.  :-(", "Schade!.");

                    messageDialog.Commands.Add(new UICommand("Schließen",
                                                new UICommandInvokedHandler(page.neuStartenHandler)));

                    await messageDialog.ShowAsync();
                }
                else
                {
                    var messageDialog = new MessageDialog("Sie haben winlevel " + winlevel + " erreicht!  :-)", "Glückwunsch!!!");

                    messageDialog.Commands.Add(new UICommand("Schließen",
                                                new UICommandInvokedHandler(page.neuStartenHandler)));

                    await messageDialog.ShowAsync();
                }
            }
            else if (currQuestion < NUM_QUESTIONS - 1)
            {           
                questions[++currQuestion].Display(page);

                winlevel++;

                Object obj = page.FindName("level" + winlevel);
                if (obj != null && obj.GetType() == typeof(TextBlock))
                {
                    tmp = (TextBlock)obj;
                    tmp.Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(100, 0, 255, 255));
                }
            }
            else
            {
                winlevel++;

                Object obj = page.FindName("level" + winlevel);
                if (obj != null && obj.GetType() == typeof(TextBlock))
                {
                    tmp = (TextBlock)obj;
                    tmp.Foreground = new SolidColorBrush(Windows.UI.Color.FromArgb(100, 0, 255, 255));
                }

                var messageDialog = new MessageDialog("Glückwunsch, Sie haben den Jackpot geknackt!!!", "Wow!!!");

                messageDialog.Commands.Add(new UICommand("Schließen",
                                            new UICommandInvokedHandler(page.neuStartenHandler)));

                await messageDialog.ShowAsync();
            }
        }



    }
}
