
import BehaviorBase from '../framework/BehaviorBase';
import DataModelManager from '../framework/DataModelManager';
import ConstDefine from '../model/ConstDefine';


export default class ShareBehavior extends BehaviorBase
{
    constructor()
    {
        super();
        this.teamId = -1;
        this.teamMatchId = -1;
        this.roomId = -1;
        this.roomMatchId = -1;

    }

    //内部逻辑方法，外部不要直接调用
    _shareMessageToWX(shareParams, snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
            var succ = false;
            window["wx"]["shareAppMessage"]({
                "withShareTicket": true,
                "title": shareParams["title"],
                "imageUrl": shareParams["image_url"],
                "query": shareParams['query'],
                "success": function (res) {
                    succ = true;
                    if(typeof(snsReportFunc) == "function") {
                        snsReportFunc({"retCode": succ ? 0 : 1});
                    }
                    if (shareSuccessFunc) {
                        console.log("shareAppMessage shareSuccessFunc");
                        shareSuccessFunc(res, shareParams['query']);
                    }
                    console.log("shareAppMessage success end");
                },
                "fail": function () {
                    console.log("shareAppMessage fail");
                    succ = false;
                    if(typeof(snsReportFunc) == "function") {
                        snsReportFunc({"retCode": succ ? 0 : 1});
                    }
                    if (shareFailFunc) {
                        shareFailFunc();
                    }
                },
                "complete": function() {
                    console.log("shareAppMessage complete succ:%s", succ ? "true" : "false");
                }
            });
    }
    //好友PK 其实是单人的
    shareFriendPKToWx(roomId,matchId,snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
        if (roomId == null || matchId == null)
        {
            console.log("roomId == null || matchId == null:" + roomId + " " + matchId );
            return ;
        }

        if(CC_WECHATGAME)
        {

            this._wxUserInfo = DataModelManager.getModel('PlayerInfoModel').getSelfWXPlayerInfo();
            // var queryValue = {};
            // queryValue['method'] = ConstDefine.ShareMessage.shareType;
            // queryValue['wxPlayerInfo'] = this._wxUserInfo;
            // queryValue['roomId'] = roomId;
            // queryValue['matchId'] = matchId;
            // var queryStr =  JSON.stringify(queryValue);
            var queryStr = 'method=' + ConstDefine.ShareFriendPKMessage.shareType+'&wxPlayerInfo=' + JSON.stringify(this._wxUserInfo)+'&roomId=' + roomId + '&matchId=' + matchId;
            console.log("query:" + queryStr);

            let shareParams = {};
            shareParams["title"] = ConstDefine.ShareFriendPKMessage.title;
            shareParams["image_url"] = ConstDefine.ShareFriendPKMessage.url;
            shareParams['query'] = queryStr
            this._shareMessageToWX(shareParams,snsReportFunc, shareSuccessFunc, shareFailFunc);
            
        }
    }
    //双人PK
    shareDoublePKToWx(teamId,matchId,snsReportFunc, shareSuccessFunc, shareFailFunc)
    {


        if (teamId == null || matchId == null)
        {
            console.log("teamId == null || matchId == null:" + teamId + " " + matchId );
            return ;
        }



        if(CC_WECHATGAME)
        {

            this._wxUserInfo = DataModelManager.getModel('PlayerInfoModel').getSelfWXPlayerInfo();
            // var queryValue = {};
            // queryValue['method'] = ConstDefine.ShareDoublePKMessage.shareType;
            // queryValue['wxPlayerInfo'] = this._wxUserInfo;
            // queryValue['roomId'] = roomId;
            // queryValue['matchId'] = matchId;
            // var queryStr =  JSON.stringify(queryValue);
            var queryStr = 'method=' + ConstDefine.ShareDoublePKMessage.shareType+'&wxPlayerInfo=' + JSON.stringify(this._wxUserInfo)+'&teamId=' + teamId + '&matchId=' + matchId;
            console.log("query:" + queryStr);

            let shareParams = {};
            shareParams["title"] = ConstDefine.ShareDoublePKMessage.title;
            shareParams["image_url"] = ConstDefine.ShareDoublePKMessage.url;
            shareParams['query'] = queryStr
            this._shareMessageToWX(shareParams,snsReportFunc, shareSuccessFunc, shareFailFunc);
            
        }
    }    

    shareGroupRankToWx(snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
        if(CC_WECHATGAME)
        {

            this._wxUserInfo = DataModelManager.getModel('PlayerInfoModel').getSelfWXPlayerInfo();
            var queryStr = 'method=' + ConstDefine.ShareGroupRankMessage.shareType+'&wxPlayerInfo=' + JSON.stringify(this._wxUserInfo);
            console.log("query:" + queryStr);

            let shareParams = {};
            shareParams["title"] = ConstDefine.ShareGroupRankMessage.title;
            shareParams["image_url"] = ConstDefine.ShareGroupRankMessage.url;
            shareParams['query'] = queryStr
            this._shareMessageToWX(shareParams,snsReportFunc, shareSuccessFunc, shareFailFunc);
            
        }
    }

    //share honor
    shareHonorToWx(title, url, snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
        if (CC_WECHATGAME)
        {
            this._wxUserInfo = DataModelManager.getModel('PlayerInfoModel').getSelfWXPlayerInfo();

            var queryStr = 'method=' + ConstDefine.ShareHonorMessage.shareType + '&wxPlayerInfo=' + JSON.stringify(this._wxUserInfo);
            console.log('query:' + queryStr);

            let shareParams = {};
            shareParams["title"] = title;
            shareParams["image_url"] = url;
            shareParams["query"] = queryStr;
            this._shareMessageToWX(shareParams, snsReportFunc, shareSuccessFunc, shareFailFunc);
        }
    }

    //share achievement
    shareACMToWx(url,snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
        if (url == null)
        {
            console.log("url = null :");
            return ;
        }

        if(CC_WECHATGAME)
        {

            this._wxUserInfo = DataModelManager.getModel('PlayerInfoModel').getSelfWXPlayerInfo();
   

            var queryStr = 'method=' + ConstDefine.ShareACMMessage.shareType+'&wxPlayerInfo=' + JSON.stringify(this._wxUserInfo);
            console.log("query:" + queryStr);

            let shareParams = {};
            shareParams["title"] = ConstDefine.ShareACMMessage.title;
            shareParams["image_url"] = url;
            shareParams['query'] = queryStr
            this._shareMessageToWX(shareParams,snsReportFunc, shareSuccessFunc, shareFailFunc);
            
        }
    }

    //好友PK 其实是单人的
    shareFriendPKWx(snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
        this.shareFriendPKToWx(this.roomId,this.roomMatchId,snsReportFunc, shareSuccessFunc, shareFailFunc);
      
    }

    //双人PK
    shareDoublePKWx(snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
        this.shareDoublePKToWx(this.teamId,this.teamMatchId,snsReportFunc, shareSuccessFunc, shareFailFunc);
    
    }
    
    //群排行
    shareGroupRankWx(snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
        this.shareGroupRankToWx(snsReportFunc, shareSuccessFunc, shareFailFunc)
    }

    //share achievement
    shareACMWx(snsReportFunc, shareSuccessFunc, shareFailFunc)
    {
        this.shareACMToWx(snsReportFunc, shareSuccessFunc, shareFailFunc)
    }    

    setTeam(team_id,team_match_id)
    {
        this.teamId = team_id;
        this.teamMatchId = team_match_id;
    }

    setRoom(room_Id,room_match_id)
    {
        this.roomId = room_Id;
        this.roomMatchId = room_match_id;
    }

}

