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 Food1 : ContentPage
    {
        Button nextbutton;
        Slider slider1, slider2, slider3, slider4, slider5, slider6, slider7, slider8, slider9;
        Label slider1valuelabel, slider2valuelabel, slider3valuelabel, slider4valuelabel, slider5valuelabel, slider6valuelabel, slider7valuelabel, slider8valuelabel, slider9valuelabel;
        double vegosszeg;
        double StepValue = 1.0;
        public Food1()
        {
            InitializeComponent();

            var scroll = new ScrollView
            {
                VerticalOptions = LayoutOptions.Fill
            };

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

            var slider1label = new Label()
            {
                Text = "Number of portion of Beef"
            };
            slider1 = new Slider()
            {
                Maximum = 10
            };
            slider1.ValueChanged += Slider1_ValueChanged;
            slider1valuelabel = new Label()
            {
                Text = slider1.Value.ToString()
            };

            var slider2label = new Label()
            {
                Text = "Number of portion of Chicken"
            };
            slider2 = new Slider()
            {
                Maximum = 10
            };
            slider2.ValueChanged += Slider2_ValueChanged;
            slider2valuelabel = new Label()
            {
                Text = slider2.Value.ToString()
            };

            var slider3label = new Label()
            {
                Text = "Number of portion of Farmed fish"
            };
            slider3 = new Slider()
            {
                Maximum = 10
            };
            slider3.ValueChanged += Slider3_ValueChanged;
            slider3valuelabel = new Label()
            {
                Text = slider3.Value.ToString()
            };

            var slider4label = new Label()
            {
                Text = "Number of portion of Wild fish"
            };
            slider4 = new Slider()
            {
                Maximum = 10
            };
            slider4.ValueChanged += Slider4_ValueChanged;
            slider4valuelabel = new Label()
            {
                Text = slider4.Value.ToString()
            };

            var slider5label = new Label()
            {
                Text = "Number of portion of Eggs"
            };
            slider5 = new Slider()
            {
                Maximum = 10
            };
            slider5.ValueChanged += Slider5_ValueChanged;
            slider5valuelabel = new Label()
            {
                Text = slider5.Value.ToString()
            };

            var slider6label = new Label()
            {
                Text = "Number of portion of Milk/dairy"
            };
            slider6 = new Slider()
            {
                Maximum = 10
            };
            slider6.ValueChanged += Slider6_ValueChanged;
            slider6valuelabel = new Label()
            {
                Text = slider6.Value.ToString()
            };

            var slider7label = new Label()
            {
                Text = "Number of portion of Fruit"
            };
            slider7 = new Slider()
            {
                Maximum = 10
            };
            slider7.ValueChanged += Slider7_ValueChanged;
            slider7valuelabel = new Label()
            {
                Text = slider7.Value.ToString()
            };

            var slider8label = new Label()
            {
                Text = "Number of portion of Vegetables"
            };
            slider8 = new Slider()
            {
                Maximum = 10
            };
            slider8.ValueChanged += Slider8_ValueChanged;
            slider8valuelabel = new Label()
            {
                Text = slider8.Value.ToString()
            };

            var slider9label = new Label()
            {
                Text = "Number of portion of Grains: bread, cereal, rice"
            };
            slider9 = new Slider()
            {
                Maximum = 10
            };
            slider9.ValueChanged += Slider9_ValueChanged;
            slider9valuelabel = new Label()
            {
                Text = slider9.Value.ToString()
            };

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

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

            var image = new Image
            {
                Source = "food1.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 = "9/49 Question" });
            layout.Children.Add(new Label { Text = "On a typical day, I eat:", FontSize = 20 });
            layout.Children.Add(slider1label);
            layout.Children.Add(slider1);
            layout.Children.Add(slider1valuelabel);
            layout.Children.Add(image);
            layout.Children.Add(slider2label);
            layout.Children.Add(slider2);
            layout.Children.Add(slider2valuelabel);
            layout.Children.Add(slider3label);
            layout.Children.Add(slider3);
            layout.Children.Add(slider3valuelabel);
            layout.Children.Add(slider4label);
            layout.Children.Add(slider4);
            layout.Children.Add(slider4valuelabel);
            layout.Children.Add(slider5label);
            layout.Children.Add(slider5);
            layout.Children.Add(slider5valuelabel);
            layout.Children.Add(slider6label);
            layout.Children.Add(slider6);
            layout.Children.Add(slider6valuelabel);
            layout.Children.Add(slider7label);
            layout.Children.Add(slider7);
            layout.Children.Add(slider7valuelabel);
            layout.Children.Add(slider8label);
            layout.Children.Add(slider8);
            layout.Children.Add(slider8valuelabel);
            layout.Children.Add(slider9label);
            layout.Children.Add(slider9);
            layout.Children.Add(slider9valuelabel);
            layout.Children.Add(nextbutton);
            layout.Children.Add(cancelbutton);

            scroll.Content = layout;
            this.Content = scroll;

            App.food1value_1 = Convert.ToDouble(Preferences.Get("food1value_1", "-1"));
            App.food1value_2 = Convert.ToDouble(Preferences.Get("food1value_2", "-1"));
            App.food1value_3 = Convert.ToDouble(Preferences.Get("food1value_3", "-1"));
            App.food1value_4 = Convert.ToDouble(Preferences.Get("food1value_4", "-1"));
            App.food1value_5 = Convert.ToDouble(Preferences.Get("food1value_5", "-1"));
            App.food1value_6 = Convert.ToDouble(Preferences.Get("food1value_6", "-1"));
            App.food1value_7 = Convert.ToDouble(Preferences.Get("food1value_7", "-1"));
            App.food1value_8 = Convert.ToDouble(Preferences.Get("food1value_8", "-1"));
            App.food1value_9 = Convert.ToDouble(Preferences.Get("food1value_9", "-1"));

            if (App.food1value_1 != -1)
            {
                slider1.Value = App.food1value_1;
            }
            if (App.food1value_2 != -1)
            {
                slider2.Value = App.food1value_2;
            }
            if (App.food1value_3 != -1)
            {
                slider3.Value = App.food1value_3;
            }
            if (App.food1value_4 != -1)
            {
                slider4.Value = App.food1value_4;
            }
            if (App.food1value_5 != -1)
            {
                slider5.Value = App.food1value_5;
            }
            if (App.food1value_6 != -1)
            {
                slider6.Value = App.food1value_6;
            }
            if (App.food1value_7 != -1)
            {
                slider7.Value = App.food1value_7;
            }
            if (App.food1value_8 != -1)
            {
                slider8.Value = App.food1value_8;
            }
            if (App.food1value_9 != -1)
            {
                slider9.Value = App.food1value_9;
            }
        }

        private void Slider1_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider1.Value = newStep * StepValue;
            slider1valuelabel.Text = slider1.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_1", slider1.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        private void Slider2_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider2.Value = newStep * StepValue;
            slider2valuelabel.Text = slider2.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_2", slider2.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        private void Slider3_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider3.Value = newStep * StepValue;
            slider3valuelabel.Text = slider3.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_3", slider3.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        private void Slider4_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider4.Value = newStep * StepValue;
            slider4valuelabel.Text = slider4.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_4", slider4.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        private void Slider5_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider5.Value = newStep * StepValue;
            slider5valuelabel.Text = slider5.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_5", slider5.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        private void Slider6_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider6.Value = newStep * StepValue;
            slider6valuelabel.Text = slider6.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_6", slider6.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        private void Slider7_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider7.Value = newStep * StepValue;
            slider7valuelabel.Text = slider7.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_7", slider7.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        private void Slider8_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider8.Value = newStep * StepValue;
            slider8valuelabel.Text = slider8.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_8", slider8.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        private void Slider9_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var newStep = Math.Round(e.NewValue / StepValue);
            slider9.Value = newStep * StepValue;
            slider9valuelabel.Text = slider9.Value.ToString();
            vegosszeg = slider1.Value * 150 + slider2.Value * 100 + slider3.Value * 80 + slider4.Value * 40 + slider5.Value * 40 + slider6.Value * 40 + slider7.Value * 20 + slider8.Value * 20 + slider9.Value * 20;
            Preferences.Set("food1", vegosszeg.ToString());
            Preferences.Set("food1value_9", slider9.Value.ToString());
            App.food1 = Convert.ToDouble(vegosszeg);
        }

        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();
        }
    }
}