using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

namespace EcoFootprintCalculator
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Events : ContentPage
    {
        Button representativebutton;
        double whatif_ecofootprint;
        double whatif_reductionfromtips;
        double remainder;
        Label tip1label;
        Label tip2label;
        Label tip3label;
        Label tip4label;
        Label tip5label;
        Slider tip1slider;
        Slider tip2slider;
        Slider tip3slider;
        Slider tip4slider;
        Slider tip5slider;
        Label tip1sliderlabel;
        Label tip2sliderlabel;
        Label tip3sliderlabel;
        Label tip4sliderlabel;
        Label tip5sliderlabel;
        Button challengeButton;

        CheckBox checkbox1;
        double StepValue = 1.0;

        public Events()
        {
            InitializeComponent();

            Title = "What if...";

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

            whatif_ecofootprint = App.calculatedecofootprint;
            whatif_reductionfromtips = App.reductionfromtips;
            remainder = 0;

            representativebutton = new Button
            {
                Scale = 1.0,
                IsEnabled = false,
                FontAttributes = FontAttributes.Bold,
                FontSize = 22
            };

            tip1label = new Label
            {
                Text = App.chosentips[0].tip,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };
            tip1slider = new Slider
            {
                Maximum = App.chosentips[0].regularities.Length - 1
            };
            tip1slider.ValueChanged += Tip1slider_ValueChanged;
            tip1sliderlabel = new Label
            {
                Text = App.chosentips[0].regularities[(int)tip1slider.Value].ToString()
            };

            tip2label = new Label
            {
                Text = App.chosentips[1].tip,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };
            tip2slider = new Slider
            {
                Maximum = App.chosentips[1].regularities.Length - 1
            };
            tip2sliderlabel = new Label
            {
                Text = App.chosentips[1].regularities[(int)tip2slider.Value].ToString()
            };
            tip2slider.ValueChanged += Tip2slider_ValueChanged;

            tip3label = new Label
            {
                Text = App.chosentips[2].tip,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };
            tip3slider = new Slider
            {
                Maximum = App.chosentips[2].regularities.Length - 1
            };
            tip3sliderlabel = new Label
            {
                Text = App.chosentips[2].regularities[(int)tip3slider.Value].ToString()
            };
            tip3slider.ValueChanged += Tip3slider_ValueChanged;

            tip4label = new Label
            {
                Text = App.chosentips[3].tip,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };
            tip4slider = new Slider
            {
                Maximum = App.chosentips[3].regularities.Length - 1
            };
            tip4sliderlabel = new Label
            {
                Text = App.chosentips[3].regularities[(int)tip4slider.Value].ToString()
            };
            tip4slider.ValueChanged += Tip4slider_ValueChanged;

            tip5label = new Label
            {
                Text = App.chosentips[4].tip,
                FontAttributes = FontAttributes.Bold,
                FontSize = 16
            };
            tip5slider = new Slider
            {
                Maximum = App.chosentips[4].regularities.Length - 1
            };
            tip5sliderlabel = new Label
            {
                Text = App.chosentips[4].regularities[(int)tip5slider.Value].ToString()
            };
            tip5slider.ValueChanged += Tip5slider_ValueChanged;

            challengeButton = new Button
            {
                Text = "Create Challenge",
                VerticalOptions = LayoutOptions.EndAndExpand
            };
            challengeButton.Clicked += ChallengeButton_Clicked;

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

            var checkbox1label = new Label
            {
                Text = "with the remainder of the active challenges",
                FontAttributes = FontAttributes.Bold,
                FontSize = 16,
                VerticalOptions = LayoutOptions.Center
            };

            checkbox1 = new CheckBox
            {
                TranslationX = 10
            };
            checkbox1.CheckedChanged += Checkbox1_CheckedChanged;


            grid.Children.Add(checkbox1);
            grid.Children.Add(checkbox1label, 1, 0);

            layout.Children.Add(new Label { Text = "" });
            layout.Children.Add(new Label { Text = "Your ecological footprint could be: ", FontSize = 16, FontAttributes = FontAttributes.Bold });
            layout.Children.Add(representativebutton);
            layout.Children.Add(new Label { Text = "if you would do these tasks regularly for a year:", FontSize = 16, FontAttributes = FontAttributes.Bold, HorizontalTextAlignment = TextAlignment.End });
            //layout.Children.Add(new Label { Text = "" });
            layout.Children.Add(tip1label);
            layout.Children.Add(tip1slider);
            layout.Children.Add(tip1sliderlabel);
            layout.Children.Add(tip2label);
            layout.Children.Add(tip2slider);
            layout.Children.Add(tip2sliderlabel);
            layout.Children.Add(tip3label);
            layout.Children.Add(tip3slider);
            layout.Children.Add(tip3sliderlabel);
            layout.Children.Add(tip4label);
            layout.Children.Add(tip4slider);
            layout.Children.Add(tip4sliderlabel);
            layout.Children.Add(tip5label);
            layout.Children.Add(tip5slider);
            layout.Children.Add(tip5sliderlabel);
            layout.Children.Add(grid);
            //layout.Children.Add(new Label { Text = "" });
            layout.Children.Add(new Label { Text = "Set the sliders to see how these tasks can affect your ecofootprint with different regularity!", FontSize = 16, FontAttributes = FontAttributes.Bold, HorizontalTextAlignment = TextAlignment.Center });
            layout.Children.Add(new Label { Text = "And create a challenge with a setting you would undertake", FontSize = 16, FontAttributes = FontAttributes.Bold, HorizontalTextAlignment = TextAlignment.Center });
            layout.Children.Add(challengeButton);
            layout.Children.Add(new Label { Text = "" });

            this.Content = layout;
        }

        private void Checkbox1_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            if (e.Value)
            {
                foreach (activechallenge i in App.activechallenges)
                {
                    remainder += ((int)i.regularity - i.counter) * i.task_value;
                }
            }
            else
            {
                remainder = 0;
            }

            whatif_reductionfromtips = App.reductionfromtips + (int)App.chosentips[0].regularities[(int)tip1slider.Value] * App.chosentips[0].point + (int)App.chosentips[1].regularities[(int)tip2slider.Value] * App.chosentips[1].point + (int)App.chosentips[2].regularities[(int)tip3slider.Value] * App.chosentips[2].point + (int)App.chosentips[3].regularities[(int)tip4slider.Value] * App.chosentips[3].point + (int)App.chosentips[4].regularities[(int)tip5slider.Value] * App.chosentips[4].point + remainder;
            whatif_reductionfromtips = whatif_reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : whatif_reductionfromtips;
            representativebutton.Text = Math.Round((whatif_ecofootprint - whatif_reductionfromtips) / 600.0, 1).ToString();
            representativebutton.ScaleX = Math.Round(((whatif_ecofootprint - whatif_reductionfromtips) / 600) / ((App.calculatedecofootprint - App.reductionfromtips) / 600), 1);
        }

        private async void ChallengeButton_Clicked(object sender, EventArgs e)
        {
            if (tip1slider.Value == 0 && tip2slider.Value == 0 && tip3slider.Value == 0 && tip4slider.Value == 0 && tip5slider.Value == 0)
            {
                DisplayAlert("No challenge selected", "Please set at least one slider off of \"never\"!", "Ok");
            }
            else
            {
                bool answer = await DisplayAlert("Create challenge", "Are you sure you want to create a new challenge with these settings?", "Yes", "No");
                if (answer)
                {
                    if (tip1slider.Value != 0)
                    {
                        App.tempchallenges.Add(new activechallenge(App.chosentips[0].regularities[(int)tip1slider.Value], App.chosentips[0].tip, App.chosentips[0].point, 1, 0));
                    }
                    if (tip2slider.Value != 0)
                    {
                        App.tempchallenges.Add(new activechallenge(App.chosentips[1].regularities[(int)tip2slider.Value], App.chosentips[1].tip, App.chosentips[1].point, 1, 0));
                    }
                    if (tip3slider.Value != 0)
                    {
                        App.tempchallenges.Add(new activechallenge(App.chosentips[2].regularities[(int)tip3slider.Value], App.chosentips[2].tip, App.chosentips[2].point, 1, 0));
                    }
                    if (tip4slider.Value != 0)
                    {
                        App.tempchallenges.Add(new activechallenge(App.chosentips[3].regularities[(int)tip4slider.Value], App.chosentips[3].tip, App.chosentips[3].point, 1, 0));
                    }
                    if (tip5slider.Value != 0)
                    {
                        App.tempchallenges.Add(new activechallenge(App.chosentips[4].regularities[(int)tip5slider.Value], App.chosentips[4].tip, App.chosentips[4].point, 1, 0));
                    }
                    
                    int deleted = App.tempchallenges.RemoveAll(item => App.activechallenges.Exists(aitem => aitem.task == item.task));

                    if (App.tempchallenges.Count > 0)
                    {
                        App.activechallenges.AddRange(App.tempchallenges);
                        
                        foreach (activechallenge i in App.tempchallenges)
                        {
                            App.activechallengesO.Add(i);
                        }

                        using (var writer = new StreamWriter(App.activechallangejson))
                        {
                            writer.Write(JsonConvert.SerializeObject(App.activechallenges));
                        }
                    }

                    string deletestring = "";
                    if (deleted > 0)
                    {
                        deletestring = "\n" + deleted + " challenges already exists!";
                    }

                    await DisplayAlert("Challeneges created", App.tempchallenges.Count + " new challenge created!" + deletestring, "Ok");

                    App.tempchallenges.Clear();
                }
            }
        }

        private void Tip5slider_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            var newStep = Math.Round(e.NewValue / StepValue);
            tip5slider.Value = newStep * StepValue;
            tip5sliderlabel.Text = App.chosentips[4].regularities[(int)tip5slider.Value].ToString();
            whatif_reductionfromtips = App.reductionfromtips + (int)App.chosentips[0].regularities[(int)tip1slider.Value] * App.chosentips[0].point + (int)App.chosentips[1].regularities[(int)tip2slider.Value] * App.chosentips[1].point + (int)App.chosentips[2].regularities[(int)tip3slider.Value] * App.chosentips[2].point + (int)App.chosentips[3].regularities[(int)tip4slider.Value] * App.chosentips[3].point + (int)App.chosentips[4].regularities[(int)tip5slider.Value] * App.chosentips[4].point + remainder;
            whatif_reductionfromtips = whatif_reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : whatif_reductionfromtips;
            representativebutton.Text = Math.Round((whatif_ecofootprint - whatif_reductionfromtips) / 600.0, 1).ToString();
            representativebutton.ScaleX = Math.Round(((whatif_ecofootprint - whatif_reductionfromtips) / 600) / ((App.calculatedecofootprint - App.reductionfromtips) / 600), 1);
        }

        private void Tip4slider_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            var newStep = Math.Round(e.NewValue / StepValue);
            tip4slider.Value = newStep * StepValue;
            tip4sliderlabel.Text = App.chosentips[3].regularities[(int)tip4slider.Value].ToString();
            whatif_reductionfromtips = App.reductionfromtips + (int)App.chosentips[0].regularities[(int)tip1slider.Value] * App.chosentips[0].point + (int)App.chosentips[1].regularities[(int)tip2slider.Value] * App.chosentips[1].point + (int)App.chosentips[2].regularities[(int)tip3slider.Value] * App.chosentips[2].point + (int)App.chosentips[3].regularities[(int)tip4slider.Value] * App.chosentips[3].point + (int)App.chosentips[4].regularities[(int)tip5slider.Value] * App.chosentips[4].point + remainder;
            whatif_reductionfromtips = whatif_reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : whatif_reductionfromtips;
            representativebutton.Text = Math.Round((whatif_ecofootprint - whatif_reductionfromtips) / 600.0, 1).ToString();
            representativebutton.ScaleX = Math.Round(((whatif_ecofootprint - whatif_reductionfromtips) / 600) / ((App.calculatedecofootprint - App.reductionfromtips) / 600), 1);
        }

        private void Tip3slider_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            var newStep = Math.Round(e.NewValue / StepValue);
            tip3slider.Value = newStep * StepValue;
            tip3sliderlabel.Text = App.chosentips[2].regularities[(int)tip3slider.Value].ToString();
            whatif_reductionfromtips = App.reductionfromtips + (int)App.chosentips[0].regularities[(int)tip1slider.Value] * App.chosentips[0].point + (int)App.chosentips[1].regularities[(int)tip2slider.Value] * App.chosentips[1].point + (int)App.chosentips[2].regularities[(int)tip3slider.Value] * App.chosentips[2].point + (int)App.chosentips[3].regularities[(int)tip4slider.Value] * App.chosentips[3].point + (int)App.chosentips[4].regularities[(int)tip5slider.Value] * App.chosentips[4].point + remainder;
            whatif_reductionfromtips = whatif_reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : whatif_reductionfromtips;
            representativebutton.Text = Math.Round((whatif_ecofootprint - whatif_reductionfromtips) / 600.0, 1).ToString();
            representativebutton.ScaleX = Math.Round(((whatif_ecofootprint - whatif_reductionfromtips) / 600) / ((App.calculatedecofootprint - App.reductionfromtips) / 600), 1);
        }

        private void Tip2slider_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            var newStep = Math.Round(e.NewValue / StepValue);
            tip2slider.Value = newStep * StepValue;
            tip2sliderlabel.Text = App.chosentips[1].regularities[(int)tip2slider.Value].ToString();
            whatif_reductionfromtips = App.reductionfromtips + (int)App.chosentips[0].regularities[(int)tip1slider.Value] * App.chosentips[0].point + (int)App.chosentips[1].regularities[(int)tip2slider.Value] * App.chosentips[1].point + (int)App.chosentips[2].regularities[(int)tip3slider.Value] * App.chosentips[2].point + (int)App.chosentips[3].regularities[(int)tip4slider.Value] * App.chosentips[3].point + (int)App.chosentips[4].regularities[(int)tip5slider.Value] * App.chosentips[4].point + remainder;
            whatif_reductionfromtips = whatif_reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : whatif_reductionfromtips;
            representativebutton.Text = Math.Round((whatif_ecofootprint - whatif_reductionfromtips) / 600.0, 1).ToString();
            representativebutton.ScaleX = Math.Round(((whatif_ecofootprint - whatif_reductionfromtips) / 600) / ((App.calculatedecofootprint - App.reductionfromtips) / 600), 1);
        }

        private void Tip1slider_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            var newStep = Math.Round(e.NewValue / StepValue);
            tip1slider.Value = newStep * StepValue;
            tip1sliderlabel.Text = App.chosentips[0].regularities[(int)tip1slider.Value].ToString();
            whatif_reductionfromtips = App.reductionfromtips + (int)App.chosentips[0].regularities[(int)tip1slider.Value] * App.chosentips[0].point + (int)App.chosentips[1].regularities[(int)tip2slider.Value] * App.chosentips[1].point + (int)App.chosentips[2].regularities[(int)tip3slider.Value] * App.chosentips[2].point + (int)App.chosentips[3].regularities[(int)tip4slider.Value] * App.chosentips[3].point + (int)App.chosentips[4].regularities[(int)tip5slider.Value] * App.chosentips[4].point + remainder;
            whatif_reductionfromtips = whatif_reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : whatif_reductionfromtips;
            representativebutton.Text = Math.Round((whatif_ecofootprint - whatif_reductionfromtips) / 600.0, 1).ToString();
            representativebutton.ScaleX = Math.Round(((whatif_ecofootprint - whatif_reductionfromtips) / 600) / ((App.calculatedecofootprint - App.reductionfromtips) / 600), 1);
        }

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

            whatif_ecofootprint = App.calculatedecofootprint;
            whatif_reductionfromtips = App.reductionfromtips;

            whatif_reductionfromtips = App.reductionfromtips + (int)App.chosentips[0].regularities[(int)tip1slider.Value] * App.chosentips[0].point + (int)App.chosentips[1].regularities[(int)tip2slider.Value] * App.chosentips[1].point + (int)App.chosentips[2].regularities[(int)tip3slider.Value] * App.chosentips[2].point + (int)App.chosentips[3].regularities[(int)tip4slider.Value] * App.chosentips[3].point + (int)App.chosentips[4].regularities[(int)tip5slider.Value] * App.chosentips[4].point + remainder;
            whatif_reductionfromtips = whatif_reductionfromtips >= App.calculatedecofootprint ? App.calculatedecofootprint - App.calculatedecofootprint * 0.1 : whatif_reductionfromtips;
            representativebutton.Text = App.calculatedecofootprint > 0 ? Math.Round((whatif_ecofootprint - whatif_reductionfromtips) / 600.0, 1).ToString() : "Start calculation on Home";
            representativebutton.ScaleX = App.calculatedecofootprint > 0 ? Math.Round(((whatif_ecofootprint - whatif_reductionfromtips) / 600) / ((App.calculatedecofootprint - App.reductionfromtips) / 600), 1) : 1.0;

            //representativebutton.Text = App.calculatedecofootprint > 0 ? Math.Round((whatif_ecofootprint - whatif_reductionfromtips) / 600.0, 1).ToString() : "Start calculation on Home";
            challengeButton.IsEnabled = App.calculatedecofootprint > 0;
            checkbox1.IsEnabled = App.calculatedecofootprint > 0 && App.activechallenges.Count > 0;
            tip1slider.IsEnabled = App.calculatedecofootprint > 0;
            tip2slider.IsEnabled = App.calculatedecofootprint > 0;
            tip3slider.IsEnabled = App.calculatedecofootprint > 0;
            tip4slider.IsEnabled = App.calculatedecofootprint > 0;
            tip5slider.IsEnabled = App.calculatedecofootprint > 0;
        }
    }
}