using Microsoft.AspNet.Identity;
using ResoursesManager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace ResoursesManager.Controllers
{
    [Authorize]
    public class SubscribeController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();
        readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public ActionResult Index()
        {
            return View(db.Resources.ToList());
        }

        public ActionResult Follow(int? resourceId)
        {
            try
            {
                if (resourceId == null)
                {
                    log.Warn("BadRequest");
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }

                var user = db.Users.Find(User.Identity.GetUserId());
                db.Resources.Find(resourceId).Users.Add(user);
                db.Statistic.Add(new StatisticModel()
                {
                    ResourceId = (int)resourceId,
                    UserId = User.Identity.GetUserId(),
                    StatisticType = StatisticType.Subscribe,
                    DateTime = DateTime.Now
                });
                db.SaveChanges();
            }
            catch (Exception e)
            {
                log.Error("DB", e);
            }

            return RedirectToAction("index");
        }


        public ActionResult UnFollowAll()
        {
            try
            {
                var user = db.Users.Find(User.Identity.GetUserId());
                foreach (var item in db.Resources)
                {
                    item.Users.Remove(user);
                }
                db.Statistic.Add(new StatisticModel()
                {
                    UserId = User.Identity.GetUserId(),
                    StatisticType = StatisticType.SubscribeAll,
                    DateTime = DateTime.Now
                });
                db.SaveChanges();
            }
            catch (Exception e)
            {
                log.Error("DB", e);
            }

            return RedirectToAction("index");
        }
        
        public ActionResult FollowAll()
        {
            try
            {
                var user = db.Users.Find(User.Identity.GetUserId());
                foreach (var item in db.Resources)
                {
                    item.Users.Add(user);
                }
                db.Statistic.Add(new StatisticModel()
                {
                    UserId = User.Identity.GetUserId(),
                    StatisticType = StatisticType.UnsubscirbeAll,
                    DateTime = DateTime.Now
                });
                db.SaveChanges();
            }
            catch (Exception e)
            {
                log.Error("DB", e);
            }

            return RedirectToAction("index");
        }

        public ActionResult Unfollow(int? resourceId)
        {
            try
            {
                if (resourceId == null)
                {
                    log.Warn("BadRequest");
                    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                }
                var user = db.Users.Find(User.Identity.GetUserId());
                db.Resources.Find(resourceId).Users.Remove(user);
                db.Statistic.Add(new StatisticModel()
                {
                    ResourceId = (int)resourceId,
                    UserId = User.Identity.GetUserId(),
                    StatisticType = StatisticType.Unsubscirbe,
                    DateTime = DateTime.Now
                });
                db.SaveChanges();
            }
            catch (Exception e)
            {
                log.Error("DB", e);
            }

            return RedirectToAction("index");
        }

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