using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TFC_ClientApp.Models;

namespace TFC_ClientApp
{
    public partial class PivotPage1 : PhoneApplicationPage
    {
        private WebClient proxy;
        private List<Models.Workout> workouts;
        
        public PivotPage1()
        {
            InitializeComponent();
            this.Loaded += (sender,e) =>
                               {
                                   GetData();
                               };    
        }


        public List<DateTime> getDates()
        {
            List<DateTime> dates = new List<DateTime>(); 
            
            foreach (Models.Workout w in workouts)
            {
                if (!dates.Contains(w.Date))
                    dates.Add(w.Date);
            }

            return dates;
        }

        private void GetData()
        {
            
            User user = (User)DataContext;
            string url = (Application.Current.Resources["URI_SERVER"] as String) + "GetTrainingByUserId/" + user.UserId;

            proxy = new WebClient();
            proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);
            Uri uri = new Uri(url, UriKind.Absolute);

            proxy.OpenReadAsync(uri);
        }

        void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                int trainingCompleted = 0;
                Stream strm = e.Result;
                DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(List<Workout>));
                workouts = s.ReadObject(strm) as List<Models.Workout>;

               this.MainListBox.ItemsSource = getDates();
               if (!workouts.Count.Equals(0))
                   trainingCompleted = (workouts.Count(w => w.IsRealized.Equals(true)) * 100) / workouts.Count;

                textBlock13.Text = "" + trainingCompleted.ToString() + " %";
            }
            else
            {
                MessageBox.Show("Ha ocurrido un error", "Vuelva a intentarlo", MessageBoxButton.OK);

            }
        }

        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            User user = (User)DataContext;
            // Si el índice seleccionado es -1 (no hay selección) no hacer nada
            if (MainListBox.SelectedIndex == -1)
                return;

            // Navegar a la página siguiente
            NavigationService.Navigate(new Uri("/WorkoutPage.xaml?selectedItem=" + MainListBox.SelectedValue + "|"+user.UserId , UriKind.Relative));

            // Restablecer el índice seleccionado en -1 (no hay selección)
            MainListBox.SelectedIndex = -1;
        }
   
    }
}