using Bp.Sync.BackEnd.Factories;
using Hangfire;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Bp.Sync.BackEnd.Controllers
{
    public class JobController : Controller
    {
        #region contruct

        private readonly IConfiguration _configuration;
        private readonly ISyncFactory _syncFactory;
        private readonly IUpdateFactory _updateFactory;
        private readonly IDailyTaskFactory _dailyTaskFactory;
        private readonly ISyncDavTaskFactory _syncDavTaskFactory;

        public JobController(ISyncFactory syncFactory, IUpdateFactory updateFactory, IDailyTaskFactory dailyTaskFactory, ISyncDavTaskFactory syncDavTaskFactory)
        {
            _syncFactory = syncFactory;
            _updateFactory = updateFactory;
            _dailyTaskFactory = dailyTaskFactory;
            _syncDavTaskFactory = syncDavTaskFactory;
        }

        #endregion contruct

        #region SC

        public async Task<IActionResult> ActiveSyncTransaction()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncTransactions(), Cron.MinuteInterval(2));
            return Json("SyncTransaction - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncBill(), Cron.MinuteInterval(2));
            return Json("SyncBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncBatch()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncBatch(), Cron.MinuteInterval(1));
            return Json("SyncBatch - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncOutboundBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncOutboundBill(), Cron.MinuteInterval(1));
            return Json("SyncOutboundBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateOutboundBill()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateOutboundBill(), Cron.MinuteInterval(2));
            return Json("UpdateProductOrder - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateProductOrder()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateProductOrder(), Cron.MinuteInterval(2));
            return Json("UpdateOutboundBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncInboundBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncInboundBill(), Cron.MinuteInterval(2));
            return Json("SyncInboundBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncInventoryBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncInventoryBill(), Cron.MinuteInterval(7));
            return Json("SyncInboundBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateInboundBill()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateInboundBill(), Cron.MinuteInterval(3));
            return Json("UpdateInboundBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateInventoryBill()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateInventoryBill(), Cron.MinuteInterval(10));
            return Json("UpdateInventoryBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncInvoice()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncInvoice(), Cron.MinuteInterval(2));
            return Json("SyncInvoice - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncProduct()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncProduct(), Cron.MinuteInterval(2));
            return Json("SyncProduct - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncProductGroup()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncProductGroup(), Cron.MinuteInterval(30));
            return Json("SyncProductGroup - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncCategory()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncCategory(), Cron.MinuteInterval(30));
            return Json("SyncCategory - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncPromotion()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncPromotion(), Cron.MinuteInterval(5));
            return Json("SyncPromotion - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncPromotionProduct()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncPromotionProduct(), Cron.MinuteInterval(5));
            return Json("SyncPromotionProduct - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncUserDistrict()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncUserDistrict(), Cron.MinuteInterval(10));
            return Json("SyncUserDistrict - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncProductType()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncProductType(), Cron.MinuteInterval(30));
            return Json("SyncProductGroup - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncProductBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncProductBill(), Cron.MinuteInterval(2));
            return Json("SyncProductBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncProductInventory()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncProductInventory(), Cron.MinuteInterval(1));
            return Json("SyncProductInventory - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncProductInvoice()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncProductInvoice(), Cron.MinuteInterval(1));
            return Json("SyncProductInvoice - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncOrder()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncOrder(), Cron.MinuteInterval(1));
            return Json("SyncOrder - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncProductOrder()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncProductOrder(), Cron.MinuteInterval(1));
            return Json("SyncProductOrder - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncRetailBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncRetailBill(), Cron.MinuteInterval(1));
            return Json("SyncRetailBill - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncBranch()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncBranch(), Cron.MinuteInterval(3));
            return Json("SyncBranch - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncBaseConfiguration()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncBaseConfiguration(), Cron.MinuteInterval(20));
            return Json("SyncBranch - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncContractDiscountLevel()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncContractDiscountLevel(), Cron.MinuteInterval(5));
            return Json("SyncContractDiscountLevel - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncDiscountBranchDetailConfig()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncDiscountBranchDetailConfig(), Cron.MinuteInterval(5));
            return Json("SyncDiscountBranchDetailConfig - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncWholesalePriceList()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncWholesalePriceList(), Cron.MinuteInterval(20));
            return Json("SyncWholesalePriceList - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateInvoiceToProductInvoiceCommand()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateInvoiceToProductInvoice(), Cron.MinuteInterval(1));
            return Json("SyncBranch - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateExpiryDateProductInventory()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateExpiryDateProductInventory(), Cron.MinuteInterval(6));
            return Json("SyncBranch - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateProductInventory()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateProductInventory(), Cron.MinuteInterval(3));
            return Json("UpdateProductInventory - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncUnit()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncUnit(), Cron.MinuteInterval(5));
            return Json("SyncUnit - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncReason()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncReason(), Cron.MinuteInterval(5));
            return Json("SyncReason - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncProductUnit()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncProductUnit(), Cron.MinuteInterval(5));
            return Json("SyncUnit - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncCustomer()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncCustomer(), Cron.MinuteInterval(5));
            return Json("SyncCustomer - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncSupplier()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncSupplier(), Cron.MinuteInterval(5));
            return Json("SyncSupplier - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncDebt()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncDebt(), Cron.MinuteInterval(1));
            return Json("SyncDebt - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncGeneralLedger()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncGeneralLedger(), Cron.MinuteInterval(1));
            return Json("SyncGeneralLedger - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncAccount()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncAccount(), Cron.MinuteInterval(11));
            return Json("SyncAccount - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        #endregion SC

        #region FC

        public async Task<IActionResult> ActiveSyncRetailStatistic()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncRetailStatistic(), Cron.MinuteInterval(1));
            return Json("SyncRetailStatistic - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateRetailStatistic()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateRetailStatistic(), Cron.MinuteInterval(3));
            return Json("SyncRetailStatistic - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncInboundStatistic()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncInboundStatistic(), Cron.MinuteInterval(11));
            return Json("SyncRetailStatistic - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcProductInbound()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcProductInbound(), Cron.MinuteInterval(2));
            return Json("SyncFcProductInbound - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncOutboundStatistic()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncOutboundStatistic(), Cron.MinuteInterval(11));
            return Json("SyncRetailStatistic - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcCustomer()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcCustomer(), Cron.MinuteInterval(5));
            return Json("SyncRetailStatistic - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcInboundBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcInboundBill(), Cron.MinuteInterval(11));
            return Json("SyncRetailStatistic - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcOutboundBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcOutboundBill(), Cron.MinuteInterval(12));
            return Json("SyncRetailStatistic - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcReceiptBill()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcReceiptBill(), Cron.MinuteInterval(2));
            return Json("SyncRetailStatistic - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcProductStorage()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcProductStorage(), Cron.MinuteInterval(3));
            return Json("SyncFcProductStorage - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcUnitExchange()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcUnitExchange(), Cron.MinuteInterval(5));
            return Json("FcUnitExchange - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateFcProductStorage()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateFcProductStorage(), Cron.MinuteInterval(5));
            return Json("UpdateFcProductStorage - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateExpiryDateFcProductStorage()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateExpiryDateFcProductStorage(), Cron.MinuteInterval(5));
            return Json("UpdateExpiryDateFcProductStorage - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcProductShipment()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcProductShipment(), Cron.MinuteInterval(1));
            return Json("SyncFcProductStorage - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveSyncFcProductSummary()
        {
            RecurringJob.AddOrUpdate(() => _syncFactory.SyncFcProductSummary(), Cron.MinuteInterval(1));
            return Json("SyncFcProductSummary - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        public async Task<IActionResult> ActiveUpdateRetailStatisticCustomerPhoneNumber()
        {
            RecurringJob.AddOrUpdate(() => _updateFactory.UpdateRetailStatisticCustomerPhoneNumber(), Cron.MinuteInterval(10));
            return Json("SyncFcProductStorage - Đã kích hoạt đồng bộ sang ElasticSearch");
        }

        #endregion FC

        #region Daily task

        public IActionResult ActiveBuildInventoryDiary()
        {
            RecurringJob.AddOrUpdate("BuildInventoryDiary", () => _dailyTaskFactory.BuildInventoryDiary(), Cron.Daily(17)); //0h
            return Json("Đã chạy tự động BuildInventoryDiary");
        }

        #endregion Daily task

        #region Liên thông dược quốc gia

        public IActionResult RetailActiveAutoSyncInboundBillDav()
        {
            RecurringJob.AddOrUpdate("RetailAutoSyncInboundBillDav_0h", () => _syncDavTaskFactory.Retail_AutoSyncInboundBillDav(), Cron.Daily(17)); //0h
            RecurringJob.AddOrUpdate("RetailAutoSyncInboundBillDav_6h", () => _syncDavTaskFactory.Retail_AutoSyncInboundBillDav(), Cron.Daily(23)); //6h
            RecurringJob.AddOrUpdate("RetailAutoSyncInboundBillDav_12h", () => _syncDavTaskFactory.Retail_AutoSyncInboundBillDav(), Cron.Daily(5)); //12h
            RecurringJob.AddOrUpdate("RetailAutoSyncInboundBillDav_18h", () => _syncDavTaskFactory.Retail_AutoSyncInboundBillDav(), Cron.Daily(11)); //18h
            return Json("Đã chạy tự động RetailAutoSyncInboundBillDav");
        }

        public IActionResult RetailActiveAutoSyncOutboundBillDav()
        {
            RecurringJob.AddOrUpdate("RetailAutoSyncOutboundBillDav_1h", () => _syncDavTaskFactory.Retail_AutoSyncOutboundBillDav(), Cron.Daily(18)); //1h
            RecurringJob.AddOrUpdate("RetailAutoSyncOutboundBillDav_7h", () => _syncDavTaskFactory.Retail_AutoSyncOutboundBillDav(), Cron.Daily(0)); //7h
            RecurringJob.AddOrUpdate("RetailAutoSyncOutboundBillDav_13h", () => _syncDavTaskFactory.Retail_AutoSyncOutboundBillDav(), Cron.Daily(6)); //13h
            RecurringJob.AddOrUpdate("RetailAutoSyncOutboundBillDav_19h", () => _syncDavTaskFactory.Retail_AutoSyncOutboundBillDav(), Cron.Daily(12)); //19h
            return Json("Đã chạy tự động RetailAutoSyncOutboundBillDav");
        }

        public IActionResult RetailActiveAutoSyncInvoiceBillDav()
        {
            RecurringJob.AddOrUpdate("RetailAutoSyncInvoiceBillDav_2h", () => _syncDavTaskFactory.Retail_AutoSyncInvoiceBillDav(), Cron.Daily(18)); //2h
            RecurringJob.AddOrUpdate("RetailAutoSyncInvoiceBillDav_8h", () => _syncDavTaskFactory.Retail_AutoSyncInvoiceBillDav(), Cron.Daily(0)); //8h
            RecurringJob.AddOrUpdate("RetailAutoSyncInvoiceBillDav_14h", () => _syncDavTaskFactory.Retail_AutoSyncInvoiceBillDav(), Cron.Daily(6)); //14h
            RecurringJob.AddOrUpdate("RetailAutoSyncInvoiceBillDav_20h", () => _syncDavTaskFactory.Retail_AutoSyncInvoiceBillDav(), Cron.Daily(12)); //20h
            return Json("Đã chạy tự động RetailAutoSyncInvoiceBillDav");
        }

        public IActionResult ActiveSyncProductStorageDav()
        {
            RecurringJob.AddOrUpdate(() => _syncDavTaskFactory.SyncProductStorageDav(), Cron.MinuteInterval(1));
            return Json("Đã chạy tự động ActiveSyncProductStorageDav");
        }

        #endregion Liên thông dược quốc gia
    }
}