using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using TP02Photo_sharing.Models;

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

        // GET: /Comments/
        public ActionResult Index()
        {
            return View(db.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);
                Image image = PhotosController.ByteArrayToImagebyMemoryStream(v.PhotoFile);
                string PhyPath = Server.MapPath("~") + @"\images\image.jpg";
                string path = Request.Url.GetLeftPart(UriPartial.Authority) + @"\images\image.jpg";
                image.Save(PhyPath);
                ViewBag.IMAGE = path;
                Comment c = new Comment();
                c.PhotoID = id.Value;
                return View(c);
            }
            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 = "ID,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("Index", "Photos", new { id = 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);
            if (comment.Owner != System.Web.HttpContext.Current.User.Identity.Name)
            { return RedirectToAction("Index", "NonAuth"); }
            if (comment == null)
            {
                return HttpNotFound();
            }
            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 = "ID,Subject,Body,PhotoID")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(comment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", "Photos", new {id= 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);
            
            if (comment == null)
            {
                return HttpNotFound();
            }
            if (comment.Owner != System.Web.HttpContext.Current.User.Identity.Name && db.Photos.Find(comment.PhotoID).Owner != System.Web.HttpContext.Current.User.Identity.Name)
            { return RedirectToAction("Index", "NonAuth"); }
            return View(comment);
        }

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

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