using Microsoft.AspNet.Identity;
using ResourcesManager.Handlers;
using ResourcesManager.Interfaces;
using ResourcesManager.Models;
using ResourcesManager.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace ResourcesManager.Controllers
{
    [Authorize]
    public class SubscribeController : Controller
    {
        private ApplicationDbContext context;
        private IResourceRepository resourceRepo;
        private IUserRepository userRepo;
        private IStatisticRepository statisticRepo;
        readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public SubscribeController()
        {
            context = new ApplicationDbContext();
            resourceRepo = new ResourceRepository(context);
            userRepo = new UserRepository(context);
            statisticRepo = new StatisticRepository(context);
        }

        public SubscribeController(IResourceRepository resouceRepo, IUserRepository userRepo, IStatisticRepository statisticRepository)
        {
            this.resourceRepo = resouceRepo;
            this.userRepo = userRepo;
            this.statisticRepo = statisticRepository;
        }

        public ActionResult Index()
        {
            return View(resourceRepo.GetResouces());
        }

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

                var user = userRepo.GetUserByID(User.Identity.GetUserId());
                var res = resourceRepo.GetResourceByID((int)resourceId);
                res.Users.Add(user);
                resourceRepo.UpdateResouce(res);
                statisticRepo.InsertStatistic(new StatisticModel()
                {
                    ResourceId = (int)resourceId,
                    UserId = User.Identity.GetUserId(),
                    StatisticType = StatisticType.Subscribe,
                    DateTime = DateTime.Now
                });
            }
            catch (Exception e)
            {
                log.Error("DB", e);
            }

            return RedirectToAction("index");
        }


        public ActionResult UnFollowAll()
        {
            try
            {
                var user = userRepo.GetUserByID(User.Identity.GetUserId());
                foreach (var item in resourceRepo.GetResouces())
                {
                    item.Users.Remove(user);
                }
                statisticRepo.InsertStatistic(new StatisticModel()
                {
                    UserId = User.Identity.GetUserId(),
                    StatisticType = StatisticType.SubscribeAll,
                    DateTime = DateTime.Now
                });
            }
            catch (Exception e)
            {
                log.Error("DB", e);
            }

            return RedirectToAction("index");
        }
        
        public ActionResult FollowAll()
        {
            try
            {
                var user = userRepo.GetUserByID(User.Identity.GetUserId());
                foreach (var item in resourceRepo.GetResouces())
                {
                    item.Users.Add(user);
                }
                statisticRepo.InsertStatistic(new StatisticModel()
                {
                    UserId = User.Identity.GetUserId(),
                    StatisticType = StatisticType.UnsubscirbeAll,
                    DateTime = DateTime.Now
                });
            }
            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 = userRepo.GetUserByID(User.Identity.GetUserId());
                var res = resourceRepo.GetResourceByID((int)resourceId);
                res.Users.Remove(user);
                resourceRepo.UpdateResouce(res);
                statisticRepo.InsertStatistic(new StatisticModel()
                {
                    ResourceId = (int)resourceId,
                    UserId = User.Identity.GetUserId(),
                    StatisticType = StatisticType.Unsubscirbe,
                    DateTime = DateTime.Now
                });
            }
            catch (Exception e)
            {
                log.Error("DB", e);
            }

            return RedirectToAction("index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                context.Dispose();
                resourceRepo.Dispose();
                statisticRepo.Dispose();
                userRepo.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}