using System;
using System.Collections.Generic;
using System.Linq;

namespace Flashcard.Models {

    public class Question {

        public List<Answer> Answers { get; set; }
        public string Text { get; set; }
        
        public static Random Random = new Random();

        public Question() {
            Answers = new List<Answer>();
            for (int i = 0; i < 4; i++) {
                Answers.Add(new Answer());
            }
        
            // Generate text
            int n1 = Question.Random.Next(9) + 1;
            int n2 = Question.Random.Next(9) + 1;
            int result = n1 + n2;
            Text = string.Format("{0} + {1}", n1, n2);
            Answers[0].Initialize(result.ToString());
            Answers[0].IsCorrect = true;

            for (int i = 1; i < Answers.Count; i++) {
                int value;
                
                // generate unique value
                do {
                    value = Math.Abs(result + Question.Random.Next(5) - 5);
                } while(Answers.Take(i).Any(a => a.Text == value.ToString()));

                Answers[i].Initialize(value.ToString());
            }

            // Random order
            Answers.Sort((a, b) => a.SortIndex.CompareTo(b.SortIndex));
        }

    }
}
