using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using CARRE_DSS2.Models;
using Microsoft.AspNet.Identity;

namespace CARRE_DSS2.Controllers
{
    public class MeasurementBindingsController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: MeasurementBindings
        [Authorize(Roles = "Admin")]
        public ActionResult Index()
        {
            var bindings = db.MeasurementBindings;
            return View(bindings);
        }

        // GET: MeasurementBindings/Create
        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            ViewBag.Message = "";
            return View();
        }

        // POST: MeasurementBindings/Create
        [HttpPost]
        [Authorize(Roles = "Admin")]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "MeasurementNode,Name,ObservableNode")] MeasurementBinding bind)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (bind.ObservableNode != null && bind.ObservableNode != "")
                    {
                        bool obsExist = db.Observables.Where(o => o.Node == bind.ObservableNode).Any();
                        if (!obsExist)
                        {
                            ViewBag.Message = "NO_OBSV";
                            return View(bind);
                        }
                    }
                    db.MeasurementBindings.Add(bind);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch
                {
                    ViewBag.Message = "ERROR";
                    return View(bind);
                }
            }
            ViewBag.Message = "ERROR";
            return View(bind);
        }



        // GET: MeasurementBindings/Delete/5
        [Authorize(Roles = "Admin")]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            MeasurementBinding bind = db.MeasurementBindings.Where(b => b.Id == id).First();

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

            db.MeasurementBindings.Remove(bind);
            db.SaveChanges();

            return RedirectToAction("Index");
        }

        //// POST: MeasurementBindings/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[Authorize]
        //[ValidateAntiForgeryToken]
        //public ActionResult DeleteConfirmed(string username)
        //{
        //    var userId = User.Identity.GetUserId();

        //    Patient patient = db.Patients.Where(p => p.UserName == username).First();

        //    var user = patient.AttachedExperts.Where(u => u.Id == userId).First();
        //    patient.AttachedExperts.Remove(user);

        //    if (!patient.AttachedExperts.Any())
        //    {
        //        db.Patients.Remove(patient);
        //    }

        //    try
        //    {
        //        db.SaveChanges();
        //    }
        //    catch
        //    {
        //        return RedirectToAction("Delete", new { username = username, error = true });
        //    }

        //    return RedirectToAction("Index");
        //}
    }
}