import {
  alpulOptions
} from '../settings/alpul';
import {
  couponOptions
} from '../settings/coupon';
import {
  hiOptions
} from '../settings/hi';
import {
  jdOptions
} from '../settings/jd';
import {
  koubeiOptions
} from '../settings/koubei';
import {
  laobiOptions
} from '../settings/laobi';
import {
  weixinOptions
} from '../settings/weixin';
import {
  okOptions
} from '../settings/ok';

export class IntroService {
  constructor($log, $uibModal, introJs, globalVar) {
    'ngInject';

    this.$log = $log;
    this.$uibModal = $uibModal;
    this.introJs = introJs;
    this.globalVar = globalVar;
  }

  openModal(component, customModalClass) {
    return this.$uibModal.open({
      component: component,
      windowClass: customModalClass,
      backdrop: 'static',
      resolve: {
        //   shopObj: () => {
        //     return new Promise((resolve, reject) => {
        //       resolve(this.shopObj);
        //     });
        //   },
        //   service: () => this.toastr,
        //   downloadFileAlp: () => this.downloadFile
      }
    }).result;
  }

  showIntros(options, onexit, onafterchange) {
    setTimeout(() => {
      // Check element exist or not, if not, do not show intro...
      if (angular.element(options.steps[0].element).length === 0) {
        return;
      }

      if (options.isOkOptions) {
        this.setShowOkIntro(true);
      }

      if (options.isFinishOptions) {
        this.setShowFinishIntro(true);
      }

      let intro = this.introJs();
      let self = this;

      intro.setOptions(options);
      if (onafterchange) {
        intro.onafterchange(onafterchange);
      } else {
        // For final OK steps, remove overlay: need to wait for intro.js framework new release to 
        // support this feature, currently, it just can set the opacity of overlay
        intro.onafterchange(function () {
          if (options.isOkOptions || options.isFinishOptions) {
            setTimeout(() => {
              angular.element('.introjs-overlay').css({
                display: 'none'
              });
            }, 10);
          }
        });
      }

      intro.oncomplete(function () {
        if (options.isOkOptions) {
          if (options.from && options.from === 'oldStager') {
            _laq.push(['send', 'click', 'btn-type', 'help-oldman-complete']);
            delete options.from;
          }

          self.setShowOkIntro(false);
        }

        if (options.isFinishOptions) {
          self.setShowFinishIntro(false);
        }

        let href = window.location.href;
        if (href.indexOf('showIntro') > -1) {
          window.location.href = href.substring(0, href.indexOf('showIntro') - 1);
        }
      });
      intro.exit();
      intro.start();
      if (onexit) {
        intro.onexit(onexit);
      }
    }, 100);
  }

  exitIntro() {
    this.introJs().exit();
  }

  goToChannel(channel, isFromQuestion) {
    this.maidian_goToChannel(channel, isFromQuestion);

    let url = this.globalVar.VOUCHER_URL + '#/activity/channel?showIntro=' + channel;
    if (window.location.href === url) {
      let options;
      if (channel) {
        switch (channel) {
          case 'alpul':
            options = alpulOptions;
            break;
          case 'coupon':
            options = couponOptions;
            break;
          case 'hi':
            options = hiOptions;
            break;
          case 'jd':
            options = jdOptions;
            break;
          case 'koubei':
            options = koubeiOptions;
            break;
          case 'laobi':
            options = laobiOptions;
            break;
          case 'weixin':
            options = weixinOptions;
            break;
          default:
            break;
        }

        this.showIntros(options);
      }
    } else {
      window.location.href = url;
    }
  }

  maidian_goToChannel(channel, isFromQuestion) {
    let maidianData;
    if (channel) {
      switch (channel) {
        case 'alpul':
          maidianData = 'alipay';
          break;
        case 'coupon':
          maidianData = 'others';
          break;
        case 'hi':
          maidianData = 'lingxi';
          break;
        case 'jd':
          maidianData = 'jdCampus';
          break;
        case 'koubei':
          maidianData = 'koubei';
          break;
        case 'laobi':
          maidianData = 'own';
          break;
        case 'weixin':
          maidianData = 'weixin';
          break;
        default:
          break;
      }
    }

    if (maidianData) {
      if (isFromQuestion) {
        maidianData = 'question-' + maidianData;
      }

      _laq.push(['send', 'click', 'btn-type', 'help-coupon-' + maidianData + '-btn']);
    }
  }

  onCloseModal(modalInstance) {
    let self = this;
    let options = okOptions;
    if (modalInstance && modalInstance.result) {
      modalInstance.result.then(res => {
        if (res && res.$value === 'close') {
          self.showIntros(options);
        }
      });
    }
  }

  setShowOkIntro(isShowOkIntro) {
    window.localStorage.setItem('SHOW_OK_INTRO', isShowOkIntro);
  }

  getShowOkIntro() {
    return window.localStorage.getItem('SHOW_OK_INTRO');
  }

  setShowFinishIntro(isShowFinishIntro) {
    window.localStorage.setItem('SHOW_FINISH_INTRO', isShowFinishIntro);
  }

  getShowFinishIntro() {
    return window.localStorage.getItem('SHOW_FINISH_INTRO');
  }
}