using System;
using System.Collections.Generic;
using TheGameExtreme.IO;
using TheGameExtreme.Resx;
using Xamarin.Forms;

namespace TheGameExtreme.view
{
    public partial class GamePreparationPage : ContentPage
    {
        public List<int> listNbPlayer = new List<int> { 1, 2, 3, 4, 5 };
        public List<string> listGameMode = new List<string> { AppResources.TypeValueWhole, AppResources.TypeValueRelative, AppResources.TypeValueTens, AppResources.TypeValuehundredth, AppResources.TypeValuethousandth, AppResources.TypeValuefractionated };
        public List<int> listNbStack = new List<int> { 4, 6, 8 };
        public List<int> listNbCard = new List<int> { 100, 80, 60, 40 };
 

        public GamePreparationPage()
        {
            InitializeComponent();
            NavigationPage.SetHasNavigationBar(this, false);

            PlayerSelecter.ItemsSource = listNbPlayer;
            LoadParameterNbPlayerGamePreparation();

            SelectMode.ItemsSource = listGameMode;
            LoadParameterGameModeValueGamePreparation();

            SelectNbStack.ItemsSource = listNbStack;
            LoadParameterNbStacksGamePreparation();

            SelectNbCard.ItemsSource = listNbCard;
            LoadParameterNbCardsGamePreparation();



        }

        private async void Back(object sender, EventArgs e)
        {
            IOGamePreparation.SaveParamaterGamePreparationNbPlayers(PlayerSelecter.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbStacks(SelectNbStack.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbCards(SelectNbCard.SelectedIndex);
            await Navigation.PopAsync();
        }

        protected override bool OnBackButtonPressed()
        {
            IOGamePreparation.SaveParamaterGamePreparationNbPlayers(PlayerSelecter.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbStacks(SelectNbStack.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbCards(SelectNbCard.SelectedIndex);
            return base.OnBackButtonPressed();
        }

        private async void Play(object sender, EventArgs args)
        {
            List<string> playersNames = new List<string>();
            

            for (int i = 1; i < NameList.Children.Count; i++)
            {
                playersNames.Add(((Entry)NameList.Children[i]).Text);
                if (string.IsNullOrWhiteSpace(playersNames[playersNames.Count - 1]))
                {
                    await DisplayAlert(AppResources.WrongPseudo, AppResources.StrEnterPseudo, AppResources.StrCloseWind);
                    return;
                }
            }
            IOGamePreparation.SaveParamaterGamePreparationNbPlayers(PlayerSelecter.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbStacks(SelectNbStack.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbCards(SelectNbCard.SelectedIndex);
            await Navigation.PushAsync(new MainPage(playersNames, (int)SelectNbStack.SelectedItem, (int)SelectMode.SelectedIndex, (int)SelectNbCard.SelectedItem));
        }

        private void ChangedPseudo(object sender, EventArgs args)
        {
            while (NameList.Children.Count - 1 != (int)PlayerSelecter.SelectedItem)
            {
                if (NameList.Children.Count - 1 < (int)PlayerSelecter.SelectedItem)
                {

                    Entry e = new Entry
                    {
                        Placeholder = "Pseudo",
                        BackgroundColor = (Color)Application.Current.Resources["Gold"],
                        WidthRequest = 200,
                        MinimumWidthRequest = 50,
                        HorizontalOptions = LayoutOptions.Center,
                        MaxLength = 18,
                        IsTextPredictionEnabled = false
                    };
                    

                    NameList.Children.Add(e);
                }
                else
                {
                    NameList.Children.RemoveAt(NameList.Children.Count - 1);
                }
            }
        }
        public void LoadParameterNbPlayerGamePreparation()
        {
            int nbJoueurs = IOGamePreparation.LoadParameterGamePreparationNbPlayers();
            PlayerSelecter.SelectedIndex = nbJoueurs;
        }

        public void LoadParameterGameModeValueGamePreparation()
        {
            int gameModeValue = IOGamePreparation.LoadParameterGamePreparationGameModeValue();
            SelectMode.SelectedIndex = gameModeValue;
        }

        public void LoadParameterNbStacksGamePreparation()
        {
            int nbStackSer = IOGamePreparation.LoadParamaterGamePreparationNbStacks();
            SelectNbStack.SelectedIndex = nbStackSer;
        }
        public void LoadParameterNbCardsGamePreparation()
        {
            int nbCards = IOGamePreparation.LoadParameterGamePreparationNbCards();
            SelectNbCard.SelectedIndex = nbCards;
        }



    }
}
