using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.Serialization.Json;
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 WorkoutEditPage : PhoneApplicationPage
    {
        private Workout workout;
        private WebClient proxy;

        public WorkoutEditPage()
        {
            InitializeComponent();
        }

        private void sendData()
        {
            string url = (Application.Current.Resources["URI_SERVER"] as String) + string.Format("getworkout/{0}/{1}-{2}-{3}-{4}-{5}", workout.WorkoutID, workout.Series, workout.Load, workout.Repetitions, workout.Pause, workout.IsRealized);

            proxy = new WebClient();
            Uri uri = new Uri(url, UriKind.Absolute);

            proxy.OpenReadAsync(uri);
        }

        private void CheckNumericEntry(TextBox textBox)
        {
            string[] invalidCharacters = {"*","#",",","(",")","x","X","-","+"," ","@","."};

            for (int i = 0; i < invalidCharacters.Length; i++ )
            {
                textBox.Text = textBox.Text.Replace(invalidCharacters[i], string.Empty);
            }

            textBox.SelectionStart = textBox.Text.Length;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            workout.Series = textBoxSeries.Text;
            workout.Load = int.Parse(textBoxPeso.Text);
            workout.Repetitions = int.Parse(textBoxRepeticiones.Text);
            workout.Pause = int.Parse(textBoxDescanso.Text);
            workout.IsRealized = (bool) checkBoxRealizado.IsChecked;
            
            sendData();
            NavigationService.GoBack();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            workout = new Workout();
            Exercice ex = new Exercice();
            workout.Exercice = ex;
            string aux;
            NavigationContext.QueryString.TryGetValue("selectedItem", out aux);

            workout.WorkoutID = int.Parse(aux.Split('|')[0]);
            workout.Exercice.Name = (aux.Split('|')[1]);
            workout.Series = aux.Split('|')[2];
            workout.Load = int.Parse(aux.Split('|')[3]);
            workout.Repetitions = int.Parse(aux.Split('|')[4]);
            workout.Pause = int.Parse(aux.Split('|')[5]);
            workout.IsRealized = bool.Parse(aux.Split('|')[6]);

            textBoxSeries.Text = workout.Series;
            textBoxPeso.Text = workout.Load.ToString();
            textBoxRepeticiones.Text = workout.Repetitions.ToString();
            textBoxDescanso.Text = workout.Pause.ToString();
            checkBoxRealizado.IsChecked = workout.IsRealized;
            PageTitle.Text = workout.Exercice.Name;

        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            string message = "Perderá los cambios si no ha guardado. Está seguro de regresar?";

            MessageBoxResult result = MessageBox.Show(message, "Volver", MessageBoxButton.OKCancel);
            if (result.Equals(MessageBoxResult.Cancel))
                e.Cancel = true;
        }


        protected void numericalTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            CheckNumericEntry((TextBox)sender);
        }
    }
}