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 ResourcesManager.Models;
using System.Web.Helpers;
using System.IO;
using ResourcesManager.Interfaces;
using ResourcesManager.Repositories;

namespace ResourcesManager.Controllers
{
    [Authorize]
    public class NotificationsController : Controller
    {
        private ApplicationDbContext context = new ApplicationDbContext();
        private INotificationRepository notifRepo;
        readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public NotificationsController()
        {
            context = new ApplicationDbContext();
            notifRepo = new NotificationRepository(context);
        }

        public NotificationsController(INotificationRepository notifRepo)
        {
            this.notifRepo = notifRepo;
        }

        public ActionResult Readed(int? id, string link = null)
        {
            try
            {
                if (id == null)
                {
                    logger.Warn("BadRequest");
                
                }
                var notif = notifRepo.GetNotificationByID((int)id);
                notif.Readed = true;
                notifRepo.UpdateNotification(notif);
                if (string.IsNullOrEmpty(link) || link == "null")
                {
                    return Redirect(Request.UrlReferrer.ToString());
                }
            }
            catch (Exception ex)
            {
                logger.Error("", ex);
            }
            return Redirect(link);

        }

        public ActionResult GetUnread()
        {
            using (ApplicationDbContext db = new ApplicationDbContext())
            {
                try
                {
                    return Json(notifRepo.GetNotifications().ToList().OrderByDescending(k => k.CreateDate).Where(m => m.Readed == false && (m.User == User.Identity.Name || m.User == null)).ToList(), JsonRequestBehavior.AllowGet);
                }
                catch (Exception e)
                {
                    logger.Warn("hiba", e);
                    return null;
                }
            }
        }
        public ActionResult MakeReadAll()
        {
            try
            {
                var list = notifRepo.GetNotifications().Where(k => k.Readed == false && (k.User == User.Identity.Name || k.User == null)).ToList();
                foreach (var item in list)
                {
                    item.Readed = true;
                    notifRepo.UpdateNotification(item);
                }
            }
            catch (Exception ex)
            {
                logger.Error("", ex);
            }
            return Redirect(HttpContext.Request.UrlReferrer.ToString());
        }

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