using System.Collections.ObjectModel;
using System.Linq;
using JXCMS.CMS.Attribute;
using JXCMS.CMS.Movie.Entity;
using JXCMS.CMS.Movie.Jobs;
using JXCMS.Core.TimingTask;
using Microsoft.AspNetCore.Mvc;
using Quartz;

namespace JXCMS.CMS.Movie.Admin.Controllers
{
    [Area("Admin")]
    [AdminAuthentication]
    public class CollectionController : Controller
    {
        public IActionResult Index(int pageNumber = 1, int pageSize = 20)
        {
            ViewBag.title = "采集地址";
            var website = WebSiteEntity.Select.Count(out long count).Page(pageNumber, pageSize).ToList();
            ViewBag.website = website;
            ViewBag.count = count;
            ViewBag.pageNumber = pageNumber;
            ViewBag.totlePage = count % pageSize == 0 ? count / pageSize : count / pageSize + 1;
            return View();
        }

        [HttpPost]
        public IActionResult AddWebSite(WebSiteEntity webSiteEntity)
        {
            webSiteEntity.Save();
            if (webSiteEntity.IsEnable)
            {
                QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(), "default", ("id", webSiteEntity.Id));
            }
            
            return Redirect(Url.Action("Index"));
        }

        public IActionResult DeleteWebSite(int id)
        {
            WebSiteEntity.Find(id).Delete();
            QuartzTask.Instance().DeleteTask(id.ToString());
            return Redirect(Url.Action("Index"));
        }

        public IActionResult DeleteWebSiteBatch(int[] ids)
        {
            WebSiteEntity.Where(x => ids.Contains(x.Id)).ToDelete().ExecuteAffrows();
            QuartzTask.Instance()
                .DeleteTasks(
                    new ReadOnlyCollection<JobKey>(ids.Select(x => new JobKey(x.ToString(), "default")).ToList()));
            return Redirect(Url.Action("Index"));
        }

        public IActionResult EnableWebSiteBatch(int[] ids)
        {
            var webSiteEntities = WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
                .Set(y => y.IsEnable, true).ExecuteUpdated();
            foreach (var webSiteEntity in webSiteEntities)
            {
                QuartzTask.Instance().AddTask(typeof(CollectionJob), webSiteEntity.Cron, webSiteEntity.Id.ToString(),
                    "default", ("id", webSiteEntity.Id));
            }
            return Redirect(Url.Action("Index"));
        }

        public IActionResult DisableWebSiteBatch(int[] ids)
        {
            WebSiteEntity.Where(x => ids.Contains(x.Id)).ToUpdate()
                .Set(y => y.IsEnable, false).ExecuteAffrows();
            QuartzTask.Instance().DeleteTasks(new ReadOnlyCollection<JobKey>(ids.Select(x => new JobKey(x.ToString(), "default")).ToList()));
            return Redirect(Url.Action("Index"));
        }

        public IActionResult WebSiteDialog(int id)
        {
            WebSiteEntity webSiteEntity = null;
            if (id == 0)
            {
                webSiteEntity = new WebSiteEntity();
                ViewBag.title = "添加新网站";
            }
            else
            {
                webSiteEntity = WebSiteEntity.Find(id);
                ViewBag.title = "修改" + webSiteEntity.WebSiteName;
            }
            return View(webSiteEntity);
        }
        
        
    }
}