import delay from 'delay';
import { Message } from 'view-design';
import i18n from '@/vue/i18n';
import { createFlashsaleFromSchedulerShopId } from '@/service/shopee/flashsale/scheduler';
import { createScheduleFlashsaleFromCampaignShopId } from '@/service/shopee/flashsale/campaign';
import { shopeeSetting } from '@/plugins/setting';

/** @type {VuexStoreModule} */

export const flashsaleStore = {
  state() {
    return {
      listCampaign: [],
      listFlashsale: {
        scheduler: [],
        save: [],
        current: [],
        next: [],
        end: [],
      },
      schedulerJob: {
        auto: shopeeSetting.shopeeFlashsaleSchedulerAuto.get(),
        log: '',
        running: false,
      },
      campaignJob: {
        auto: shopeeSetting.shopeeFlashsaleCampaignAuto.get(),
        log: '',
        running: false,
      },
    };
  },
  mutations: {
    SET_SHOPEE_FLASHSALE_LIST(state, { typeSet, listSet }) {
      state.listFlashsale[typeSet] = listSet;
    },
    SET_SHOPEE_FLASHSALE_CAMPAIGN(state, listCampaign) {
      state.listCampaign = listCampaign;
    },
    SET_SHOPEE_FLASHSALE_SCHEDULER_LOG(state, log) {
      state.schedulerJob.log = log;
    },
    SET_SHOPEE_FLASHSALE_CAMPAIGN_LOG(state, log) {
      state.schedulerJob.log = log;
    },
    SET_SHOPEE_FLASHSALE_SCHEDULER_RUNNING(state, running) {
      state.schedulerJob.running = running;
    },
    SET_SHOPEE_FLASHSALE_CAMPAIGN_RUNNING(state, running) {
      state.campaignJob.running = running;
    },
    SET_SHOPEE_FLASHSALE_SCHEDULER_AUTO(state, auto) {
      state.schedulerJob.auto = auto;
      shopeeSetting.shopeeFlashsaleSchedulerAuto.set(auto);
    },
    SET_SHOPEE_FLASHSALE_CAMPAIGN_AUTO(state, auto) {
      state.campaignJob.auto = auto;
      shopeeSetting.shopeeFlashsaleCampaignAuto.set(auto);
    },
  },
  getters: {
    getShopeeFlashsaleList(state) {
      return state.listFlashsale;
    },
    getShopeeFlashsaleCampagin(state) {
      return state.listCampaign;
    },
    getShopeeFlashsaleSchedulerJob(state) {
      return state.schedulerJob;
    },
    getShopeeFlashsaleCampaignJob(state) {
      return state.campaignJob;
    },
  },
  actions: {
    FetchShopeeFlashsaleList() { },
    SetShopeeFlashsaleList({ commit }, { listSet, typeSet }) {
      if (!(typeSet && listSet)) {
        return null;
      }

      return commit('SET_SHOPEE_FLASHSALE_LIST', { listSet, typeSet });
    },
    SetShopeeFlashsaleCampagin({ commit }, listCampaign) {
      return commit('SET_SHOPEE_FLASHSALE_CAMPAIGN', listCampaign);
    },
    SetShopeeFlashsaleCampaignAuto({ commit, dispatch }, auto) {
      if (auto) {
        dispatch('RunShopeeFlashsaleAutoCampaign');
      }

      return commit('SET_SHOPEE_FLASHSALE_CAMPAIGN_AUTO', auto);
    },
    SetShopeeFlashsaleSchedulerAuto({ commit, dispatch }, auto) {
      if (auto) {
        dispatch('RunShopeeFlashsaleAutoScheduler');
      }

      dispatch('FetchShopeeFlashsaleList', 'scheduler');

      return commit('SET_SHOPEE_FLASHSALE_SCHEDULER_AUTO', auto);
    },
    async RunShopeeFlashsaleAutoCampaign({
      state, getters, commit, dispatch,
    }) {
      const shopId = getters.getShopeeShopId;
      const { campaignJob } = state;

      if (!(shopId && !campaignJob.running)) {
        return false;
      }

      if (!campaignJob.auto) {
        commit('SET_SHOPEE_FLASHSALE_CAMPAIGN_LOG', i18n.t('FLASHSALE_CAMPAIGN_LOG_PAUSED'));
        return false;
      }

      commit('SET_SHOPEE_FLASHSALE_CAMPAIGN_RUNNING', true);

      /**
       * @param {{flashsaleScheduler: FlashSaleListScheduler, current: number, total: number}} payload
       */
      const callbackScheduler = (payload) => {
        const {
          flashsaleScheduler,
          current,
          total,
        } = payload;

        const logMessage = i18n.t('FLASHSALE_CAMPAGIN_SCHEDULER_LOG_RUNNING_V2', {
          time: flashsaleScheduler.timeSlot.labelTime,
          current,
          total,
        });

        commit('SET_SHOPEE_FLASHSALE_CAMPAIGN_LOG', logMessage);
        dispatch('FetchShopeeFlashsaleList', 'scheduler');
      };

      await createScheduleFlashsaleFromCampaignShopId(shopId, callbackScheduler);

      commit('SET_SHOPEE_FLASHSALE_CAMPAIGN_RUNNING', false);

      dispatch('FetchShopeeFlashsaleList', 'scheduler');

      return true;
    },
    async RunShopeeFlashsaleAutoScheduler({
      state, getters, commit, dispatch,
    }) {
      const shopId = getters.getShopeeShopId;
      const { schedulerJob } = state;

      if (!(shopId && !schedulerJob.running)) {
        return false;
      }

      if (!schedulerJob.auto) {
        commit('SET_SHOPEE_FLASHSALE_SCHEDULER_LOG', i18n.t('FLASHSALE_SCHEDULER_LOG_PAUSED'));
        return false;
      }

      commit('SET_SHOPEE_FLASHSALE_SCHEDULER_RUNNING', true);

      /**
       * @param {{flashsaleData: FlashSaleListScheduler, current: number, total: number}} payload
       */
      const callbackFlashsale = (payload) => {
        const {
          flashsaleData,
          current,
          total,
        } = payload;

        const logMessage = i18n.t('FLASHSALE_SCHEDULER_LOG_RUNNING_V2', {
          time: flashsaleData.timeSlot.labelTime,
          current,
          total,
        });

        Message.info(logMessage);
        commit('SET_SHOPEE_FLASHSALE_SCHEDULER_LOG', logMessage);
        dispatch('FetchShopeeFlashsaleList', 'scheduler');
      };

      commit('SET_SHOPEE_FLASHSALE_SCHEDULER_LOG', i18n.t('FLASHSALE_SCHEDULER_LOG_START'));

      const listFlashsaleCreated = await createFlashsaleFromSchedulerShopId(shopId, callbackFlashsale);

      await delay(1000);

      if (listFlashsaleCreated && listFlashsaleCreated.length) {
        commit('SET_SHOPEE_FLASHSALE_SCHEDULER_LOG', i18n.t('FLASHSALE_SCHEDULER_LOG_COMPLETE'));
        dispatch('FetchShopeeFlashsaleList', 'scheduler');
      } else {
        commit('SET_SHOPEE_FLASHSALE_SCHEDULER_LOG', i18n.t('FLASHSALE_SCHEDULER_LOG_EMPTY'));
      }

      commit('SET_SHOPEE_FLASHSALE_SCHEDULER_RUNNING', false);

      return true;
    },
  },
};
