import {
  writeLocalStorage,
  setBack,
  GetQueryString,
  readLocalstorage,
  $,
  setBg
} from '../../../../util/utils'
import {
  mobileLogin,
  partnerLogin,
  getAdImg
} from '../../../../service/course'
import {getPhoneLogin, huaweiLogin, phoneSuccess} from '../../../../util/login'
class adScene extends scene {

  constructor(scope) {
    super(scope);
    this.timer = 5;
  }

  onCreate(data) {
    setBack(true)
    this.setContentView(require('../../../../res/tpl/match/ad.tpl'), {}, 'ad', {
      // containerId: 'root'
    }, () => {
      // 兼容网页端可预览
      // if (sessionStorage.getItem("firstOpen") === 'true') {
      if (sessionStorage.getItem("firstOpen") === 'true') {
        this.enterMain();
      } else {
        this.countTime()
        this.adImg(readLocalstorage("vip"))
      }
    });
  }

  onOK(e) {
    const flag = e.target.con.attributes['data-type'].nodeValue;
    if (flag === 'jump') {
      this.enterMain();
    }

  }
  countTime() {
    $('.ad_count_time').innerHTML = '0' + this.timer;
    this.countTimeTimer = setTimeout(() => {
      this.timer--;
      if (this.timer < 0) return;
      this.countTime();
    }, 1000)
  }

  enterMain() {
    console.log('enterMain()');
    clearTimeout(this.countTimeTimer)
    clearTimeout(this.AdDelay)
    sessionStorage.setItem("firstOpen", "true");
    if (GetQueryString('userPhone')) {
      const mobile = GetQueryString('userPhone')
      this.renderLogin(mobile)
      return false;
    }
    if ((~~GetQueryString('version') > 10010) && !readLocalstorage('mobile')) {
      writeLocalStorage('uid', '123456');
      writeLocalStorage('nickName', '');
      // this.showScene(require('./ChineseMathScene.js'), {})
      return false;
    }
    if (readLocalstorage('mobile') || readLocalstorage('uid')) {
      // this.showScene(require('./ChineseMathScene.js'), {})
    }else {
      showPhoneNumberLogin()
    }
  }

  onBack(e) {
    try {
      if (typeof efunboxJS != "undefined") {
        efunboxJS.closeApp();
      }
    } catch (err) {
      console.log('没有安卓方法产生的', err)
    }
    return true;
  }
  closeLogin(code) {
    if (GetQueryString('appCode') === '2001') {
      if (code * 1 === 700000) {
        if (typeof efunboxJS != "undefined") {
          efunboxJS.closeApp();
        }
        return false;
      }
      $('.log').style = 'display: block;font-size: 1rem;color: red;margin: 2rem 3rem';
      $('.log').innerHTML = code
    } else {
      if (typeof efunboxJS != "undefined") {
        efunboxJS.closeApp();
      }
    }
  }
  adImg(state) {
      console.log('state:' + state);
      state = state || '0'
      const appCode = GetQueryString('appCode')
      console.log('appCode:' + appCode);
      getAdImg(appCode).success(res => {
        if (res.data.vipStartImg && res.data.startImg) {
            // 调用接口查看广告图片
            $('#ad_img').src = state * 1 ? res.data.vipStartImg : res.data.startImg;
            const color = state * 1 ? res.data.vipStartColor : res.data.startColor
            setBg(color);
            this.AdDelay = setTimeout(() => {
              this.enterMain();
            }, 5000)
        }else {
          this.enterMain();
        }
      })
      if (appCode == 2010 || appCode == 2009 || appCode == 2012 || appCode == 3003 || appCode == 2013 || appCode == 2014) {
        $('.ad_img_btn').style = 'right: .3rem; transform: rotate(0);'
      } else {
        $('.ad_img_btn').style = 'left: .3rem';
      }
      $('.ad_img_box').style.display = 'block';
  }
  renderLogin(mobileNo) {
    const deviceCode = GetQueryString('uuid')
    const channel = GetQueryString('appCode')
    // const self = this;
    mobileLogin({
      mobileNo,
      deviceCode,
      channel
    }).success(res => {
      console.log(res)
      writeLocalStorage('uid', res.data.uid);
      writeLocalStorage('channelCode', res.data.channelCode);
      writeLocalStorage('nickName', res.data.nickName);
      writeLocalStorage('mobile', res.data.mobile);
      writeLocalStorage('userCreateTime', res.data.gmtCreated);
      console.log(123123, res.data.grade)
      if (res.data.grade) {
        writeLocalStorage('class', res.data.grade)
      } else {
        writeLocalStorage('class', '')
      }
      this.showScene(require('./matchScene'), {
        type: 'whole'
      });
    }).fail(error => {
      console.log(error)
    })
  }
}
module.exports = adScene;