using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TFC.Context;
using TFC.Models;

namespace TFC.Controllers
{ 
    public class TrainingController : Controller
    {
        private DataContext db = new DataContext();
        public static int FindTrainingIdByUserName(String username)
        {
            Training find =
                new DataContext().Training.ToList().Find(
                    t => t.UserId.Equals(UserController.FindUserIdByUsername(username)));

            return find == null ? 0 : find.TrainingID;
        }

        public static int FindTrainingIdByUserId(Guid userId)
        {
            Training find =
                new DataContext().Training.ToList().Find(
                    t => t.UserId.Equals(userId));

            return find == null ? 0 : find.TrainingID;
        }

        //
        // GET: /Training/
        [Authorize(Roles = "Trainer")]
        public ViewResult Index()
        {
            var training = db.Training.Include(t => t.Cliente);
            return View(training.ToList());
        }

        //
        // GET: /Training/Details/5
        [Authorize(Roles = "Trainer")]
        public ViewResult Details(int id)
        {
            List<User> clients = new List<User>();
            List<DateTime> dates = new List<DateTime>();
            int i = 0;
            int trainingCompleted = 0;
            foreach (User u in db.Users)
            {
                if (new CodeFirstRoleProvider().IsUserInRole(u.Username, "Client"))
                    clients.Add(u);
            }

            foreach (Workout w in db.Training.Find(id).workouts)
            {
                if (!dates.Exists(d => w.Date.Equals(d)))
                    dates.Add(w.Date);
            }

            if (!db.Training.Find(id).workouts.Count.Equals(0))
                trainingCompleted = (db.Training.Find(id).workouts.Count(w => w.IsRealized.Equals(true)) * 100) / db.Training.Find(id).workouts.Count;

            dates.Sort();

            ViewBag.TrainingCompleted = trainingCompleted;
            ViewBag.Dates = dates;
            ViewBag.It = i;
            Training Training = db.Training.Find(id);
            return View(Training);
        }

        //
        // GET: /Training/Create
        [Authorize(Roles = "Trainer")]
        public ActionResult Create()
        {
            List<User> clients = new List<User>();
            foreach (User u in db.Users) 
            {
                if (new CodeFirstRoleProvider().IsUserInRole(u.Username, "Client")&& FindTrainingIdByUserId(u.UserId)==0)
                    clients.Add(u);
            }
            ViewBag.UserId = new SelectList(clients, "UserId", "Username");
            return View();
        } 

        //
        // POST: /Training/Create

        [HttpPost]
        [Authorize(Roles = "Trainer")]
        public ActionResult Create(Training Training)
        {
            if (ModelState.IsValid)
            {
                Training.Cliente = db.Users.Find(Training.UserId);
                db.Training.Add(Training);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.UserId = new SelectList(db.Users, "UserId", "Username", Training.UserId);
            return View(Training);
        }
        
        //
        // GET: /Training/Edit/5
        [Authorize]
        public ActionResult Edit(int id)
        {
           

                List<User> clients = new List<User>();
                List<DateTime> dates = new List<DateTime>();
                Training Training = db.Training.Find(id);
                int i = 0;
                int trainingCompleted = 0;
                foreach (User u in db.Users)
                {
                    if (new CodeFirstRoleProvider().IsUserInRole(u.Username, "Client"))
                        clients.Add(u);
                }

                if (Training!=null)
                {
                    foreach (Workout w in db.Training.Find(id).workouts)
                    {
                        if (!dates.Exists(d => w.Date.Equals(d)))
                            dates.Add(w.Date);
                    }
                    if (!db.Training.Find(id).workouts.Count.Equals(0))
                        trainingCompleted = (db.Training.Find(id).workouts.Count(w => w.IsRealized.Equals(true))*100)/db.Training.Find(id).workouts.Count;

                }
                dates.Sort();
            ViewBag.TrainingCompleted = trainingCompleted;
                ViewBag.Dates = dates;
                ViewBag.It = i;
                if(Training!=null)
                    ViewBag.UserId = new SelectList(clients, "UserId", "Username", Training.UserId);
                return View(Training);
            }
        
    

        //
        // POST: /Training/Edit/5

        [HttpPost]
        [Authorize]
        public ActionResult Edit(Training Training)
        {
            if (ModelState.IsValid)
            {
                db.Entry(Training).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserId = new SelectList(db.Users, "UserId", "Username", Training.UserId);
            return View(Training);
        }

        //
        // GET: /Training/Delete/5
        [Authorize(Roles = "Trainer")]
        public ActionResult Delete(int id)
        {
            List<User> clients = new List<User>();
            List<DateTime> dates = new List<DateTime>();
            int i = 0;
            foreach (User u in db.Users)
            {
                if (new CodeFirstRoleProvider().IsUserInRole(u.Username, "Client"))
                    clients.Add(u);
            }

            foreach (Workout w in db.Training.Find(id).workouts)
            {
                if (!dates.Exists(d => w.Date.Equals(d)))
                    dates.Add(w.Date);
            }

            dates.Sort();

            ViewBag.Dates = dates;
            ViewBag.It = i;
            Training Training = db.Training.Find(id);
            ViewBag.UserId = new SelectList(clients, "UserId", "Username", Training.UserId);
            return View(Training);
        }

        //
        // POST: /Training/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Training Training = db.Training.Find(id);
            db.Training.Remove(Training);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}