using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TFC.Models;
using TFC.Context;
namespace TFC.Controllers
{
    public class ServicesController : ApiController
    {
        private DataContext db = new DataContext();

        public List<Workout> GetWorkoutsByDateAndUserId(Guid id, String pass)
        {
            Training training = db.Training.ToList().Find(t => t.UserId.Equals(id));
            var workouts = new List<Workout>();

            foreach (var workout in db.Workout)
            {
                if (workout.TrainingID.Equals(training.TrainingID) && workout.Date.ToShortDateString().Replace('/','-').Equals(pass))
                    workouts.Add(workout);
            }

            return workouts;
        }

        public List<Workout> GetTrainingByUserId(Guid id)
        {
            //db.Configuration.LazyLoadingEnabled = false;
            Training training = db.Training.ToList().Find(t => t.UserId.Equals(id));
            var workouts = new List<Workout>();
            
            foreach (var workout in db.Workout)
            {
                if(workout.TrainingID.Equals(training.TrainingID))
                    workouts.Add(workout);
            }
            //if (training == null)
            //    throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            
            return workouts;
        }

        public User GetUser(String id, string pass)
        {
            if (new CodeFirstMembershipProvider().ValidateUser(id, pass))
            {
                db.Configuration.LazyLoadingEnabled = false;
                var users = new DataContext().Users.ToList();
                var user = users.Find(u => u.Username == id);
   
                return user;
            }
            else
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
        }

        public IEnumerable<Workout> GetWorkoutsByTrainingId(int id)
        {
            var workouts = new List<Workout>(); 

            db.Configuration.LazyLoadingEnabled = false;
            foreach (var workout in db.Workout)
            {
               if(db.Workout.ToList().Exists(w => workout.TrainingID.Equals(id)))
                   workouts.Add(workout);
            }

            foreach (var workout in workouts)
            {
                workout.Exercice = db.Exercice.ToList().Find(e => e.ExerciceID.Equals(workout.ExerciceID));
                workout.Exercice.VideoPath = "";
            }

            if (workouts.Capacity.Equals(0))         
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            

            return workouts;
        }

        public List<Workout> GetWorkout(int id, string pass)
        {
            Workout workout = db.Workout.Find(id);
            workout.Series = pass.Split('-')[0];
            workout.Load = int.Parse(pass.Split('-')[1]);
            workout.Repetitions = int.Parse(pass.Split('-')[2]);
            workout.Pause = int.Parse(pass.Split('-')[3]);
            workout.IsRealized = bool.Parse(pass.Split('-')[4]);
            
            db.Entry(workout).State = EntityState.Modified;
            db.SaveChanges();

            return new List<Workout>();
        }
    }
}