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 Photo_Shared.Models;

namespace Photo_Shared.Controllers
{
    [Authorize]
    public class CommentsController : Controller
    {
        private PhotoSharingDBContext db = new PhotoSharingDBContext();

        // GET: Comments
        public ActionResult Index()
        {
            var comments = db.Comments.Include(c => c.photo);
            return View(comments.ToList());
        }

        // GET: Comments/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Comment comment = db.Comments.Find(id);
            if (comment == null)
            {
                return HttpNotFound();
            }
            return View(comment);
        }

        // GET: Comments/Create
        public ActionResult Create(int? id)
        {

            if (id != null)
            {
                var v = db.Photos.Find(id.Value);
                Comment c = new Comment();
                c.PhotoID = id.Value;
                return View(c);
            }
            return View();
            //ViewBag.PhotoID = new SelectList(db.Photos, "PhotoID", "Title");
            //return View();
        }

        // POST: Comments/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "CommentID,Owner,Subject,Body,PhotoID")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                comment.Owner = System.Web.HttpContext.Current.User.Identity.Name;
                
                db.Comments.Add(comment);
                db.SaveChanges();
                return RedirectToAction("Details","Photos", new { id = comment.PhotoID });
            }

            ViewBag.PhotoID = new SelectList(db.Photos, "PhotoID", "Title", comment.PhotoID);
            return View(comment);
        }

        // GET: Comments/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Comment comment = db.Comments.Find(id);
            Photo photo = db.Photos.Find(comment.PhotoID);
            if (photo.Owner != System.Web.HttpContext.Current.User.Identity.Name)
            { 
                return RedirectToAction("Index", "AccessDenied"); 
            }
            if (comment == null)
            {
                return HttpNotFound();
            }
            ViewBag.PhotoID = new SelectList(db.Photos, "PhotoID", "Title", comment.PhotoID);
            return View(comment);
        }

        // POST: Comments/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "CommentID,Owner,Subject,Body,PhotoID")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(comment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PhotoID = new SelectList(db.Photos, "PhotoID", "Title", comment.PhotoID);
            return View(comment);
        }

        // GET: Comments/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Comment comment = db.Comments.Find(id);
            Photo photo = db.Photos.Find(comment.PhotoID);
            if (photo.Owner != System.Web.HttpContext.Current.User.Identity.Name)
            {
                return RedirectToAction("Index", "AccessDenied");
            }
            if (comment == null)
            {
                return HttpNotFound();
            }
            return View(comment);
        }

        // POST: Comments/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Comment comment = db.Comments.Find(id);
            db.Comments.Remove(comment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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