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 WorkoutController : Controller
    {
        private DataContext db = new DataContext();

        //
        // GET: /Workout/Details/5
        [Authorize]
        public ViewResult Details(int id)
        {
            Workout workout = db.Workout.Find(id);
            return View(workout);
        }

        //
        // GET: /Workout/Create
        [Authorize(Roles = "Trainer")]
        public ActionResult Create(int trainingId)
        {
            ViewData["days"] = 0;
            ViewBag.ExerciceID = new SelectList(db.Exercice, "ExerciceID", "Name");
            ViewBag.TrainingID = trainingId;
            return View();
        } 

        //
        // POST: /Workout/Create
        [HttpPost]
        public ActionResult Create(Workout workout, int days, int sessions)
        {
            if (ModelState.IsValid)
            {
                if (days.Equals(0))
                {
                    db.Workout.Add(workout);
                    db.SaveChanges();
                    return RedirectToAction("Edit", "Training", new {id = workout.TrainingID});
                }
                else
                {
                    for(int i=0; i< sessions; i++)
                    {
                        db.Workout.Add(workout);
                        db.SaveChanges();
                        workout.Date = workout.Date.AddDays(days);

                    }
                }
                return RedirectToAction("Edit", "Training", new { id = workout.TrainingID });

            }

            ViewBag.ExerciceID = new SelectList(db.Exercice, "ExerciceID", "Name", workout.ExerciceID);
            return View(workout);
        }
        
        //
        // GET: /Workout/Edit/5
        public ActionResult Edit(int id, int trainingId)
        {
            Workout workout = db.Workout.Find(id);
            ViewBag.ExerciceID = new SelectList(db.Exercice, "ExerciceID", "Name", workout.ExerciceID);
            ViewBag.TrainingID = trainingId;
            return View(workout);
        }

        //
        // POST: /Workout/Edit/5
        [HttpPost]
        public ActionResult Edit(Workout workout)
        {

            if (ModelState.IsValid)
            {
                
                db.Entry(workout).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Edit", "Training", new { id = workout.TrainingID });  
            }

            ViewBag.TrainingID = new SelectList(db.Training, "TrainingID", "TrainingName", workout.TrainingID);
            return View(workout);
        }

        //
        // GET: /Workout/Delete/5
        [Authorize(Roles = "Trainer")]
        public ActionResult Delete(int id)
        {
            Workout workout = db.Workout.Find(id);
            return View(workout);
        }

        //
        // POST: /Workout/Delete/5

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

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