using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Essentials;

namespace EcoFootprintCalculator
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Country1 : ContentPage
    {
        Button nextbutton;
        public Country1()
        {
            InitializeComponent();

            var layout = new StackLayout
            {
                Orientation = StackOrientation.Vertical,
                Background = App.bgcolor
            };

            var countrylist = new List<string>();
            countrylist.Add("Afghanistan");
            countrylist.Add("Albania");
            countrylist.Add("Algeria");
            countrylist.Add("American Samoa");
            countrylist.Add("Andorra");
            countrylist.Add("Angola");
            countrylist.Add("Anguilla");
            countrylist.Add("Antarctica");
            countrylist.Add("Antigua and Barbuda");
            countrylist.Add("Argentina");
            countrylist.Add("Armenia");
            countrylist.Add("Aruba");
            countrylist.Add("Australia");
            countrylist.Add("Austria");
            countrylist.Add("Azerbaijan");
            countrylist.Add("Bahamas");
            countrylist.Add("Bahrain");
            countrylist.Add("Bangladesh");
            countrylist.Add("Barbados");
            countrylist.Add("Belarus");
            countrylist.Add("Belgium");
            countrylist.Add("Belize");
            countrylist.Add("Benin");
            countrylist.Add("Bermuda");
            countrylist.Add("Bhutan");
            countrylist.Add("Bolivia, Plurinational State of");
            countrylist.Add("Bonaire, Sint Eustatius and Saba");
            countrylist.Add("Bosnia and Herzegovina");
            countrylist.Add("Botswana");
            countrylist.Add("Bouvet Island");
            countrylist.Add("Brazil");
            countrylist.Add("British Indian Ocean Territory");
            countrylist.Add("Brunei Darussalam");
            countrylist.Add("Bulgaria");
            countrylist.Add("Burkina Faso");
            countrylist.Add("Burundi");
            countrylist.Add("Cambodia");
            countrylist.Add("Cameroon");
            countrylist.Add("Canada");
            countrylist.Add("Cape Verde");
            countrylist.Add("Cayman Islands");
            countrylist.Add("Central African Republic");
            countrylist.Add("Chad");
            countrylist.Add("Chile");
            countrylist.Add("China");
            countrylist.Add("Christmas Island");
            countrylist.Add("Cocos(Keeling) Islands");
            countrylist.Add("Colombia");
            countrylist.Add("Comoros");
            countrylist.Add("Congo");
            countrylist.Add("Congo, the Democratic Republic of the");
            countrylist.Add("Cook Islands");
            countrylist.Add("Costa Rica");
            countrylist.Add("Croatia");
            countrylist.Add("Cuba");
            countrylist.Add("Curaçao");
            countrylist.Add("Cyprus");
            countrylist.Add("Czech Republic");
            countrylist.Add("Côte d'Ivoire");
            countrylist.Add("Denmark");
            countrylist.Add("Djibouti");
            countrylist.Add("Dominica");
            countrylist.Add("Dominican Republic");
            countrylist.Add("Ecuador");
            countrylist.Add("Egypt");
            countrylist.Add("El Salvador");
            countrylist.Add("Equatorial Guinea");
            countrylist.Add("Eritrea");
            countrylist.Add("Estonia");
            countrylist.Add("Ethiopia");
            countrylist.Add("Falkland Islands(Malvinas)");
            countrylist.Add("Faroe Islands");
            countrylist.Add("Fiji");
            countrylist.Add("Finland");
            countrylist.Add("France");
            countrylist.Add("French Guiana");
            countrylist.Add("French Polynesia");
            countrylist.Add("French Southern Territories");
            countrylist.Add("Gabon");
            countrylist.Add("Gambia");
            countrylist.Add("Georgia");
            countrylist.Add("Germany");
            countrylist.Add("Ghana");
            countrylist.Add("Gibraltar");
            countrylist.Add("Greece");
            countrylist.Add("Greenland");
            countrylist.Add("Grenada");
            countrylist.Add("Guadeloupe");
            countrylist.Add("Guam");
            countrylist.Add("Guatemala");
            countrylist.Add("Guernsey");
            countrylist.Add("Guinea");
            countrylist.Add("Guinea - Bissau");
            countrylist.Add("Guyana");
            countrylist.Add("Haiti");
            countrylist.Add("Heard Island and McDonald Islands");
            countrylist.Add("Holy See(Vatican City State)");
            countrylist.Add("Honduras");
            countrylist.Add("Hong Kong");
            countrylist.Add("Hungary");
            countrylist.Add("Iceland");
            countrylist.Add("India");
            countrylist.Add("Indonesia");
            countrylist.Add("Iran, Islamic Republic of");
            countrylist.Add("Iraq");
            countrylist.Add("Ireland");
            countrylist.Add("Isle of Man");
            countrylist.Add("Israel");
            countrylist.Add("Italy");
            countrylist.Add("Jamaica");
            countrylist.Add("Japan");
            countrylist.Add("Jersey");
            countrylist.Add("Jordan");
            countrylist.Add("Kazakhstan");
            countrylist.Add("Kenya");
            countrylist.Add("Kiribati");
            countrylist.Add("Korea, Democratic People's Republic of");
            countrylist.Add("Korea, Republic of");
            countrylist.Add("Kuwait");
            countrylist.Add("Kyrgyzstan");
            countrylist.Add("Lao People's Democratic Republic");
            countrylist.Add("Latvia");
            countrylist.Add("Lebanon");
            countrylist.Add("Lesotho");
            countrylist.Add("Liberia");
            countrylist.Add("Libya");
            countrylist.Add("Liechtenstein");
            countrylist.Add("Lithuania");
            countrylist.Add("Luxembourg");
            countrylist.Add("Macao");
            countrylist.Add("Macedonia, the Former Yugoslav Republic of");
            countrylist.Add("Madagascar");
            countrylist.Add("Malawi");
            countrylist.Add("Malaysia");
            countrylist.Add("Maldives");
            countrylist.Add("Mali");
            countrylist.Add("Malta");
            countrylist.Add("Marshall Islands");
            countrylist.Add("Martinique");
            countrylist.Add("Mauritania");
            countrylist.Add("Mauritius");
            countrylist.Add("Mayotte");
            countrylist.Add("Mexico");
            countrylist.Add("Micronesia, Federated States of");
            countrylist.Add("Moldova, Republic of");
            countrylist.Add("Monaco");
            countrylist.Add("Mongolia");
            countrylist.Add("Montenegro");
            countrylist.Add("Montserrat");
            countrylist.Add("Morocco");
            countrylist.Add("Mozambique");
            countrylist.Add("Myanmar");
            countrylist.Add("Namibia");
            countrylist.Add("Nauru");
            countrylist.Add("Nepal");
            countrylist.Add("Netherlands");
            countrylist.Add("New Caledonia");
            countrylist.Add("New Zealand");
            countrylist.Add("Nicaragua");
            countrylist.Add("Niger");
            countrylist.Add("Nigeria");
            countrylist.Add("Niue");
            countrylist.Add("Norfolk Island");
            countrylist.Add("Northern Mariana Islands");
            countrylist.Add("Norway");
            countrylist.Add("Oman");
            countrylist.Add("Pakistan");
            countrylist.Add("Palau");
            countrylist.Add("Palestine, State of");
            countrylist.Add("Panama");
            countrylist.Add("Papua New Guinea");
            countrylist.Add("Paraguay");
            countrylist.Add("Peru");
            countrylist.Add("Philippines");
            countrylist.Add("Pitcairn");
            countrylist.Add("Poland");
            countrylist.Add("Portugal");
            countrylist.Add("Puerto Rico");
            countrylist.Add("Qatar");
            countrylist.Add("Romania");
            countrylist.Add("Russian Federation");
            countrylist.Add("Rwanda");
            countrylist.Add("Réunion");
            countrylist.Add("Saint Barthélemy");
            countrylist.Add("Saint Helena, Ascension and Tristan da Cunha");
            countrylist.Add("Saint Kitts and Nevis");
            countrylist.Add("Saint Lucia");
            countrylist.Add("Saint Martin(French part)");
            countrylist.Add("Saint Pierre and Miquelon");
            countrylist.Add("Saint Vincent and the Grenadines");
            countrylist.Add("Samoa");
            countrylist.Add("San Marino");
            countrylist.Add("Sao Tome and Principe");
            countrylist.Add("Saudi Arabia");
            countrylist.Add("Senegal");
            countrylist.Add("Serbia");
            countrylist.Add("Seychelles");
            countrylist.Add("Sierra Leone");
            countrylist.Add("Singapore");
            countrylist.Add("Sint Maarten(Dutch part)");
            countrylist.Add("Slovakia");
            countrylist.Add("Slovenia");
            countrylist.Add("Solomon Islands");
            countrylist.Add("Somalia");
            countrylist.Add("South Africa");
            countrylist.Add("South Georgia and the South Sandwich Islands");
            countrylist.Add("South Sudan");
            countrylist.Add("Spain");
            countrylist.Add("Sri Lanka");
            countrylist.Add("Sudan");
            countrylist.Add("Suriname");
            countrylist.Add("Svalbard and Jan Mayen");
            countrylist.Add("Swaziland");
            countrylist.Add("Sweden");
            countrylist.Add("Switzerland");
            countrylist.Add("Syrian Arab Republic");
            countrylist.Add("Taiwan, Province of China");
            countrylist.Add("Tajikistan");
            countrylist.Add("Tanzania, United Republic of");
            countrylist.Add("Thailand");
            countrylist.Add("Timor - Leste");
            countrylist.Add("Togo");
            countrylist.Add("Tokelau");
            countrylist.Add("Tonga");
            countrylist.Add("Trinidad and Tobago");
            countrylist.Add("Tunisia");
            countrylist.Add("Turkey");
            countrylist.Add("Turkmenistan");
            countrylist.Add("Turks and Caicos Islands");
            countrylist.Add("Tuvalu");
            countrylist.Add("Uganda");
            countrylist.Add("Ukraine");
            countrylist.Add("United Arab Emirates");
            countrylist.Add("United Kingdom");
            countrylist.Add("United States");
            countrylist.Add("United States Minor Outlying Islands");
            countrylist.Add("Uruguay");
            countrylist.Add("Uzbekistan");
            countrylist.Add("Vanuatu");
            countrylist.Add("Venezuela, Bolivarian Republic of");
            countrylist.Add("Viet Nam");
            countrylist.Add("Virgin Islands, British");
            countrylist.Add("Virgin Islands, U.S.");
            countrylist.Add("Wallis and Futuna");
            countrylist.Add("Western Sahara");
            countrylist.Add("Yemen");
            countrylist.Add("Zambia");
            countrylist.Add("Zimbabwe");
            countrylist.Add("Åland Islands");

            var picker = new Picker()
            {
                Title = "Select your country:"
            };
            picker.ItemsSource = countrylist;
            picker.SelectedIndexChanged += Picker_SelectedIndexChanged;

            nextbutton = new Button()
            {
                Text = "Next",
                IsEnabled = false,
                VerticalOptions = LayoutOptions.EndAndExpand
            };
            nextbutton.Clicked += Nextbutton_Clicked;

            var cancelbutton = new Button()
            {
                Text = "Cancel"
            };
            cancelbutton.Clicked += Cancelbutton_Clicked;

            var image = new Image
            {
                Source = "country.jpg",
                VerticalOptions = LayoutOptions.FillAndExpand,
                Aspect = Aspect.AspectFit,
            };
            /*image.GestureRecognizers.Add(new TapGestureRecognizer ////////////////arra az esetre ha linket akarnánk a képekre, pl tippek, érdekes infók stb...
            {
                Command = new Command(() => {
                    Launcher.OpenAsync(new Uri("https://erasmus-plus.ec.europa.eu/"));
                })
            });*/

            //layout.Children.Add(new Label { Text = "All my garbage from today could fit into a:" });
            layout.Children.Add(new Label { Text = "1/49 Question" });
            //layout.Children.Add(new Label { Text = "Select your country:" });
            layout.Children.Add(picker);
            layout.Children.Add(image);
            layout.Children.Add(nextbutton);
            layout.Children.Add(cancelbutton);

            this.Content = layout;

            //picker.SelectedIndex = 0;
            App.country = Convert.ToString(Preferences.Get("country", "0"));
            if (App.country != "0")
            {
                picker.SelectedIndex = countrylist.IndexOf(App.country);
            }
        }

        private void Picker_SelectedIndexChanged(object sender, EventArgs e)
        {
            nextbutton.IsEnabled = true;
            var pc = (Picker)sender;
            int selectedIndex = pc.SelectedIndex;
            Preferences.Set("country", pc.ItemsSource[selectedIndex].ToString());
        }

        private async void Cancelbutton_Clicked(object sender, EventArgs e)
        {
            bool answer = await DisplayAlert("Question?", "Are you sure you want to cancel the calculation?", "Yes", "No");
            if (answer)
            {
                await Navigation.PopModalAsync();
            }
        }

        private async void Nextbutton_Clicked(object sender, EventArgs e)
        {
            App.answered = true;
            await Navigation.PopModalAsync();
        }
    }
}