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 Waste2 : ContentPage
    {
        Button nextbutton;
        public Waste2()
        {
            InitializeComponent();

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

            var radio1 = new RadioButton()
            {
                Value = -20,
                Content = "Yes"
            };
            radio1.CheckedChanged += Radio1_CheckedChanged;

            var radio2 = new RadioButton()
            {
                Value = 0,
                Content = "No"
            };
            radio2.CheckedChanged += Radio1_CheckedChanged;

            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 = "waste2.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 = "41/49 Question" });
            layout.Children.Add(new Label { Text = "I reuse items rather than throw them out.", FontSize = 20 });
            layout.Children.Add(radio1);
            layout.Children.Add(radio2);
            layout.Children.Add(image);
            layout.Children.Add(nextbutton);
            layout.Children.Add(cancelbutton);

            this.Content = layout;

            App.waste2value = Convert.ToDouble(Preferences.Get("waste2value", "-1"));
            if (App.waste2value == -20)
            {
                radio1.IsChecked = true;
            }
            if (App.waste2value == 0)
            {
                radio2.IsChecked = true;
            }
        }
        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 Radio1_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            nextbutton.IsEnabled = true;
            var rb = (RadioButton)sender;
            Preferences.Set("waste2", rb.Value.ToString());
            Preferences.Set("waste2value", rb.Value.ToString());
            App.waste2 = Convert.ToDouble(rb.Value);
        }
    }
}