var Game=Game||{};

Game.submit_location = function(_msg){
	GameData.submitLocation = true;
	if(_msg.data.wechat_gzh){
		GameData.wechat_gzh = _msg.data.wechat_gzh;
		set_self(340,41,0,0,0);
		set_self(340,7,GameData.wechat_gzh,0,0);
		set_self(339,20,GameData.wechat_gzh.gblen()*13,0,0);
		set_self(340,18,get_self(16,18,0,0,0)+get_self(16,20,0,0,0)/2-GameData.wechat_gzh.gblen()*6.5,0,0);
		set_self(339,18,get_self(16,18,0,0,0)+get_self(16,20,0,0,0)/2-GameData.wechat_gzh.gblen()*6.5,0,0);
	}
	if(_msg.data.wechat_ewm){
		GameData.wechat_ewm=_msg.data.wechat_ewm;
	}
	if(_msg.data.wechat_kfh){
		GameData.wechat_kfh=_msg.data.wechat_kfh;
		set_self(179,7,GameData.wechat_kfh,0,0);
	}
	if(_msg.data.qq){
		GameData.qq=_msg.data.qq;
		set_self(682,7,GameData.qq,0,0);
	}
	if(_msg.data.tel){
		GameData.tel=_msg.data.tel;
		set_self(GameData.telSpid,20,String(GameData.tel).gblen()*14,0,0);
		set_self(GameData.telSpid,7,GameData.tel,0,0);
	}
	if(_msg.data.invitecode){
		C_Player.setInvitecod(_msg.data.invitecode);
		GameUI.CloseBind();
	}
	
}
//游戏其他信息
//Game.mainSceneInfo = {};
var recorderManager;
var innerAudioContext1;

    /**
   * ??????????   mdata ??????
   *  openId,avatarUrl,nickName, gender, city, province, unionId
   */
  function getwxuserinfo(mdata) {
	sharelogin(mdata.openId,mdata.avatarUrl,mdata.nickName,mdata.gender,mdata.city,mdata.province,mdata.unionId)
  }


  /**
   * ????????
   */
  function wxgameshow(mdata) {
	appservice(1);
  }
  /**
   * ????????
   */
  function wxgamehide(mdata) {
	appservice(2);
  }
  /**
   * ??????  state ????  1 ?? 2 ??
   */
  function sharegameinfo(state) {
    //if (state == 1) {

    //}
    //if (state == 2) {

    //}
    state = Number(state);
	//type = Number(type);
	C_Player.sharesuccess(state,1);


  }
//Recorderload();


/**
 * ???????
 */
function Recorderload(){
	if(!innerAudioContext1){
		innerAudioContext1 = wx.createInnerAudioContext();
	}
    var that = this;
   recorderManager=wx.getRecorderManager()
    recorderManager.onError(function(){

        console.log("????!");
    });
    recorderManager.onStop(function(res){

      console.log(res.tempFilePath );

       console.log("????!");
       //playRecord(res.tempFilePath);
       getaudiourl(res.tempFilePath,5);
    });


   //innerAudioContext1.onError((res) => {
      //console.log("??????!");

    //})
   //innerAudioContext1.onEnded((res) => {
     //console.log("??????!");
   //})
   innerAudioContext1.onError(function(res){
      console.log("??????!");

    })
   innerAudioContext1.onEnded(function(res){
     console.log("??????!");
   })
}
/**
 * ??????????
 */
  function isxiaowxgame(){
    var iswxgame;
    try{
    var recorderManager = wx.getRecorderManager();
    iswxgame=true;
    }catch(e){
    iswxgame=false;
    }
    return iswxgame;

  }
  function wxauthorize(){

  }
  /**
 * ????
 */
  function startRecordAac (){
  	console.log("开始录音");
     recorderManager.start({
      format: 'aac'
    });
//     wx.authorize({
//   scope: 'scope.record',
//   fail: function (res) {
//     // iOS ? Android ????????? errMsg ????,?????????
//       console.log("????");
//         console.log(res);
//         if (res.errMsg.indexOf('auth deny') > -1 || res.errMsg.indexOf('fail') > -1 || res.errMsg.indexOf('auth denied') > -1 ) {
//       // ???????????
//           console.log("????");
//           wx.showModal({
//             title: '??',
//             content: '??????????????????->??(?????)->?????->??',
//             showCancel:false,
//             success: function (res) {
//               if (res.confirm) {
//                 console.log('??????')
//               } else if (res.cancel) {
//                 console.log('??????')
//               }
//             }
//           })
//     }
//   },
//   success:function (res) {
//      console.log("????");
//      recorderManager.start({
//       format: 'aac'
//     });
//   }
// })


  }
  /**
 * ????
 */

  function stopRecord (){
  	console.log("结束录音");
//     wx.authorize({
//   scope: 'scope.record',
//   fail: function (res) {
//   console.log("fail????");
//   },
//   success:function (res) {
//      console.log("stop????");

//   }
// })
   recorderManager.stop()
  }
  /***
   * ????
   */
  function playRecord (src) {
   if(!innerAudioContext1){
		innerAudioContext1 = wx.createInnerAudioContext();
	}
  //var that = this;
 // var src = this.data.src;
  if (src == '') {
    console.log("????!");
    return;
  }
  innerAudioContext1.src = src;
  innerAudioContext1.play()
}





  var Wxloginbutton;
  /**
   * ?????
   */
  function initwxgame() {
    gameshow();
    gamehide();
  }
   //initwxgame();
  /**
   * ???????
   */
  //function gameshow() {
    //wx.onShow(data => {
      //console.log("******" + data)
      //console.log(data)
      //wxgameshow(data);

    //});
  //}
  function gameshow() {
    wx.onShow(function(data){
      console.log("******" + data)
      console.log(data)
      wxgameshow(data);

    });
  }
  /**
   * ???????
   */
  //function gamehide() {
    //wx.onHide(data => {
      //wxgamehide(data)

    //});
  //}
  function gamehide() {
    wx.onHide(function(data){
      wxgamehide(data)

    });
  }
  /*
   * ?????????
   *   wxgamew ??????  wxgameh ?????? imagex ???????? imagey ???????? imagew???? imageh ???? btnimageurl ????
   */
  function WXgameloginbuttonshow(wxgamew, wxgameh, imagex, imagey, imagew, imageh, btnimageurl) {

    var winh, winw;
    try {


      wx.getSystemInfo({
        success: function (res) {
          console.log(res.model)
          console.log(res.pixelRatio)
          console.log(res.windowWidth)
          console.log(res.windowHeight)
          winh = res.windowHeight;
          winw = res.windowWidth;

        }
      })


      var sh = winh / wxgameh;
      var sw = winw / wxgamew;
      console.log("sw=" + sw + "sh=" + sh);

      imagew = parseInt(imagew * sw);
      imageh = parseInt(imageh * sh);
      imagex = parseInt(imagex * sw);
      imagey = parseInt(imagey * sh);;


      if (wx.createUserInfoButton) {


        if (Wxloginbutton) {
          console.log("??button");
          Wxloginbutton.show();
          Wxloginbutton.offTap();//????????

        } else {

          console.log("????button");
          //  console.log(document.title);

          Wxloginbutton = wx.createUserInfoButton({
            type: 'image',
            image: btnimageurl,
            style: {
              left: imagex,
              top: imagey,
              width: imagew,
              height: imageh
            }
          })

        }
        //Wxloginbutton.onTap((res) => {


          //console.log(res)
          //if (res.errMsg.indexOf('auth deny') > -1 || res.errMsg.indexOf('auth denied') > -1 || res.errMsg.indexOf('fail') > -1 ) {
            //wx.showModal({
              //title: '??',
              //content: '??????????????????->??(?????)->?????->??',
              //showCancel: false,
              //success: function (res) {
                //if (res.confirm) {
                  //console.log('??????')
                //} else if (res.cancel) {
                  //console.log('??????')
                //}
              //}
            //})
          //} else {
            //WXgameLogin(res);
          //}
        //})
        Wxloginbutton.onTap(function(res){


          console.log(res)
          if (res.errMsg.indexOf('auth deny') > -1 || res.errMsg.indexOf('auth denied') > -1 || res.errMsg.indexOf('fail') > -1 ) {
            wx.showModal({
              title: '??',
              content: '??????????????????->??(?????)->?????->??',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('??????')
                } else if (res.cancel) {
                  console.log('??????')
                }
              }
            })
          } else {
            WXgameLogin(res);
          }
        })

      } else {
        // ???????????????????????,???????
        console.log("????????");
        wx.showModal({
          title: '??',
          content: '????????,???????,??????????????',
          showCancel:false,
          success: function (res) {
            if (res.confirm) {
              console.log('??????')
            } else if (res.cancel) {
              console.log('??????')
            }
          }
        })
      }
    } catch (e) {

    }
  }
  /**
   * ????????
   */
  function WXgameloginbuttonhide() {
    if (Wxloginbutton) {
      Wxloginbutton.hide();
    }
  }

  function Decrypt(key, iv, word) {
    // var openDataContext = wx.getOpenDataContext()
    // openDataContext.postMessage({
    //   text: 'hello',
    //   year: (new Date()).getFullYear()
    // })

    var murl = "https://games.tscce.cn/wechatsmallgame/encrypt.php";
    var date = {};
    date.key = encodeURIComponent(key);
    date.type = "decrypt";
    date.iv = encodeURIComponent(iv);
    date.data = encodeURIComponent(word);


    wx.request({
      url: murl,
      data: date,
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        if (res.statusCode == 200) {

          if (res.data) {
            var mdata = decodeURIComponent(res.data);
            console.log('??------' + mdata)
            mdata = JSON.parse(mdata);

            try {
              console.log("??");
              Wxloginbutton.hide();
              console.log("Wxloginbutton??");
              getwxuserinfo(mdata);
            } catch (e) {

            }

            // sharelogin(mdata.openId, mdata.avatarUrl, mdata.nickName, mdata.gender, mdata.city, mdata.province, mdata.unionId);

          }

        } else {
          console.log('??!res.statusCode' + res.statusCode)
        }
      },
      fail: function (res) {
        console.log('??!')
      }
    });
  }
  function WXgameLogin(uesrdata) {

    // ??
    wx.login({

      //success: res => {

        //if (res.code) {
           //var murl = "https://games.tscce.cn/wechatsmallgame/code2accessToken.php?appid=wxaeeca8ffffa94105" +"&secret=639a292164680d4cd87a024edae21f41"+"&js_code="+res.code;

          //console.log("????res.code=" + res.code);
          //wx.request({
            //url: murl,
            //method: 'GET',
            //header: {
              //'content-type': 'application/json'
            //},
            //success: function (res) {
              //console.log('??' + JSON.stringify(res));
              //console.log('res.statusCode' + res.statusCode);
              //if (res.statusCode == 200) {
                //var openid = res.data.openid;
                //var unionid = res.data.unionid;
                //var session_key = res.data.session_key;
                //Decrypt(session_key, uesrdata.iv, uesrdata.encryptedData);

              //} else {
                //console.log('??!res.statusCode' + res.statusCode)
              //}
            //},
            //fail: function (res) {
              //console.log('??!')
            //}
          //});
        //}

      //}
      success: function(res){

        if (res.code) {
           var murl = "https://games.tscce.cn/wechatsmallgame/code2accessToken.php?appid=wxaeeca8ffffa94105" +"&secret=639a292164680d4cd87a024edae21f41"+"&js_code="+res.code;

          console.log("????res.code=" + res.code);
          wx.request({
            url: murl,
            method: 'GET',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log('??' + JSON.stringify(res));
              console.log('res.statusCode' + res.statusCode);
              if (res.statusCode == 200) {
                var openid = res.data.openid;
                var unionid = res.data.unionid;
                var session_key = res.data.session_key;
                Decrypt(session_key, uesrdata.iv, uesrdata.encryptedData);

              } else {
                console.log('??!res.statusCode' + res.statusCode)
              }
            },
            fail: function (res) {
              console.log('??!')
            }
          });
        }

      }
    });

  }
  /**
   * ?????????
   */
  function initgameshareinfo(sharetitle, shareimageurl) {
    wx.showShareMenu({ withShareTicket: true });
    //wx.onShareAppMessage(data => {
      //return {
        //title: sharetitle,
        //imageUrl: shareimageurl,
        //query: '',
        //success: function (res) {
          //console.log(res);
          //console.log('??????');
          //sharegameinfo(1);
        //},
        //fail: function (res) {
          //console.log('??????');
          //console.log(res);
          //sharegameinfo(2);
        //}
      //}
    //})
    wx.onShareAppMessage(function(data){
      return {
        title: sharetitle,
        imageUrl: shareimageurl,
        query: '',
        success: function (res) {
          console.log(res);
          console.log('??????');
          sharegameinfo(1);
        },
        fail: function (res) {
          console.log('??????');
          console.log(res);
          sharegameinfo(2);
        }
      }
    })

  }
  /**
   * ?????????
   */
  function hidegameshareinfo(sharetitle, shareimageurl) {
    wx.hideShareMenu();

  }
  /**
   * ??????
   */
  function Pullupgameshare(sharetitle, shareimageurl) {
    wx.shareAppMessage({
      title: sharetitle,
      imageUrl: shareimageurl,
      query: "",
      success: function (res) {
        console.log(res);
        console.log('??????');
        sharegameinfo(1);

      },
      fail: function (res) {
        console.log('??????');
        console.log(res);
        sharegameinfo(2);
      }
    });
  }
  /**
   * ???????
   */
  function createGameClubButtonimage(mleft, mtop, mwidth, mheight) {
    var button = wx.createGameClubButton({
      type: 'image',
      icon: 'green',
      style: {
        left: mleft,
        top: mtop,
        width: mwidth,
        height: mheight
      }
    })
  }
  // var button = wx.createGameClubButton({
  //   type: 'text',
  //   icon: 'green',
  //   style: {
  //     left: 10,
  //     top: 76,
  //     width: 40,
  //     height: 40
  //   }
  // })





