using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace EcoFootprintCalculator
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Challenges : ContentPage
    {
        Label numberofchallenges;
        List<string> activechallenges = new List<string>();
        ListView listview;
        Label numberoffinished;
        Label reduced;
        

        public Challenges()
        {
            InitializeComponent();

            Title = "Challenges";
            
            var scroll = new ScrollView
            {
                //VerticalOptions = LayoutOptions.Center
            };

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

            numberofchallenges = new Label
            {
                FontSize = 16,
                FontAttributes = FontAttributes.Bold
            };

            var header = new Grid
            {
                RowDefinitions =
                {
                    new RowDefinition { Height = new GridLength(20) },
                },
                ColumnDefinitions =
                {
                    new ColumnDefinition{ Width = new GridLength(80) },
                    new ColumnDefinition{ Width = GridLength.Star },
                    new ColumnDefinition{ Width = new GridLength(30) }
                }
            };

            header.Children.Add(new Label { Text = "Regularity" });
            header.Children.Add(new Label { Text = "Task" }, 1, 0);
            header.Children.Add(new Label { Text = "#", HorizontalTextAlignment = TextAlignment.End }, 2, 0);

            var DataTemplate = new DataTemplate(() =>
            {
                var grid = new Grid
                {
                    ColumnDefinitions =
                    {
                        new ColumnDefinition{ Width = new GridLength(80) },
                        new ColumnDefinition{ Width = GridLength.Star },
                        new ColumnDefinition{ Width = new GridLength(30) }
                    }
                };
                var regLabel = new Label { FontAttributes = FontAttributes.Bold };
                var taskLabel = new Label { HorizontalTextAlignment = TextAlignment.Start };
                var counterLabel = new Label { HorizontalTextAlignment = TextAlignment.End };

                regLabel.SetBinding(Label.TextProperty, "regularity");
                taskLabel.SetBinding(Label.TextProperty, "task");
                counterLabel.SetBinding(Label.TextProperty, "counter");

                grid.Children.Add(regLabel);
                grid.Children.Add(taskLabel, 1, 0);
                grid.Children.Add(counterLabel, 2, 0);

                return new ViewCell { View = grid };
            });

            listview = new ListView
            {
                ItemsSource = App.activechallengesO,
                ItemTemplate = DataTemplate
            };

            numberoffinished = new Label
            {
                FontSize = 16,
                FontAttributes = FontAttributes.Bold
            };

            reduced = new Label
            {
                FontSize = 16,
                FontAttributes = FontAttributes.Bold
            };

            layout.Children.Add(numberofchallenges);
            layout.Children.Add(header);
            layout.Children.Add(listview);
            layout.Children.Add(numberoffinished);
            layout.Children.Add(reduced);
            layout.Children.Add(new Label { Text = "" });

            layout.HeightRequest = App.screenheight;

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

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

            numberofchallenges.Text = "There are " + (App.activechallenges.Count > 0 ? App.activechallenges.Count.ToString() : "no") + " active challenges:";

            int finished = Int32.Parse(Preferences.Get("finished", "0"));

            numberoffinished.Text = "You have completed " + finished + " challenges.";

            double challengereduction = Convert.ToDouble(Preferences.Get("challengereduction", "0"));

            double reductioninearths = (App.calculatedecofootprint / 600) - ((App.calculatedecofootprint - challengereduction) / 600);

            reduced.Text = "You have reduced your footprint by " + Math.Round(challengereduction, 2) + " through challenges.";
        }
    }
}