/* eslint-disable */
'use strict';

(function () {
  var appointment2_plan_a = $('.appointment2-click-a');// 新游预约2.0 拉起预约点击按钮

  var appointment_plan_a = $('.appointment-click-a'); // a方案点击按钮
  var appointment_plan_b = $('.appointment-click-b'); // b方案点击按钮
  var appointment_down = $('.ui-click-appointment-down'); // 下载app提示弹窗
  var appointment_tic = $('.ui-click-appointment-tic'); // 拉起提示弹窗
  var appointment_phone = $('.ui-click-appointment-phone'); // 输入手机号码弹窗
  var appointment_success = $('.ui-click-appointment-success'); // 预约成功弹窗
  var appointment_bottom_btn = $('.yuyue-bottom-btn'); // 底部预约按钮
  var app_info = $('#app-game-id');

  // 已经预约的改变按钮状态
  init();

  /*
  * 新游预约2.0 增量迭代
  * 需求：对于预约事件不再使用提示弹窗，直接拉起九游app
  * 增量点： 增加对 .appointment2_plan_a 的事件监听
  * 修改： 对于拉起抛包使用share链接形式
  */
  appointment2_plan_a.on('click', function () {
    var _that = this;
    var gameId = app_info.data('gameId');
    var statis = app_info.data('statis')||'';
    var planId = statis.split('_')[4];
    var adCardId = statis.split('_')[5];
    var ch = 'O​K_140';
    var from = 'ugt-web-reserve-'+gameId+'-jihua-'+planId+'_kapian-'+adCardId;
    gameId ? location.href = 'http://web.9game.cn/share?pageType=game_detail&ch='+ch+'&gameId='+gameId+'&pullUpFrom='+from: '';
    window.setTimeout(function(){
      appointment2reserveDone($(_that));
    },500)
  });
// 针对2.0预约按钮点击后的 done 遮罩
  function appointment2reserveDone(){
    for(var i in arguments){
      if (arguments[ i ].parent().attr('class').match(/appointment-2/)){
        arguments[ i ].parent().addClass('done');
      }
    }
  }
// -------新游预约2.0 增量迭代 end---------

  // plan A
  appointment_plan_a.on('click', function () {
    if (!localStorage.getItem(getKey())) {
      appointment_down.show();
    }
  });

  // plan B
  appointment_plan_b.on('click', function () {
    if (!localStorage.getItem(getKey())) {
      appointment_phone.show();
    }
  });

  // plan A 点击安装九游app,拉起预约
  appointment_down.find('.appointment-down-btn').on('click', function () {
    var gameId = app_info.data('gameId');
    gameId ? pullApp(getWakeupCfg(gameId)) : '';
  });

  // plan B 输入手机号码,点击预约
  appointment_phone.find('.btn-down').on('click', function () {
    var phone_input = $('.phone-num .num').val();
    var gameId = app_info.data('gameId');
    if (testPhone(phone_input)) {
      ajaxAppointment({
        gameId: gameId,
        mobile: phone_input
      });
    } else {
      appointment_phone.find('.wrong-tic').show();
      if (timter) clearTimeout(timter);
      var timter = setTimeout(function () {
        appointment_phone.find('.wrong-tic').hide();
      }, 2000);
    }
  });

  // plan B 点击我知道了
  appointment_success.find('.btn-down').on('click', function () {
    appointment_success.hide();
  });

  // 统一弹窗点击
  var mask_block = $('.mod-mask-block');
  var mask_con = $('.mod-mask-con');
  mask_block.on('click', function () {
    mask_con.hide();
  });

  function init() {
    if (localStorage.getItem(getKey())) {
      var appointment_plan = $('.appointment-text').parent();
      appointment_plan.addClass('unable');
      appointment_plan.find('.desc-tic').html('已预约');
      appointment_bottom_btn.addClass('unable').html('已预约');

      appointment2reserveDone(appointment_plan_b,appointment2_plan_a);
    }

    var yuyue_mask = $('.yuyue-mask');
    var WakeupCfg = getWakeupCfg(app_info.data('gameId'));
    var yuyue_down_url = NineGameClientPullUp.getSchemeDownloadUrl(WakeupCfg);
    if (yuyue_mask.length > 0) {
      var yuyue_down_qrcode = xMan_qrcode({render:'canvas',width:190,height:190,correctLevel:0,text:yuyue_down_url});
      yuyue_mask.find('.qrcode').data('url', yuyue_down_url);
      yuyue_mask.find('.qrcode').append(yuyue_down_qrcode);
    }
  }

  function getKey() {
    var gameId = app_info.data('gameId');
    return 'appointment-' + gameId;
  }

  function pullApp(wakeupCfg) {
    NineGameClientPullUp.wakeup(wakeupCfg, function (error, update, result) {
      if (!error) {
        console.log('拉起成功');
      } else {
        if (update) {
          console.log('拉起高速下载失败, 需升级, 下载地址: ' + result);
          window.location.href = result;
        } else {
          console.log('拉起高速下载失败, 未安装, 下载地址: ' + result);
          window.location.href = result;
        }
      }
    });
  }

  function getWakeupCfg(gameId) {
    var ch = app_info.data('ch');
    var _actionpath = $('#_actionpath').val();
    return {
      udf: true,
      apkName: 'ninegame_' + ch + '.apk',
      type: 'game_detail',
      action: 'reserve',
      pullUpFrom: 'ugt_game_highspeed_' + gameId + '_' + _actionpath,
      checkTime: 1000,
      ch: ch,
      params: {
        gameId: gameId
      }
    };
  }

  function testPhone(number) {
    return /^1(3|4|5|7|8)[0-9]\d{8}$/.test(number);
  }

  function ajaxAppointment(data) {
    $.ajax({
      type: 'POST',
      url: '/reserve/add',
      data: data,
      dataType: 'json',
      success: function(response) {
        mask_con.hide();
        appointment_success.show();
        var appointment_plan = $('.appointment-text').parent();
        appointment_plan.addClass('unable');
        appointment_plan.find('.desc-tic').html('已预约');
        appointment_bottom_btn.addClass('unable').html('已预约');

        var pc_yuyue = $('.btn-con .yuyue');
        pc_yuyue.each(function () {
          var $this = $(this);
          if (!$this.hasClass('appointment-hhc-btn')) {
            $this.addClass('unable');
            $this.html('<span class="icon"></span>已预约');
          } else {
            $this.addClass('no');
            $this.html('已预约');
          }
        });

        appointment2reserveDone(appointment_plan_b);

        localStorage.setItem(getKey(), 'true');
      },
      error: function(error) {
        console.log(error);
      },
    });
  }
})();