export class IntroQuestionController {
  constructor($http, $log, $state, toastr, globalVar, $scope, introService) {
    'ngInject';

    this.$http = $http;
    this.$log = $log;
    this.$state = $state;
    this.toastr = toastr;
    this.globalVar = globalVar;
    this.$scope = $scope;
    this.introService = introService;


    this.$onInit = this.onInit.bind(this);
    this.$onChanges = this.onChanges.bind(this);
  }

  onInit() {
    this.isShowPre = false;
    this.isShowSubmit = false;
    this.isSubmited = false;
    this.currentQuestionNum = 1;
    this.questions = [{
      question: '请问，您曾经有使用过能够发【优惠券活动】的平台吗？',
      options: [{
        active: false,
        value: '有',
        key: 'yes'
      }, {
        active: false,
        value: '没有',
        key: 'no'
      }],
      isShow: true,
      answer: ''
    }, {
      question: '请问，您拥有微信公众号功能吗？',
      options: [{
        active: false,
        value: '有',
        key: 'yes'
      }, {
        active: false,
        value: '没有',
        key: 'no'
      }],
      isShow: false,
      answer: ''
    }, {
      question: '请问，您是否已经在使用口碑店铺功能？',
      options: [{
        active: false,
        value: '是',
        key: 'yes'
      }, {
        active: false,
        value: '不是',
        key: 'no'
      }],
      isShow: false,
      answer: ''
    }, {
      question: '请问，您是否需要将优惠券放在您的线上店铺之外的位置投放？',
      options: [{
        active: false,
        value: '是',
        key: 'yes'
      }, {
        active: false,
        value: '不是',
        key: 'no'
      }],
      isShow: false,
      answer: ''
    }, {
      question: '请问，您是否有开发团队，或愿意让我们帮您开发功能进行连接？',
      options: [{
        active: false,
        value: '有',
        key: 'yes'
      }, {
        active: false,
        value: '没有',
        key: 'no'
      }],
      isShow: false,
      answer: ''
    }, {
      question: '请问，您是否有意愿向支付宝和京东大学生群体投放优惠活动？',
      options: [{
        active: false,
        value: '有',
        key: 'yes'
      }, {
        active: false,
        value: '没有',
        key: 'no'
      }],
      isShow: false,
      answer: ''
    }];
    this.results = [{
        isShow: false,
        channel: 'koubei',
        value: '口碑App'
      }, {
        isShow: false,
        channel: 'alpul',
        value: '支付宝大学生活'
      },
      {
        isShow: false,
        channel: 'jd',
        value: '京东校园专区'
      }, {
        isShow: false,
        channel: 'weixin',
        value: '微信公众号'
      },
      {
        isShow: false,
        channel: 'hi',
        value: '灵犀自有渠道'
      }, {
        isShow: false,
        channel: 'coupon',
        value: '异业线下合作渠道'
      },
      {
        isShow: false,
        channel: 'laobi',
        value: '商家自有线上渠道'
      }
    ];
  }

  onChanges(changes) {
    if (changes) {

    }
  }

  showIntros(channel) {
    this.introService.goToChannel(channel, true);
    this.closeModal('reopen');
  }

  pre() {
    let index;
    for (index = 0; index < this.questions.length - 1; index++) {
      const element = this.questions[index];
      if (element.isShow) {
        element.isShow = false;
        break;
      }
    }

    if (this.questions[index - 1]) {
      this.questions[index - 1].isShow = true;
    }

    this.isShowPre = !this.questions[0].isShow;

    if (index === 0) {
      this.questions[0].isShow = true;
    }

    this.currentQuestionNum--;
  }

  next() {
    if (!this.validate()) {
      return;
    }

    let index;
    for (index = 0; index < this.questions.length - 1; index++) {
      const element = this.questions[index];
      if (element.isShow) {
        element.isShow = false;
        break;
      }
    }

    if (this.questions[index + 1]) {
      this.questions[index + 1].isShow = true;
    }

    this.isShowPre = !this.questions[0].isShow;

    if (index === this.questions.length - 2) {
      this.isShowSubmit = true;
    }

    this.currentQuestionNum++;
  }

  validate() {
    let currentQuestion = this.questions.filter(f => f.isShow)[0];
    if (currentQuestion && !currentQuestion.answer) {
      this.toastr.warning('请回答以上问题');
      return false;
    }

    return true;
  }

  submit() {
    if (!this.validate()) {
      return;
    }

    this.getResults();
    this.isSubmited = true;

    this.currentQuestionNum = -1;
  }

  back() {
    _laq.push(['send', 'click', 'btn-type', 'help-coupon-question-return']);

    this.closeModal('reopen');
    this.introService.openModal('introCouponActivity', 'modal-650');
  }

  getResults() {
    // TODO: use index as id is stupid, but no time left, need to revise in feature
    // Q1 === > 灵犀自有渠道
    // Q2 === > 微信公众号
    // Q3 === > 口碑App
    // Q4 === > 异业线下合作渠道
    // Q5 === > 商家自有线上渠道
    // Q6 === > 支付宝大学生活 & 京东校园专区
    this.results.filter(f => f.value === '灵犀自有渠道')[0].isShow = this.questions[0].answer === this.questions[0].options[0].value;
    this.results.filter(f => f.value === '微信公众号')[0].isShow = this.questions[1].answer === this.questions[1].options[0].value;
    this.results.filter(f => f.value === '口碑App')[0].isShow = this.questions[2].answer === this.questions[2].options[0].value;
    this.results.filter(f => f.value === '异业线下合作渠道')[0].isShow = this.questions[3].answer === this.questions[3].options[0].value;
    this.results.filter(f => f.value === '商家自有线上渠道')[0].isShow = this.questions[4].answer === this.questions[4].options[0].value;
    this.results.filter(f => f.value === '支付宝大学生活')[0].isShow = this.questions[5].answer === this.questions[5].options[0].value;
    this.results.filter(f => f.value === '京东校园专区')[0].isShow = this.questions[5].answer === this.questions[5].options[0].value;

    // 如果所有渠道都没有选择：显示“灵犀自有渠道”
    if (this.results.filter(f => f.isShow).length === 0) {
      this.results.filter(f => f.value === '灵犀自有渠道')[0].isShow = true;
    }
  }

  reset() {
    _laq.push(['send', 'click', 'btn-type', 'help-coupon-question-close']);

    this.closeModal('reopen');
    this.introService.openModal('introQuestion', 'modal-650');
  }

  onSelectOption(question, selectedOption, index) {
    let currentoption = question.options.filter(f => f.value === selectedOption.value)[0];
    currentoption.active = !selectedOption.active;

    let anotheroption = question.options.filter(f => f.value !== selectedOption.value)[0];
    if (currentoption.active) {
      anotheroption.active = false;
    }

    question.answer = selectedOption.active ? selectedOption.value : '';

    this.maidian(selectedOption, index);
  }

  maidian(selectedOption, index) {
    if (!selectedOption.active) {
      return;
    }

    let questionNum = index + 1 + '';
    let questionChoice;
    if (selectedOption) {
      questionChoice = selectedOption.key;
    }

    if (questionNum && questionChoice) {
      _laq.push(['send', 'click', 'btn-type', 'help-coupon-question' + questionNum + '-' + questionChoice]);
    }
  }

  closeModal(value) {
    return this.close({
      $value: value
    });
  }

  onCloseBtn() {
    // this.currentQuestionNum > 0 : question & answer
    // this.currentQuestionNum = -1 : finish
    if (this.currentQuestionNum > 0) {
      _laq.push(['send', 'click', 'btn-type', 'help-coupon-question' + this.currentQuestionNum + '-close']);
    } else {
      _laq.push(['send', 'click', 'btn-type', 'help-coupon-question-complete-close']);
    }
  }
}

export const introQuestionComponent = {
  bindings: {
    // For uibModal
    resolve: '<',
    modalInstance: '<',
    close: '&',
    dismiss: '&'
  },
  templateUrl: 'app/modules/intros/components/intro-question/intro-question.html',
  controller: IntroQuestionController
};