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 Waste7 : ContentPage
    {
        Button nextbutton;
        Slider slider1;
        Label slider1valuelabel;
        double StepValue = 1.0;
        public Waste7()
        {
            InitializeComponent();

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

            slider1 = new Slider()
            {
                Maximum = 1000
            };
            slider1.ValueChanged += Slider1_ValueChanged;
            slider1valuelabel = new Label()
            {
                Text = slider1.Value.ToString()
            };

            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 = "waste7.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 = "46/49 Question" });
            layout.Children.Add(new Label { Text = "Add one point for each Euro you spend in a typical day.", FontSize = 20 });
            layout.Children.Add(slider1);
            layout.Children.Add(slider1valuelabel);
            layout.Children.Add(image);
            layout.Children.Add(nextbutton);
            layout.Children.Add(cancelbutton);

            this.Content = layout;

            App.waste7value = Convert.ToDouble(Preferences.Get("waste7value", "-1"));
            if (App.waste7value != -1)
            {
                slider1.Value = App.waste7value;
            }
        }
        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();
        }

        private void Slider1_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            var newStep = Math.Round(e.NewValue / StepValue);
            slider1.Value = newStep * StepValue;
            slider1valuelabel.Text = slider1.Value.ToString();
            Preferences.Set("waste7", slider1.Value.ToString());
            Preferences.Set("waste7value", slider1.Value.ToString());
            App.waste7 = Convert.ToDouble(slider1.Value);
        }
    }
}