const interactInfo = require('../data/interactInfo');
var { UserInteractionType } = require('../utils/GameEnum');
var UserInformationApi = require('../net/UserInformationApi');
const DWTool = require('../utils/DWTool');
const GameModule = require('../utils/GameModule');

cc.Class({
    extends: cc.Component,

    properties: {

        //互动次数已满提示
        interactionFullNode: cc.Node,

        //互动CD中提示
        interactionCDNode: cc.Node,
        timeLabel: cc.Label,

        //亲密度显示
        intimacyNode: cc.Node,

        //反抗成功
        revoltSuccessNode: cc.Node,

        //讨好成功
        playUpSuccessNode: cc.Node,

        //互动选项节点
        interactionSelectNode: cc.Node,
        interactionTitleRichText: cc.RichText,

        //互动选项item
        interactionItemPrefab: cc.Prefab,
    },

    // LIFE-CYCLE CALLBACKS:

    init(uid, interactionInfo, userInformation) {
        this.node.parent = cc.find("Canvas");
        this.node.setContentSize(cc.view.getVisibleSize());
        this.node.active = false;

        this.uid = uid;
        this.userInformation = userInformation;
        this.interactionInfo = interactionInfo;
    },

    onLoad () {
        GameModule.homeGuide.on('Fire_state26', this.selectInteraction, this);

        GameModule.homeGuide.on('Fire_state27', this.playUpShare, this);
    },

    start () {

    },

    onDisable() {
        this.node.zIndex = 0;
        this.hideAllNode();

        //销毁选项
        for (let child of this.interactionSelectNode.children) {
            if (child.getComponent('UserInteractionItem') != undefined) {
                child.destroy();
            }
        }

        this.activeNode = null;
    },

    update (dt) {
        if (this.interactionCDNode.active === true) {
            var curTimestamp = parseInt(new Date().getTime());
            if ((this.cdEnd - curTimestamp) > 0) {
                this.timeLabel.string = DWTool.calculateTime((this.cdEnd - curTimestamp) / 1000);
            } else {
                this.showInteraction();
            }
        }
    },

    closeNodeAction() {
        // this.node.active = false;
        this.hideNodeAnimation();
    },

    hideAllNode() {
        this.interactionFullNode.active = false;
        this.interactionCDNode.active = false;
        this.intimacyNode.active = false;
        this.revoltSuccessNode.active = false;
        this.playUpSuccessNode.active = false;
        this.interactionSelectNode.active = false;
    },

    showNodeAnimation(showNode) {
        this.activeNode = showNode;
        showNode.setScale(0,0);
        let scaleAnimation = cc.scaleTo(0.15,1,1);
        showNode.runAction(scaleAnimation);
    },

    hideNodeAnimation() {
        if (this.activeNode) {
            let scaleAnimation = cc.scaleTo(0.15,0,0);

            let finish = cc.callFunc(() => {
                this.node.active = false;
            }, this);

            let sequenceAcation = cc.sequence(scaleAnimation, finish);
            this.activeNode.runAction(sequenceAcation);
        } else {
            this.node.active = false;
        }
    },

    //亲密度显示
    showIntimacyInfo(isCommon) {
        this.node.zIndex += 1;
        this.node.active = true;

        this.hideAllNode();
        this.intimacyNode.active = true;
        this.showNodeAnimation(this.intimacyNode);
        this.intimacyMng = this.intimacyNode.getComponent('UserIntimacyInfo');
        this.intimacyMng.configIntimacyInfo(this.interactionInfo,true, isCommon);
    },

    //互动操作相关
    showInteraction(interactionType) {

        this.node.zIndex += 1;
        this.node.active = true;

        if (interactionType != undefined) {
            this.interactionType = interactionType;
        }

        if (this.interactionInfo.cnt >= 2) {
            this.interactionIsFull();
            return;
        }

        if (this.interactionIsCD() == true ) {
            return;
        }

        this.hideAllNode();
        this.interactionSelectNode.active = true;
        this.showNodeAnimation(this.interactionSelectNode);
        var originY = (this.interactionSelectNode.height/2) - 85;

        switch (this.interactionType) {
            case UserInteractionType.Common:
                this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>互 动</b></outline>";

                interactInfo.interactInfo5.forEach(item => {
                    var itemNode = cc.instantiate(this.interactionItemPrefab);
                    itemNode.parent = this.interactionSelectNode;
                    itemNode.y = originY;
                    originY -= (18 + 80);
                    var itemMng = itemNode.getComponent('UserInteractionItem');
                    itemMng.init(this);
                    itemMng.configData(item);
                });
                break;
            case UserInteractionType.PlayUp:

                this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>讨 好</b></outline>";

                interactInfo.interactInfo1.forEach(item => {
                    var itemNode = cc.instantiate(this.interactionItemPrefab);
                    itemNode.parent = this.interactionSelectNode;
                    itemNode.y = originY;
                    originY -= (18 + 80);
                    var itemMng = itemNode.getComponent('UserInteractionItem');
                    itemMng.init(this);
                    itemMng.configData(item);
                });
                break;
            case UserInteractionType.Revolt:

                this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>反 抗</b></outline>";

                interactInfo.interactInfo2.forEach(item => {
                    var itemNode = cc.instantiate(this.interactionItemPrefab);
                    itemNode.parent = this.interactionSelectNode;
                    itemNode.y = originY;
                    originY -= (18 + 80);
                    var itemMng = itemNode.getComponent('UserInteractionItem');
                    itemMng.init(this);
                    itemMng.configData(item);
                });
                break;
            case UserInteractionType.Pacify:

                this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>安 抚</b></outline>";

                interactInfo.interactInfo3.forEach(item => {
                    var itemNode = cc.instantiate(this.interactionItemPrefab);
                    itemNode.parent = this.interactionSelectNode;
                    itemNode.y = originY;
                    originY -= (18 + 80);
                    var itemMng = itemNode.getComponent('UserInteractionItem');
                    itemMng.init(this);
                    itemMng.configData(item);
                });

                GameModule.homeGuide.getComponent('HomeGuide').handleState('state26');

                break;
            case UserInteractionType.Order:

                this.interactionTitleRichText.string = "<outline color=#e1596c width=3><b>使 唤</b></outline>";

                interactInfo.interactInfo4.forEach(item => {
                    var itemNode = cc.instantiate(this.interactionItemPrefab);
                    itemNode.parent = this.interactionSelectNode;
                    itemNode.y = originY;
                    originY -= (18 + 80);
                    var itemMng = itemNode.getComponent('UserInteractionItem');
                    itemMng.init(this);
                    itemMng.configData(item);
                });
                break;
        }
    },

    selectInteraction() {
        UserInformationApi.postInteract(this.uid, this.interactionType, (responseData) => {
            console.log('互动操作成功' + JSON.stringify(responseData));
            this.interactionResponse(responseData);
            this.userInformation.configIntimacy(responseData);
        }, (error) => {
            console.log('互动操作失败' + error);

        });
    },

    interactionResponse(responseData) {
        this.hideAllNode();

        switch (this.interactionType) {
            case UserInteractionType.Common:
                this.playUpSuccessNode.active = true;
                this.showNodeAnimation(this.playUpSuccessNode);
                break;
            case UserInteractionType.PlayUp:
                this.playUpSuccessNode.active = true;
                this.showNodeAnimation(this.playUpSuccessNode);
                break;
            case UserInteractionType.Revolt:
                this.revoltSuccessNode.active = true;
                this.showNodeAnimation(this.revoltSuccessNode);
                break;
            case UserInteractionType.Pacify:
                this.playUpSuccessNode.active = true;
                this.showNodeAnimation(this.playUpSuccessNode);
                GameModule.homeGuide.getComponent('HomeGuide').handleGuideStateNext('state26', 'state27');
                break;
            case UserInteractionType.Order:
                this.revoltSuccessNode.active = true;
                this.showNodeAnimation(this.revoltSuccessNode);
                break;
        }

        this.interactionInfo.cd = responseData.cd;
        this.interactionInfo.cdEnd = responseData.cdEnd;
        this.interactionInfo.cnt = responseData.cnt;
        this.interactionInfo.heart = responseData.heart;
        this.interactionInfo.intimacy = responseData.intimacy;

        this.playUpSuccessMng = this.playUpSuccessNode.getComponent('UserIntimacyInfo');
        let isCommon = this.interactionType == UserInteractionType.Common ? true : false;
        this.playUpSuccessMng.configIntimacyInfo(this.interactionInfo,false, isCommon);
    },

    //当天互动次数已满
    interactionIsFull() {
        this.hideAllNode();
        this.interactionFullNode.active = true;
        this.showNodeAnimation(this.interactionFullNode);
    },

    //正在CD中
    interactionIsCD() {
        this.hideAllNode();

        var curTimestamp = parseInt(new Date().getTime()); //当前时间戳
        this.cdEnd = this.interactionInfo.cdEnd;

        if ((this.cdEnd - curTimestamp) > 0) {
            this.interactionCDNode.active = true;
            this.showNodeAnimation(this.interactionCDNode);
            this.timeLabel.string = DWTool.calculateTime((this.cdEnd - curTimestamp) / 1000);
            return true;
        } else {
            return false;
        }
    },

    //讨好、安抚成功分享
    playUpShare() {
        if (!CC_WECHATGAME) {
            return;
        }

        var shareTitle = '一起来互动增长亲密度吧！';

        window.wx.shareAppMessage({
            title: shareTitle,
            imageUrl: 'http://t2.hddhhn.com/uploads/tu/201806/9999/eae9e74ec8.jpg',
        });
    },

    //反抗、使唤成功分享
    revoltShare() {
        if (!CC_WECHATGAME) {
            return;
        }

        var shareTitle = '真解气，一起来反抗老板的剥削！';
        if (this.interactionType == UserInteractionType.Order) {
            shareTitle = '真爽，一起来使唤艺人！';
        }

        window.wx.shareAppMessage({
            title: shareTitle,
            imageUrl: 'http://t2.hddhhn.com/uploads/tu/201806/9999/eae9e74ec8.jpg',
        });
    },

    //邀请互动
    inviteInteractionShare() {
        if (!CC_WECHATGAME) {
            return;
        }

        var shareTitle = '一起来互动增长亲密度吧！';

        window.wx.shareAppMessage({
            title: shareTitle,
            imageUrl: 'http://t2.hddhhn.com/uploads/tu/201806/9999/eae9e74ec8.jpg',
            success: (res) => {

            },
            fail: (res) => {

            }
        });
    }
});
