using System;
using System.Collections.Generic;
using TheGameExtreme.model.card;

namespace TheGameExtreme.model.deck
{
    public abstract class Deck
    {

        protected List<Card> deck = new List<Card>();
        protected int nbCard;

        protected Deck(int nbCard)
        {
            this.nbCard = nbCard;
        }

        public int size()
        {
            return deck.Count;
        }

        public void removeAt(int index)
        {
            deck.RemoveAt(index);
        }

        public Card getCard(int index)
        {
            return deck[index];
        }

        protected void InsertionDichotomique(List<Card> deck, int start, int end, Card card)
        {
            if (deck.Count == 0)
            {
                deck.Add(card);
                return;
            }

            int mediane = (end - start) / 2 + start;
            int comparateur = deck[mediane].Value.CompareTo(card.Value);

            if (comparateur == 0)
            {
                return;
            }

            if (mediane == start)
            {
                if (comparateur < 0)
                {
                    if (deck[end].Value.CompareTo(card.Value) < 0)
                    {
                        deck.Insert(end + 1, card);
                        return;
                    }
                    else if (deck[end].Value.CompareTo(card.Value) > 0)
                    {
                        deck.Insert(end, card);
                        return;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    deck.Insert(start, card);
                    return;
                }
            }
            else
            {
                if (comparateur < 0)
                {
                    InsertionDichotomique(deck, mediane, end, card);
                    return;
                }
                else
                {
                    InsertionDichotomique(deck, start, mediane, card);
                    return;
                }
            }
        }
    }
}
