using System.Net;
using System.Web.Mvc;
using CD67.FicheCollege.Entity;
using CD67.FicheCollege.Factory;
using CD67.FicheCollege.MVC.Models;
using System.Linq;
using System.Collections.Generic;
using System;

namespace CD67.FicheCollege.MVC.Controllers
{
    public class RestaurationFormulairesController : Controller
    {
        private Entities db = new Entities();

        // GET: RestaurationFormulaires/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            CollegeFactory colFact = new CollegeFactory(db);
            College college = colFact.getById(id.Value);

            if (college == null)
            {
                return HttpNotFound();
            }

            RestaurationFormulaireFactory restoFact = new RestaurationFormulaireFactory(db);
            RestaurationFormulaire restoForm = restoFact.getBy(x => x.College_Id == id.Value);

            // si null on est en mode création
            if (restoForm == null)
                return RedirectToAction("Edit", "RestaurationFormulaires", new { id = college.Id, mode = ModeAcces.Creation });

            AnneeFactory fact = new AnneeFactory(db);
            Annee annee = fact.getById(college.Annee_Id);
            ViewBag.AnneeEnCours = annee.AnneeRentree;

            RestaurationParametreFactory paramFact = new RestaurationParametreFactory(db);
            RestaurationParametre param = paramFact.getBy(x => x.Annee_Id == annee.Id);
            ViewBag.Param = param;

            restoForm.College = college;
            RestaurationFormulairesRepaFactory formRepasFact = new RestaurationFormulairesRepaFactory(db);
            restoForm.RestaurationFormulairesRepas = formRepasFact.getByForm(restoForm.Id);

            int total = formRepasFact.getSumTotal(restoForm.Id);
            ViewBag.SumTotal = total;

            int moyenneTotal = formRepasFact.getMoyTotal(restoForm.Id);
            ViewBag.MoyenneTotal = moyenneTotal;

            RestaurationFormulaireViewModel model = new RestaurationFormulaireViewModel(restoForm, ModeAcces.Lecture);

            return View(model);
        }

        // GET: RestaurationFormulaires/Edit/5
        public ActionResult Edit(int? id, ModeAcces mode)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            CollegeFactory colFact = new CollegeFactory(db);
            College college = colFact.getById(id.Value);

            if (college == null)
            {
                return HttpNotFound();
            }

            RestaurationFormulaireFactory restoFact = new RestaurationFormulaireFactory(db);
            RestaurationFormulaire restoForm = restoFact.getBy(x => x.College_Id == id.Value);

            List<RestaurationFormulairesRepa> listeFormRepas = new List<RestaurationFormulairesRepa>();

            AnneeFactory fact = new AnneeFactory(db);
            Annee annee = fact.getById(college.Annee_Id);
            ViewBag.AnneeEnCours = annee.AnneeRentree;

            RestaurationParametreFactory paramFact = new RestaurationParametreFactory(db);
            RestaurationParametre param = paramFact.getBy(x => x.Annee_Id == annee.Id);
            ViewBag.Param = param;

            RestaurationFormulairesRepaFactory formRepasFact = new RestaurationFormulairesRepaFactory(db);
            RestaurationTypesRepaFactory typeRepasFact = new RestaurationTypesRepaFactory(db);

            if (restoForm == null)
            {
                restoForm = new RestaurationFormulaire();
                restoForm.Id = Guid.NewGuid();
                restoForm.College_Id = college.Id;
                restoForm.Statut = Constants.Statut.EN_COURS_DE_SAISIE.ToString();
                restoForm.College = college;

                foreach (RestaurationTypesRepa listeTypeRepas in param.RestaurationTypesRepas)
                {
                    RestaurationFormulairesRepa getRepas = new RestaurationFormulairesRepa();
                    getRepas.RestaurationFormulaires_Id = restoForm.Id;
                    getRepas.RestaurationTypesRepas_Id = listeTypeRepas.Id;
                    getRepas.RestaurationTypesRepa = typeRepasFact.getById(listeTypeRepas.Id);

                    listeFormRepas.Add(getRepas);
                }

                restoForm.RestaurationFormulairesRepas = listeFormRepas;
            }
            else
            {
                restoForm.College = college;
                restoForm.RestaurationFormulairesRepas = formRepasFact.getByForm(restoForm.Id);
            }

            RestaurationFormulaireViewModel model = new RestaurationFormulaireViewModel(restoForm, mode);

            return View(model);
        }

    // POST: RestaurationFormulaires/Edit/5
    // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
    // plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, ModeAcces mode, RestaurationFormulaire form, List<RestaurationFormulairesRepa> repas)
        {
            RestaurationFormulaireFactory formFact = new RestaurationFormulaireFactory(db);
            RestaurationFormulairesRepaFactory formRepasFact = new RestaurationFormulairesRepaFactory(db);

            CollegeFactory colFact = new CollegeFactory(db);
            College college = colFact.getById(id);
            form.College = college;

            AnneeFactory fact = new AnneeFactory(db);
            Annee annee = fact.getById(college.Annee_Id);
            ViewBag.AnneeEnCours = annee.AnneeRentree;

            RestaurationParametreFactory paramFact = new RestaurationParametreFactory(db);
            RestaurationParametre param = paramFact.getBy(x => x.Annee_Id == annee.Id);
            ViewBag.Param = param;

            if (ModelState.IsValid)
            {
                if (mode == ModeAcces.Creation)
                    formFact.add(ref form);
                else
                    formFact.update(ref form);

                foreach (RestaurationFormulairesRepa listeRepas in repas)
                {
                    RestaurationFormulairesRepa getRepas = listeRepas;

                    if (listeRepas.Id == 0)
                        formRepasFact.add(ref getRepas);
                    else
                        formRepasFact.update(ref getRepas);
                }

                return RedirectToAction("Index", "Restauration", new { annee_id = param.Annee_Id });
            }

            form.RestaurationFormulairesRepas = formRepasFact.getByForm(form.Id);
            RestaurationFormulaireViewModel model = new RestaurationFormulaireViewModel(form, ModeAcces.Modification);

            return View(form);
        }

        // GET: RestaurationFormulaires/Finish/5
        public ActionResult Finish(int? id)
        {
            CollegeFactory colFact = new CollegeFactory(db);
            College college = colFact.getById(id);

            RestaurationFormulaireFactory restoFact = new RestaurationFormulaireFactory(db);
            RestaurationFormulaire restoForm = restoFact.getBy(x => x.College_Id == id.Value);

            restoForm.Statut = Constants.Statut.PROPOSE.ToString();
            restoFact.update(ref restoForm);

            return RedirectToAction("Details", new { id = college.Id });
        }

        // GET: RestaurationFormulaires/Input/5
        public ActionResult Input(int? id)
        {
            CollegeFactory colFact = new CollegeFactory(db);
            College college = colFact.getById(id);

            RestaurationFormulaireFactory restoFact = new RestaurationFormulaireFactory(db);
            RestaurationFormulaire restoForm = restoFact.getBy(x => x.College_Id == id.Value);

            restoForm.Statut = Constants.Statut.EN_COURS_DE_SAISIE.ToString();
            restoFact.update(ref restoForm);

            return RedirectToAction("Details", new { id = college.Id });
        }

        // GET: RestaurationFormulaires/Validate/5
        public ActionResult Validate(int? id)
        {
            CollegeFactory colFact = new CollegeFactory(db);
            College college = colFact.getById(id);

            RestaurationFormulaireFactory restoFact = new RestaurationFormulaireFactory(db);
            RestaurationFormulaire restoForm = restoFact.getBy(x => x.College_Id == id.Value);

            restoForm.Statut = Constants.Statut.VALIDE.ToString();
            restoFact.update(ref restoForm);

            return RedirectToAction("Details", new { id = college.Id });
        }

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