

namespace GameAtlas.Models
{
    public class Popup
    {
        public static Manager PopUpManager => (App.Current as App).MyManager;

        public static async Task<string> CreatePopupAsync()
        {
            var tcs = new TaskCompletionSource<string>();

            var popupPage = new ContentPage();
            popupPage.BackgroundColor = Color.FromRgba(255, 255, 255, 0);
            var popupLayout = new StackLayout
            {
                Padding = new Thickness(20),
                BackgroundColor = Color.FromRgba(255, 255, 255, 255)
            };

            var titleLabel = new Label
            {
                Text = "Informations sur le Jeu",
                FontSize = 18,
                FontAttributes = FontAttributes.Bold,
                HorizontalOptions = LayoutOptions.Center
            };

            popupLayout.Children.Add(titleLabel);

            var nomLabel = new Label
            {
                Text = "Nom :"
            };

            var nomEntry = new Entry();
            popupLayout.Children.Add(nomLabel);
            popupLayout.Children.Add(nomEntry);

            var sortieLabel = new Label
            {
                Text = "Date de Sortie :"
            };

            var sortieDatePicker = new DatePicker();
            popupLayout.Children.Add(sortieLabel);
            popupLayout.Children.Add(sortieDatePicker);

            var plateformesLabel = new Label
            {
                Text = "Plateformes :"
            };

            var plateformesEntry = new Entry();
            popupLayout.Children.Add(plateformesLabel);
            popupLayout.Children.Add(plateformesEntry);


            var developpeurLabel = new Label
            {
                Text = "Développeur :"
            };

            var developpeurEntry = new Entry();
            popupLayout.Children.Add(developpeurLabel);
            popupLayout.Children.Add(developpeurEntry);

            var editeurLabel = new Label
            {
                Text = "Éditeur :"
            };

            var editeurEntry = new Entry();
            popupLayout.Children.Add(editeurLabel);
            popupLayout.Children.Add(editeurEntry);

            var noteLabel = new Label
            {
                Text = "Note :"
            };

            var noteEntry = new Entry();
            popupLayout.Children.Add(noteLabel);
            popupLayout.Children.Add(noteEntry);

            var nbTelechargementLabel = new Label
            {
                Text = "Nombre de téléchargements :"
            };

            var nbTelechargementEntry = new Entry();
            popupLayout.Children.Add(nbTelechargementLabel);
            popupLayout.Children.Add(nbTelechargementEntry);

            var genresLabel = new Label
            {
                Text = "Genres :"
            };

            var genresEntry = new Entry();
            popupLayout.Children.Add(genresLabel);
            popupLayout.Children.Add(genresEntry);

            var imageLabel = new Label
            {
                Text = "Image :"
            };

            var imageEntry = new Entry();
            popupLayout.Children.Add(imageLabel);
            popupLayout.Children.Add(imageEntry);

            var saveButton = new Button
            {
                Text = "Enregistrer",
                HorizontalOptions = LayoutOptions.Center,
                Margin = new Thickness(0, 20, 0, 0)
            };

            saveButton.Clicked += (sender, args) =>
            {
                // Récupérer les valeurs des champs
                string nom = nomEntry.Text;
                DateTime sortie = sortieDatePicker.Date;
                string sortieString = sortie.ToString("dd/MM/yyyy");
                string plateformesText = plateformesEntry.Text;
                string developpeur = developpeurEntry.Text;
                string editeur = editeurEntry.Text;
                int note = int.Parse(noteEntry.Text);
                int nbTelechargement = int.Parse(nbTelechargementEntry.Text);
                string genresText = genresEntry.Text;
                string image = imageEntry.Text;

                // Séparer les plateformes par des espaces et les ajouter à une liste
                // List<string> plateformesList = plateformesText.Split(' ').ToList();

                // Séparer les genres par des espaces et les ajouter à une liste
                // List<string> genresList = genresText.Split(' ').ToList();

                Jeu jeu = new Jeu
                (
                    nom,
                    sortieString,
                    //plateformesList,
                    plateformesText,
                    developpeur,
                    editeur,
                    note,
                    nbTelechargement,
                    //genresList,
                    genresText,
                    image
                );

                Popup.PopUpManager.AddJeux(jeu);
                Popup.PopUpManager.SauvegardeDonnees();

                // Fermer la popup et retourner les valeurs
                Application.Current.MainPage.Navigation.PopModalAsync();
                tcs.SetResult("");
            };

            popupLayout.Children.Add(saveButton);

            popupPage.Content = popupLayout;

            await Application.Current.MainPage.Navigation.PushModalAsync(popupPage);

            return await tcs.Task;
        }
    }
}
