using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Essentials;
using Firebase.Database;
using Firebase.Database.Query;
using Firebase.Auth;

namespace EcoFootprintCalculator
{
    public partial class MainPage : ContentPage
    {
        Button ecofootprintcalcbutton;
        Label countryfootprint;
        Label allfootprint;
        FormattedString allfootprinttext = "";
        FormattedString countrytext = "";
        Color countryfootprinttextcolor;

        CheckBox checkbox1;
        Label checkbox1label;
        CheckBox checkbox2;
        Label checkbox2label;
        CheckBox checkbox3;
        Label checkbox3label;
        CheckBox checkbox4;
        Label checkbox4label;
        CheckBox checkbox5;
        Label checkbox5label;

        public MainPage()
        {
            InitializeComponent();

            Title = "Home";

            var mainGrid = new Grid
            {
                RowDefinitions =
                {
                    new RowDefinition { Height = GridLength.Star }
                },
                ColumnDefinitions =
                {
                    new ColumnDefinition { Width = GridLength.Star }
                }
            };
            
            var layout = new StackLayout
            {
                Orientation = StackOrientation.Vertical,
                Background = App.bgcolor,
                Opacity = 0.9
            };

            var image = new Image
            {
                Source = "projektlogos.jpg",
                VerticalOptions = LayoutOptions.FillAndExpand,
                Aspect = Aspect.AspectFit,
                Opacity = 1,
                TranslationY = 100
            };

            allfootprint = new Label()
            {
                FontAttributes = FontAttributes.Bold,
                TranslationX = 10,
                FontSize = 16
            };

            countryfootprint = new Label()
            {
                FontAttributes = FontAttributes.Bold,
                TranslationX = 10,
                FontSize = 16
            };

            ecofootprintcalcbutton = new Button()
            {
                FontAttributes = FontAttributes.Bold,
                FontSize = 22
            };
            ecofootprintcalcbutton.Clicked += Ecofootprintcalcbutton_Clicked;

            Grid tipsgrid = new Grid
            {
                RowDefinitions =
                {
                    new RowDefinition { Height = GridLength.Auto },
                    new RowDefinition { Height = GridLength.Auto },
                    new RowDefinition { Height = GridLength.Auto },
                    new RowDefinition { Height = GridLength.Auto },
                    new RowDefinition { Height = GridLength.Auto },
                },
                ColumnDefinitions =
                {
                    new ColumnDefinition{ Width = GridLength.Star },
                    new ColumnDefinition{ Width = new GridLength(40) },
                }
            };

            checkbox1label = new Label
            {
                Text = App.chosentips[0].tip,
                TranslationX = 10,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };
            
            checkbox1 = new CheckBox
            {

            };
            checkbox1.CheckedChanged += Checkbox1_CheckedChanged;
            if (App.calculatedecofootprint<=0)
            {
                checkbox1.IsEnabled = false;
            }

            checkbox2label = new Label
            {
                Text = App.chosentips[1].tip,
                TranslationX = 10,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };

            checkbox2 = new CheckBox
            {

            };
            checkbox2.CheckedChanged += Checkbox2_CheckedChanged;
            if (App.calculatedecofootprint <= 0)
            {
                checkbox2.IsEnabled = false;
            }

            checkbox3label = new Label
            {
                Text = App.chosentips[2].tip,
                TranslationX = 10,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };

            checkbox3 = new CheckBox
            {
                
            };
            checkbox3.CheckedChanged += Checkbox3_CheckedChanged;
            if (App.calculatedecofootprint <= 0)
            {
                checkbox3.IsEnabled = false;
            }

            checkbox4label = new Label
            {
                Text = App.chosentips[3].tip,
                TranslationX = 10,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };

            checkbox4 = new CheckBox
            {

            };
            checkbox4.CheckedChanged += Checkbox4_CheckedChanged;
            if (App.calculatedecofootprint <= 0)
            {
                checkbox4.IsEnabled = false;
            }

            checkbox5label = new Label
            {
                Text = App.chosentips[4].tip,
                TranslationX = 10,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };

            checkbox5 = new CheckBox
            {

            };
            checkbox5.CheckedChanged += Checkbox5_CheckedChanged;
            if (App.calculatedecofootprint <= 0)
            {
                checkbox5.IsEnabled = false;
            }

            var factbutton = new Button
            {
                Text = "Fact of the day",
                VerticalOptions = LayoutOptions.EndAndExpand
            };
            factbutton.Clicked += Factbutton_Clicked;

            layout.Children.Add(new Label { Text = "" });
            layout.Children.Add(new Label { Text = "We would need", FontAttributes = FontAttributes.Bold, TranslationX = 10, FontSize = 16});
            layout.Children.Add(ecofootprintcalcbutton);
            layout.Children.Add(new Label { Text = "Earth(s), if everyone lived like me.", FontAttributes = FontAttributes.Bold, HorizontalTextAlignment = TextAlignment.End, TranslationX = -10, FontSize = 16});
            layout.Children.Add(countryfootprint);
            layout.Children.Add(allfootprint);
            layout.Children.Add(new Label { Text = "" });
            layout.Children.Add(new Label { Text = "Weekly tasks to reduce your footprint:", FontAttributes = FontAttributes.Bold, FontSize = 20 });

            tipsgrid.Children.Add(checkbox1label, 0, 0);
            tipsgrid.Children.Add(checkbox1, 1, 0);
            tipsgrid.Children.Add(checkbox2label, 0, 1);
            tipsgrid.Children.Add(checkbox2, 1, 1);
            tipsgrid.Children.Add(checkbox3label, 0, 2);
            tipsgrid.Children.Add(checkbox3, 1, 2);
            tipsgrid.Children.Add(checkbox4label, 0, 3);
            tipsgrid.Children.Add(checkbox4, 1, 3);
            tipsgrid.Children.Add(checkbox5label, 0, 4);
            tipsgrid.Children.Add(checkbox5, 1, 4);
            layout.Children.Add(tipsgrid);
            layout.Children.Add(factbutton);
            layout.Children.Add(new Label { Text = "" });

            mainGrid.Children.Add(image, 0, 0);
            mainGrid.Children.Add(layout, 0, 0);
            
            this.Content = mainGrid;
        }

        private void Factbutton_Clicked(object sender, EventArgs e)
        {
            DisplayAlert("Fact of the day", App.dailyfacts[App.daylyfactrow], "Thanks for the info", "Not interesting");
        }

        private async void Checkbox1_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            bool answer = await DisplayAlert("Honesty check!", "Have you finished the task?", "Yes", "No");

            if (answer)
            {
                checkbox1.IsEnabled = false;
                Preferences.Set("tip1done", "1");
                App.reductionfromtips += App.chosentips[0].point;
                App.reductionfromtips = App.reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : App.reductionfromtips;
                Preferences.Set("reductionfromtips", App.reductionfromtips.ToString());
            }
            else
            {
                checkbox1.CheckedChanged -= Checkbox1_CheckedChanged;
                checkbox1.IsChecked = false;
                checkbox1.CheckedChanged += Checkbox1_CheckedChanged;
            }
        }

        private async void Checkbox2_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            bool answer = await DisplayAlert("Honesty check!", "Have you finished the task?", "Yes", "No");

            if (answer)
            {
                checkbox2.IsEnabled = false;
                Preferences.Set("tip2done", "1");
                App.reductionfromtips += App.chosentips[1].point;
                App.reductionfromtips = App.reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : App.reductionfromtips;
                Preferences.Set("reductionfromtips", App.reductionfromtips.ToString());
            }
            else
            {
                checkbox2.CheckedChanged -= Checkbox2_CheckedChanged;
                checkbox2.IsChecked = false;
                checkbox2.CheckedChanged += Checkbox2_CheckedChanged;
            }
        }

        private async void Checkbox3_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            bool answer = await DisplayAlert("Honesty check!", "Have you finished the task?", "Yes", "No");

            if (answer)
            {
                checkbox3.IsEnabled = false;
                Preferences.Set("tip3done", "1");
                App.reductionfromtips += App.chosentips[2].point;
                App.reductionfromtips = App.reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : App.reductionfromtips;
                Preferences.Set("reductionfromtips", App.reductionfromtips.ToString());
            }
            else
            {
                checkbox3.CheckedChanged -= Checkbox3_CheckedChanged;
                checkbox3.IsChecked = false;
                checkbox3.CheckedChanged += Checkbox3_CheckedChanged;
            }
        }

        private async void Checkbox4_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            bool answer = await DisplayAlert("Honesty check!", "Have you finished the task?", "Yes", "No");

            if (answer)
            {
                checkbox4.IsEnabled = false;
                Preferences.Set("tip4done", "1");
                App.reductionfromtips += App.chosentips[3].point;
                App.reductionfromtips = App.reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : App.reductionfromtips;
                Preferences.Set("reductionfromtips", App.reductionfromtips.ToString());
            }
            else
            {
                checkbox4.CheckedChanged -= Checkbox4_CheckedChanged;
                checkbox4.IsChecked = false;
                checkbox4.CheckedChanged += Checkbox4_CheckedChanged;
            }
        }

        private async void Checkbox5_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            bool answer = await DisplayAlert("Honesty check!", "Have you finished the task?", "Yes", "No");

            if (answer)
            {
                checkbox5.IsEnabled = false;
                Preferences.Set("tip5done", "1");
                App.reductionfromtips += App.chosentips[4].point;
                App.reductionfromtips = App.reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : App.reductionfromtips;
                Preferences.Set("reductionfromtips", App.reductionfromtips.ToString());
            }
            else
            {
                checkbox5.CheckedChanged -= Checkbox5_CheckedChanged;
                checkbox5.IsChecked = false;
                checkbox5.CheckedChanged += Checkbox5_CheckedChanged;
            }
        }

        protected override async void OnAppearing()
        {
            base.OnAppearing();

            App.screenheight = Application.Current.MainPage.Height;

            App.dayspassedsincelastlogin_calc();

            if (App.loginerror != "")
                await DisplayAlert("Alert", App.loginerror, "Ok");

            if (App.CheckConnection() != NetworkAccess.Internet)
            {
                await DisplayAlert("Connection Error", "No internet connection!\nPlease, restart program after you are connected!", "Ok");
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }

            if (App.loginerror != "")
            {
                App.loginerror = "";
                try
                {
                    var task = Task.Run(() => App.loginanomifnotyet());
                    if (task.Wait(TimeSpan.FromSeconds(5)))
                    {
                        App.auth = task.Result;
                        //App.recalculateecofootprint();
                    }
                    else
                        throw new Exception("Timed out");
                }
                catch (Exception ex)
                {
                    App.loginerror = "There are connection issues!\n Your internet might be slowed down!\n";
                    App.auth.ExpiresIn = 0;
                    App.auth.FirebaseToken = "";
                    App.auth.RefreshToken = "";
                    App.auth.User = new User();
                    App.auth.User.LocalId = "";
                    App.auth.Created = DateTime.Now;
                }
            }            

            if (App.loginerror == "" && (App.updatealreadydone == false || App.dayspassedsincelastlogin > 0))
            {
                App.recalculateecofootprint();
                await App.uploaduserdata();
                await App.downloadcountrydata();
                await App.downloadalldata();
                App.updatealreadydone = true;
            }


            ecofootprintcalcbutton.Text = App.calculatedecofootprint <= 0 ? "Click here to calculate your footprint!" : System.Math.Round((App.calculatedecofootprint - App.reductionfromtips) / 600, 1).ToString();

            Color ecobuttontextcolor = Color.Black;
            if (App.calculatedecofootprint == 0 || String.IsNullOrEmpty(App.countryFootprint.Country))
            {
                ecobuttontextcolor = Color.Black;
            }
            else if ((App.calculatedecofootprint - App.reductionfromtips) > (App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip + 900))
            {
                ecobuttontextcolor = Color.Red;
            }
            else if ((App.calculatedecofootprint - App.reductionfromtips) < (App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip - 900))
            {
                ecobuttontextcolor = Color.Green;
            }
            else if ((App.calculatedecofootprint - App.reductionfromtips) > (App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip + 450))
            {
                ecobuttontextcolor = Color.Salmon;
            }
            else if ((App.calculatedecofootprint - App.reductionfromtips) < (App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip - 450))
            {
                ecobuttontextcolor = Color.LimeGreen;
            }
            else
            {
                ecobuttontextcolor = Color.Goldenrod;
            }
            ecofootprintcalcbutton.TextColor = ecobuttontextcolor;

            //countrytext and color
            if (App.countryFootprint.Country == "0")
            {
                countrytext = "No data available on your country";
            }
            else
            {
                if (String.IsNullOrEmpty(App.countryFootprint.Country))
                {
                    countryfootprinttextcolor = Color.Black;
                }
                else if ((App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip) > (App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips + 900))
                {
                    countryfootprinttextcolor = Color.Red;
                }
                else if ((App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip) < (App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips - 900))
                {
                    countryfootprinttextcolor = Color.Green;
                }
                else if ((App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip) > (App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips + 450))
                {
                    countryfootprinttextcolor = Color.Salmon;
                }
                else if ((App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip) < (App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips - 450))
                {
                    countryfootprinttextcolor = Color.LimeGreen;
                }
                else
                {
                    countryfootprinttextcolor = Color.Goldenrod;
                }

                var formattedstring = new FormattedString();
                formattedstring.Spans.Add(new Span
                {
                    Text = "The avarage user in " + App.countryFootprint.Country + "\n          needs "
                });
                formattedstring.Spans.Add(new Span
                {
                    Text = System.Math.Round((App.countryFootprint.CountryEcoFootprint - App.countryFootprint.CountryReductfromtip) / App.countryFootprint.NumberofUsersofCountry / 600, 1).ToString(),
                    TextColor = countryfootprinttextcolor,
                    FontAttributes = FontAttributes.Bold,
                    FontSize = 20
                });
                formattedstring.Spans.Add(new Span
                {
                    Text = " Earths."
                });
                countrytext = formattedstring;
            }
            countryfootprint.FormattedText = countrytext;
            countryfootprint.FontAttributes = FontAttributes.Bold;


            //Global text and color
            if (App.allFootprint.AllEcoFootprint == 0)
            {
                allfootprinttext = "No data available on the world yet";
            }
            else
            {
                if (App.allFootprint.AllEcoFootprint == 0)
                {
                    countryfootprinttextcolor = Color.Black;
                }
                else if ((App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips) / App.allFootprint.NumberofallUser / 600 > 3.25)
                {
                    countryfootprinttextcolor = Color.Red;
                }
                else if ((App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips) / App.allFootprint.NumberofallUser / 600 > 2.5)
                {
                    countryfootprinttextcolor = Color.Salmon;
                }
                else if ((App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips) / App.allFootprint.NumberofallUser / 600 > 1.75)
                {
                    countryfootprinttextcolor = Color.Goldenrod;
                }
                else if ((App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips) / App.allFootprint.NumberofallUser / 600 > 1)
                {
                    countryfootprinttextcolor = Color.LimeGreen;
                }
                else
                {
                    countryfootprinttextcolor = Color.Green;
                }

                var formattedstring = new FormattedString();
                formattedstring.Spans.Add(new Span
                {
                    Text = "The avarage user around the world\n          needs "
                });
                formattedstring.Spans.Add(new Span
                {
                    Text = System.Math.Round((App.allFootprint.AllEcoFootprint - App.allFootprint.AllReductfromtips) / App.allFootprint.NumberofallUser / 600, 1).ToString(),
                    TextColor = countryfootprinttextcolor,
                    FontAttributes = FontAttributes.Bold,
                    FontSize = 20
                });
                formattedstring.Spans.Add(new Span
                {
                    Text = " Earths."
                });
                allfootprinttext = formattedstring;
            }

            allfootprint.FormattedText = allfootprinttext;
            allfootprint.FontAttributes = FontAttributes.Bold;

            /*await DisplayAlert("Alert", "Upload: " + App.uploaderror + "\nDownloadCountry: " + App.downloadcountryerror + "\nDownloadAll: " + App.downloadallerror, "Ok");
            await DisplayAlert("Alert", App.allFootprint.AllEcoFootprint.ToString(), "Ok");*/

            if (Preferences.Get("tip1done", "0") == "1")
            {
                checkbox1.CheckedChanged -= Checkbox1_CheckedChanged;
                checkbox1.IsChecked = true;
                checkbox1.IsEnabled = false;
                checkbox1.CheckedChanged += Checkbox1_CheckedChanged;
            }

            if (Preferences.Get("tip2done", "0") == "1")
            {
                checkbox2.CheckedChanged -= Checkbox2_CheckedChanged;
                checkbox2.IsChecked = true;
                checkbox2.IsEnabled = false;
                checkbox2.CheckedChanged += Checkbox2_CheckedChanged;
            }

            if (Preferences.Get("tip3done", "0") == "1")
            {
                checkbox3.CheckedChanged -= Checkbox3_CheckedChanged;
                checkbox3.IsChecked = true;
                checkbox3.IsEnabled = false;
                checkbox3.CheckedChanged += Checkbox3_CheckedChanged;
            }

            if (Preferences.Get("tip4done", "0") == "1")
            {
                checkbox4.CheckedChanged -= Checkbox4_CheckedChanged;
                checkbox4.IsChecked = true;
                checkbox4.IsEnabled = false;
                checkbox4.CheckedChanged += Checkbox4_CheckedChanged;
            }

            if (Preferences.Get("tip5done", "0") == "1")
            {
                checkbox5.CheckedChanged -= Checkbox5_CheckedChanged;
                checkbox5.IsChecked = true;
                checkbox5.IsEnabled = false;
                checkbox5.CheckedChanged += Checkbox5_CheckedChanged;
            }
        }

        private async void Ecofootprintcalcbutton_Clicked(object sender, EventArgs e)
        {
            bool answer = await DisplayAlert("Would you like to know your footprint?", "Please, give honest answers!\nOnly this way we can save our planet!", "Yes", "No");

            if (answer)
            {
                App.answered = false;
                App.Current.ModalPopped += countryPopped;
                var country = new Country1();
                await Navigation.PushModalAsync(country);
            }
        }

        private async void countryPopped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += water1Popped;
                var water1 = new Water1();
                await Navigation.PushModalAsync(water1);
                App.Current.ModalPopped -= countryPopped;
            }
            else
            {
                App.Current.ModalPopped -= countryPopped;
            }
        }

        private async void water1Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += water2Popped;
                var water2 = new Water2();
                await Navigation.PushModalAsync(water2);
                App.Current.ModalPopped -= water1Popped;
            }
            else
            {
                App.Current.ModalPopped -= water1Popped;
            }
        }

        private async void water2Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += water3Popped;
                var water3 = new Water3();
                await Navigation.PushModalAsync(water3);
                App.Current.ModalPopped -= water2Popped;
            }
            else
            {
                App.Current.ModalPopped -= water2Popped;
            }
        }

        private async void water3Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += water4Popped;
                var water4 = new Water4();
                await Navigation.PushModalAsync(water4);
                App.Current.ModalPopped -= water3Popped;
            }
            else
            {
                App.Current.ModalPopped -= water3Popped;
            }
        }

        private async void water4Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += water5Popped;
                var water5 = new Water5();
                await Navigation.PushModalAsync(water5);
                App.Current.ModalPopped -= water4Popped;
            }
            else
            {
                App.Current.ModalPopped -= water4Popped;
            }
        }

        private async void water5Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += water6Popped;
                var water6 = new Water6();
                await Navigation.PushModalAsync(water6);
                App.Current.ModalPopped -= water5Popped;
            }
            else
            {
                App.Current.ModalPopped -= water5Popped;
            }
        }

        private async void water6Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += water7Popped;
                var water7 = new Water7();
                await Navigation.PushModalAsync(water7);
                App.Current.ModalPopped -= water6Popped;
            }
            else
            {
                App.Current.ModalPopped -= water6Popped;
            }
        }

        private async void water7Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += food1Popped;
                var food1 = new Food1();
                await Navigation.PushModalAsync(food1);
                App.Current.ModalPopped -= water7Popped;
            }
            else
            {
                App.Current.ModalPopped -= water7Popped;
            }
        }

        private async void food1Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += food2Popped;
                var food2 = new Food2();
                await Navigation.PushModalAsync(food2);
                App.Current.ModalPopped -= food1Popped;
            }
            else
            {
                App.Current.ModalPopped -= food1Popped;
            }
        }

        private async void food2Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += food3Popped;
                var food3 = new Food3();
                await Navigation.PushModalAsync(food3);
                App.Current.ModalPopped -= food2Popped;
            }
            else
            {
                App.Current.ModalPopped -= food2Popped;
            }
        }

        private async void food3Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += food4Popped;
                var food4 = new Food4();
                await Navigation.PushModalAsync(food4);
                App.Current.ModalPopped -= food3Popped;
            }
            else
            {
                App.Current.ModalPopped -= food3Popped;
            }
        }

        private async void food4Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += food5Popped;
                var food5 = new Food5();
                await Navigation.PushModalAsync(food5);
                App.Current.ModalPopped -= food4Popped;
            }
            else
            {
                App.Current.ModalPopped -= food4Popped;
            }
        }

        private async void food5Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += food6Popped;
                var food6 = new Food6();
                await Navigation.PushModalAsync(food6);
                App.Current.ModalPopped -= food5Popped;
            }
            else
            {
                App.Current.ModalPopped -= food5Popped;
            }
        }

        private async void food6Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += food7Popped;
                var food7 = new Food7();
                await Navigation.PushModalAsync(food7);
                App.Current.ModalPopped -= food6Popped;
            }
            else
            {
                App.Current.ModalPopped -= food6Popped;
            }
        }

        private async void food7Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += transport1Popped;
                var transport1 = new Transport1();
                await Navigation.PushModalAsync(transport1);
                App.Current.ModalPopped -= food7Popped;
            }
            else
            {
                App.Current.ModalPopped -= food7Popped;
            }
        }

        private async void transport1Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += transport2Popped;
                var transport2 = new Transport2();
                await Navigation.PushModalAsync(transport2);
                App.Current.ModalPopped -= transport1Popped;
            }
            else
            {
                App.Current.ModalPopped -= transport1Popped;
            }
        }

        private async void transport2Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += transport3Popped;
                var transport3 = new Transport3();
                await Navigation.PushModalAsync(transport3);
                App.Current.ModalPopped -= transport2Popped;
            }
            else
            {
                App.Current.ModalPopped -= transport2Popped;
            }
        }

        private async void transport3Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += transport4Popped;
                var transport4 = new Transport4();
                await Navigation.PushModalAsync(transport4);
                App.Current.ModalPopped -= transport3Popped;
            }
            else
            {
                App.Current.ModalPopped -= transport3Popped;
            }
        }

        private async void transport4Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += transport5Popped;
                var transport5 = new Transport5();
                await Navigation.PushModalAsync(transport5);
                App.Current.ModalPopped -= transport4Popped;
            }
            else
            {
                App.Current.ModalPopped -= transport4Popped;
            }
        }

        private async void transport5Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += transport6Popped;
                var transport6 = new Transport6();
                await Navigation.PushModalAsync(transport6);
                App.Current.ModalPopped -= transport5Popped;
            }
            else
            {
                App.Current.ModalPopped -= transport5Popped;
            }
        }

        private async void transport6Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += shelter1Popped;
                var shelter1 = new Shelter1();
                await Navigation.PushModalAsync(shelter1);
                App.Current.ModalPopped -= transport6Popped;
            }
            else
            {
                App.Current.ModalPopped -= transport6Popped;
            }
        }

        private async void shelter1Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += shelter2Popped;
                var shelter2 = new Shelter2();
                await Navigation.PushModalAsync(shelter2);
                App.Current.ModalPopped -= shelter1Popped;
            }
            else
            {
                App.Current.ModalPopped -= shelter1Popped;
            }
        }

        private async void shelter2Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += shelter3Popped;
                var shelter3 = new Shelter3();
                await Navigation.PushModalAsync(shelter3);
                App.Current.ModalPopped -= shelter2Popped;
            }
            else
            {
                App.Current.ModalPopped -= shelter2Popped;
            }
        }

        private async void shelter3Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += energy1Popped;
                var energy1 = new Energy1();
                await Navigation.PushModalAsync(energy1);
                App.Current.ModalPopped -= shelter3Popped;
            }
            else
            {
                App.Current.ModalPopped -= shelter3Popped;
            }
        }

        private async void energy1Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += energy2Popped;
                var energy2 = new Energy2();
                await Navigation.PushModalAsync(energy2);
                App.Current.ModalPopped -= energy1Popped;
            }
            else
            {
                App.Current.ModalPopped -= energy1Popped;
            }
        }

        private async void energy2Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += energy3Popped;
                var energy3 = new Energy3();
                await Navigation.PushModalAsync(energy3);
                App.Current.ModalPopped -= energy2Popped;
            }
            else
            {
                App.Current.ModalPopped -= energy2Popped;
            }
        }

        private async void energy3Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += energy4Popped;
                var energy4 = new Energy4();
                await Navigation.PushModalAsync(energy4);
                App.Current.ModalPopped -= energy3Popped;
            }
            else
            {
                App.Current.ModalPopped -= energy3Popped;
            }
        }

        private async void energy4Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += energy5Popped;
                var energy5 = new Energy5();
                await Navigation.PushModalAsync(energy5);
                App.Current.ModalPopped -= energy4Popped;
            }
            else
            {
                App.Current.ModalPopped -= energy4Popped;
            }
        }

        private async void energy5Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += energy6Popped;
                var energy6 = new Energy6();
                await Navigation.PushModalAsync(energy6);
                App.Current.ModalPopped -= energy5Popped;
            }
            else
            {
                App.Current.ModalPopped -= energy5Popped;
            }
        }

        private async void energy6Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += energy7Popped;
                var energy7 = new Energy7();
                await Navigation.PushModalAsync(energy7);
                App.Current.ModalPopped -= energy6Popped;
            }
            else
            {
                App.Current.ModalPopped -= energy6Popped;
            }
        }

        private async void energy7Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += cloth1Popped;
                var cloth1 = new Cloth1();
                await Navigation.PushModalAsync(cloth1);
                App.Current.ModalPopped -= energy7Popped;
            }
            else
            {
                App.Current.ModalPopped -= energy7Popped;
            }
        }

        private async void cloth1Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += cloth2Popped;
                var cloth2 = new Cloth2();
                await Navigation.PushModalAsync(cloth2);
                App.Current.ModalPopped -= cloth1Popped;
            }
            else
            {
                App.Current.ModalPopped -= cloth1Popped;
            }
        }

        private async void cloth2Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += cloth3Popped;
                var cloth3 = new Cloth3();
                await Navigation.PushModalAsync(cloth3);
                App.Current.ModalPopped -= cloth2Popped;
            }
            else
            {
                App.Current.ModalPopped -= cloth2Popped;
            }
        }

        private async void cloth3Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += cloth4Popped;
                var cloth4 = new Cloth4();
                await Navigation.PushModalAsync(cloth4);
                App.Current.ModalPopped -= cloth3Popped;
            }
            else
            {
                App.Current.ModalPopped -= cloth3Popped;
            }
        }

        private async void cloth4Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += cloth5Popped;
                var cloth5 = new Cloth5();
                await Navigation.PushModalAsync(cloth5);
                App.Current.ModalPopped -= cloth4Popped;
            }
            else
            {
                App.Current.ModalPopped -= cloth4Popped;
            }
        }

        private async void cloth5Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += cloth6Popped;
                var cloth6 = new Cloth6();
                await Navigation.PushModalAsync(cloth6);
                App.Current.ModalPopped -= cloth5Popped;
            }
            else
            {
                App.Current.ModalPopped -= cloth5Popped;
            }
        }

        private async void cloth6Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += cloth7Popped;
                var cloth7 = new Cloth7();
                await Navigation.PushModalAsync(cloth7);
                App.Current.ModalPopped -= cloth6Popped;
            }
            else
            {
                App.Current.ModalPopped -= cloth6Popped;
            }
        }

        private async void cloth7Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += cloth8Popped;
                var cloth8 = new Cloth8();
                await Navigation.PushModalAsync(cloth8);
                App.Current.ModalPopped -= cloth7Popped;
            }
            else
            {
                App.Current.ModalPopped -= cloth7Popped;
            }
        }

        private async void cloth8Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += waste1Popped;
                var waste1 = new Waste1();
                await Navigation.PushModalAsync(waste1);
                App.Current.ModalPopped -= cloth8Popped;
            }
            else
            {
                App.Current.ModalPopped -= cloth8Popped;
            }
        }

        private async void waste1Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += waste2Popped;
                var waste2 = new Waste2();
                await Navigation.PushModalAsync(waste2);
                App.Current.ModalPopped -= waste1Popped;
            }
            else
            {
                App.Current.ModalPopped -= waste1Popped;
            }
        }

        private async void waste2Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += waste3Popped;
                var waste3 = new Waste3();
                await Navigation.PushModalAsync(waste3);
                App.Current.ModalPopped -= waste2Popped;
            }
            else
            {
                App.Current.ModalPopped -= waste2Popped;
            }
        }

        private async void waste3Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += waste4Popped;
                var waste4 = new Waste4();
                await Navigation.PushModalAsync(waste4);
                App.Current.ModalPopped -= waste3Popped;
            }
            else
            {
                App.Current.ModalPopped -= waste3Popped;
            }
        }

        private async void waste4Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += waste5Popped;
                var waste5 = new Waste5();
                await Navigation.PushModalAsync(waste5);
                App.Current.ModalPopped -= waste4Popped;
            }
            else
            {
                App.Current.ModalPopped -= waste4Popped;
            }
        }

        private async void waste5Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += waste6Popped;
                var waste6 = new Waste6();
                await Navigation.PushModalAsync(waste6);
                App.Current.ModalPopped -= waste5Popped;
            }
            else
            {
                App.Current.ModalPopped -= waste5Popped;
            }
        }

        private async void waste6Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += waste7Popped;
                var waste7 = new Waste7();
                await Navigation.PushModalAsync(waste7);
                App.Current.ModalPopped -= waste6Popped;
            }
            else
            {
                App.Current.ModalPopped -= waste6Popped;
            }
        }

        private async void waste7Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += fun1Popped;
                var fun1 = new Fun1();
                await Navigation.PushModalAsync(fun1);
                App.Current.ModalPopped -= waste7Popped;
            }
            else
            {
                App.Current.ModalPopped -= waste7Popped;
            }
        }

        private async void fun1Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += fun2Popped;
                var fun2 = new Fun2();
                await Navigation.PushModalAsync(fun2);
                App.Current.ModalPopped -= fun1Popped;
            }
            else
            {
                App.Current.ModalPopped -= fun1Popped;
            }
        }

        private async void fun2Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;
                App.Current.ModalPopped += fun3Popped;
                var fun3 = new Fun3();
                await Navigation.PushModalAsync(fun3);
                App.Current.ModalPopped -= fun2Popped;
            }
            else
            {
                App.Current.ModalPopped -= fun2Popped;
            }
        }

        private async void fun3Popped(object sender, ModalPoppedEventArgs e)
        {
            if (App.answered)
            {
                App.answered = false;

                if (App.CheckConnection() != NetworkAccess.Internet)
                {
                    await DisplayAlert("Connection Error", "No internet connection!\nPlease, restart program after you are connected!", "Ok");
                    System.Diagnostics.Process.GetCurrentProcess().Kill();
                }

                App.allwater = App.water1 + App.water2 + App.water3 + App.water4 + App.water5 + App.water6 + App.water7;
                App.allfood = App.food1 + App.food2 + App.food3 + App.food4 + App.food5 + App.food6 + App.food7;
                App.alltransport = App.transport1 + App.transport2 + App.transport3 + App.transport4 + App.transport5 + App.transport6;
                App.allshelter = App.shelter1 + App.shelter2 + App.shelter3;
                App.allenergy = App.energy1 + App.energy2 + App.energy3 + App.energy4 + App.energy5 + App.energy6 + App.energy7;
                App.allcloth = App.cloth1 + App.cloth2 + App.cloth3 + App.cloth4 + App.cloth5 + App.cloth6 + App.cloth7 + App.cloth8;
                App.allwaste = App.waste1 + App.waste2 + App.waste3 + App.waste4 + App.waste5 + App.waste6 + App.waste7;
                App.allfun = App.fun1 + App.fun2 + App.fun3;
                App.calculatedecofootprint = App.allwater + App.allfood + App.alltransport + App.allshelter + App.allenergy + App.allcloth + App.allwaste + App.allfun;

                if (App.calculatedecofootprint > 0)
                {
                    App.updatealreadydone = false;
                    checkbox1.IsEnabled = true;
                    checkbox2.IsEnabled = true;
                    checkbox3.IsEnabled = true;
                    checkbox4.IsEnabled = true;
                    checkbox5.IsEnabled = true;
                }
                else
                {
                    await DisplayAlert("Slight stretch detected!", "Please, read the About page, and fill in the questionnaire again!", "Ok");
                }                

                /*if (App.uploaderror != "" || App.downloadallerror != "" || App.downloadcountryerror != "")
                {
                    await DisplayAlert("Alert", "Upload: " + App.uploaderror + "\nDownloadCountry: " + App.downloadcountryerror + "\nDownloadAll: " + App.downloadallerror, "Ok");
                }
                else
                {
                    await DisplayAlert("Finished", "Thank you!", "Ok");
                }*/
                await DisplayAlert("Finished", "Thank you!", "Ok");

                App.Current.ModalPopped -= fun3Popped;
            }
            else
            {
                App.Current.ModalPopped -= fun3Popped;
            }
        }
    }
}
