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<string> listGameMode = new List<string> { AppResources.TypeValueWhole, AppResources.TypeValueRelative, AppResources.TypeValueTenthFract, AppResources.TypeValueHundredthFract, AppResources.TypeValueThousandthFract, AppResources.TypeValuefractionated, AppResources.TypeValueTenth, AppResources.TypeValueHundredth };

        public GamePreparationPage()
        {
            InitializeComponent();

            NavigationPage.SetHasNavigationBar(this, false);

            LoadParameterName();
            
            nbPlayersChoose.SetBinding(Label.TextProperty, new Binding("Value", source: PlayerSelecter));
            nbstacks.SetBinding(Label.TextProperty, new Binding("Value", source: SelectNbStack));
            nbCard.SetBinding(Label.TextProperty, new Binding("Value", source: SelectNbCard));





            LoadParameterNbPlayerGamePreparation();

            SelectMode.ItemsSource = listGameMode;
            LoadParameterGameModeValueGamePreparation();

            //SelectNbStack.ItemsSource = listNbStack;
            LoadParameterNbStacksGamePreparation();

            //SelectNbCard.ItemsSource = listNbCard;
            LoadParameterNbCardsGamePreparation();

        }


        private async void Back(object sender, EventArgs e)
        {
            IOGamePreparation.SaveParamaterGamePreparationNbPlayers((int)PlayerSelecter.Value);
            IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbStacks((int)SelectNbStack.Value);
            IOGamePreparation.SaveParameterGamePreparationNbCards((int)SelectNbCard.Value);
            if ((int)PlayerSelecter.Value == 1)
            {
                IOGamePreparation.SaveParameterGamePreparationName(FirstEntry.Text);
            }
            await Navigation.PopToRootAsync();
        }

        protected override bool OnBackButtonPressed()
        {
            IOGamePreparation.SaveParamaterGamePreparationNbPlayers((int)PlayerSelecter.Value);
            IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbStacks((int)SelectNbStack.Value);
            IOGamePreparation.SaveParameterGamePreparationNbCards((int)SelectNbCard.Value);
            if ((int)PlayerSelecter.Value == 1)
            {
                IOGamePreparation.SaveParameterGamePreparationName(FirstEntry.Text);
            }
            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((int)PlayerSelecter.Value);
            IOGamePreparation.SaveParameterGamePreparationGameModeValue(SelectMode.SelectedIndex);
            IOGamePreparation.SaveParameterGamePreparationNbStacks((int)SelectNbStack.Value);
            IOGamePreparation.SaveParameterGamePreparationNbCards((int)SelectNbCard.Value);
            IOGamePreparation.SaveParameterGamePreparationName(FirstEntry.Text);
            await Navigation.PushAsync(new MainPage(playersNames, (int)SelectNbStack.Value, (int)SelectMode.SelectedIndex, (int)SelectNbCard.Value));
        }

        private bool canModif = true;
        private void ChangedPseudo(object sender, EventArgs args)
        {
            if (canModif)
            {
                canModif = false;
                PlayerSelecter.Value = Math.Round(PlayerSelecter.Value);

                if ((int)PlayerSelecter.Value == 1)
                {
                    LoadParameterName();
                }
                else
                {
                    FirstEntry.Text = "";
                }

                while (NameList.Children.Count - 1 != (int)PlayerSelecter.Value)
                {
                    if (NameList.Children.Count - 1 < (int)PlayerSelecter.Value)
                    {

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

                        NameList.Children.Add(e);

                    }
                    else
                    {
                        NameList.Children.RemoveAt(NameList.Children.Count - 1);
                    }

                }
                canModif = true;
            }
        }

        private void ChangedStacks(object sender, EventArgs args)
        {
            if (canModif)
            {
                canModif = false;

                if (SelectNbStack.Value < 5)
                {
                    SelectNbStack.Value = 4;
                }
                else if (SelectNbStack.Value < 7)
                {
                    SelectNbStack.Value = 6;
                }
                else
                {
                    SelectNbStack.Value = 8;
                }

                canModif = true;
            }
        }

        private void ChangedNbCards(object sender, EventArgs args)
        {
            if (canModif)
            {
                canModif = false;

                if (SelectNbCard.Value < 50)
                {
                    SelectNbCard.Value = 40;
                }
                else if (SelectNbCard.Value < 70)
                {
                    SelectNbCard.Value = 60;
                }
                else if (SelectNbCard.Value < 90)
                {
                    SelectNbCard.Value = 80;
                }
                else
                {
                    SelectNbCard.Value = 100;
                }
                canModif = true;
            }
        }

        public void LoadParameterNbPlayerGamePreparation()
        {
            int nbJoueurs = IOGamePreparation.LoadParameterGamePreparationNbPlayers();
            PlayerSelecter.Value = nbJoueurs;
        }

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

        public void LoadParameterNbStacksGamePreparation()
        {
            int nbStackSer = IOGamePreparation.LoadParamaterGamePreparationNbStacks();
            SelectNbStack.Value = nbStackSer;
        }
        public void LoadParameterNbCardsGamePreparation()
        {
            int nbCards = IOGamePreparation.LoadParameterGamePreparationNbCards();
            SelectNbCard.Value = nbCards;
        }
        public void LoadParameterName()
        {
            string pseudo = IOGamePreparation.LoadNameFromGamePrepararion();
            FirstEntry.Text = pseudo;
        }



    }
}
