import DaDianScript from "../../../FrameWork/Base/DaDianScript";
import WordGameBaseComponent from "../../../FrameWork/Base/WordGameBaseComptent";
import { ryw_Event } from "../../../FrameWork/Event/EventEnum";
import EventMgr from "../../../FrameWork/Event/EventMgr";
import Common from "../../../FrameWork/Util/Common";
import Common5 from "../../../Platform/th/Common5";
import Game from "../../../Scripts/Game";
import CccGame from "../../../WordGame/gameComScript/CccGame/CccGame";
import DuiChoosePrefab from "../../../WordGame/gameComScript/DuiChoosePrefab";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";
// import YingJingFangGeConfig from "./YingJingFangGeConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NuDuiJianPanXiaScript extends WordGameBaseComponent {

    @property(cc.Node)
    npc: cc.Node = null;

    @property(cc.Node)
    player: cc.Node = null;

    @property(cc.Node)
    duiChoosePrefab: cc.Node = null;

    // @property(cc.Node)
    // playerHp: cc.Node = null;

    @property(cc.Node)
    npcHp: cc.Node = null;

    @property(cc.Node)
    role: cc.Node = null;

    @property(cc.Node)
    record: cc.Node = null;

    @property(cc.Node)
    yinFu: cc.Node = null;

    @property(cc.Node)
    fenShouZhi: cc.Node = null;

    //当前层级下标
    curLayerIndex = 0;

    //当前进度
    curSchedule: number = 0;
    playerLifeNum: number = 0;
    npcLifeNum: number = 0;

    //文本配置
    duihuaArrayConfig: any[] = [];
    duiListConfig: any[] = [];
    maxHp: number = 0;


    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        this.openTouchEvent(this.node.getChildByName('bg'));
        // this.initParameters();
        // this.initComponent();
   
        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/YingJingFangGeConfig',(assest)=>{
            //this.jsonData = JSON.stringify(assest.json)

            this.jsonData =assest.json

            this.initParameters();
            this.initComponent();
        } )

    }

    //初始化参数
    initParameters() {
        this.curSchedule = 0;
        this.maxHp = this.npcHp.width;
        this.duihuaArrayConfig = this.jsonData.duihuaArray//YingJingFangGeConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.duiListConfig = this.jsonData.duiList//YingJingFangGeConfig.getInstance().getGameConfig('duiList') as any[];
        Common.Type = 0
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.tipsArray//YingJingFangGeConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray//YingJingFangGeConfig.getInstance().getGameConfig('answersArray');

        EventMgr.onEvent_custom(ryw_Event.DuiChooseResult, (data_) => {
            this.duiChooseResultCallback(data_);
        }, this);

        EventMgr.onEvent_custom(ryw_Event.NormalTouchMoveCheck, (data_) => {
            this.normalTouchMoveCheckCallback(data_.targetNode);
        }, this);
    }


    //初始化组件
    initComponent() {
        let lab = this.node.getChildByName("标题").getChildByName("lab");
        lab.getComponent(cc.Label).string = this.jsonData.titleArray//YingJingFangGeConfig.getInstance().getGameConfig('titleArray');

        let title = this.jsonData.titleArray//YingJingFangGeConfig.getInstance().getGameConfig('titleArray');
        Game.ins.setGameTitle(title)

        this.playerLifeNum = this.duiListConfig.length + 1;
        this.npcLifeNum = this.duiListConfig.length + 1;
        this.fenShouZhi.getComponent(cc.Label).string = (this.npcLifeNum * 10).toString();

        // this.player.getComponent(sp.Skeleton).setAnimation(0, 'shuohua', true);
        // this.showQiPao(this.duihuaArrayConfig[0], () => {
        //     this.player.getComponent(sp.Skeleton).setAnimation(0, 'jingzhi', true);
        //     this.showAsk();
        // });
        this.showAsk();
    }

    showAsk() {
        let dui_ = this.duiListConfig[this.curSchedule];

        if (dui_) {
            let npcAsk_ = dui_.npcAsk;
            this.node.getChildByName("mask").active = true;
            CccGame.playAnimation(this.player, 'shuohua', true);
            this.showDuiHua(npcAsk_[0], () => {
                CccGame.playAnimation(this.player, 'daiji', true);
                CccGame.playAnimation(this.npc, 'shuohua', true);
                this.node.getChildByName("mask").active = false;
                this.showDuiHua(npcAsk_[1], () => {
                    CccGame.playAnimation(this.npc, 'daiji', true);
                });
            });

            let answerArray_ = dui_.answerArray;
            let scr: DuiChoosePrefab = this.duiChoosePrefab.getComponent("DuiChoosePrefab");
            scr.setDuiChooseList(answerArray_);
        }
    }

    duiChooseResultCallback(data_: any) {
        let duiChooseInfo = this.duiListConfig[this.curSchedule].answerArray[data_.touchIndex];
        console.log("++++++++++++++++++touchIndex + duiChooseResult: ", data_.touchIndex, duiChooseInfo.isCorrect);

        CccGame.playAnimation(this.record, 'xunhuan', true);
        this.yinFu.active = true;
        CccGame.playAnimation(this.yinFu, 'xunhuan', true);
        console.log("duiChooseInfo==", duiChooseInfo);
        if (duiChooseInfo.effectUrl) {
            Common5.playEffectCustom("yingjingfangge", duiChooseInfo.effectUrl);
        }

        this.scheduleOnce(() => {
            CccGame.playAnimation(this.record, 'jingzhi', true);
            this.yinFu.active = false;
            if (duiChooseInfo.isCorrect) {
                CccGame.playAnimation(this.role, 'paizhuozi', true);
                CccGame.playAnimation(this.npc, 'wulian', true);
                this.scheduleOnce(() => {
                    CccGame.playAnimation(this.role, 'daiji', true);
                    CccGame.playAnimation(this.npc, 'daiji', true);
                }, 3);
                this.npcLifeNum--;
                
            }
            else {
                CccGame.playAnimation(this.player, 'ku', true);
                this.scheduleOnce(() => {
                    CccGame.playAnimation(this.player, 'daiji', true);
                }, 3);
                this.playerLifeNum--;
            
            }
            this.scheduleOnce(() => {
                //this.playerHp.width = this.maxHp * (this.playerLifeNum / (this.duiListConfig.length + 1));
                this.npcHp.width = this.maxHp * (this.npcLifeNum / (this.duiListConfig.length + 1));
                this.fenShouZhi.getComponent(cc.Label).string = (this.npcLifeNum * 10).toString();
            }, 0);
        }, duiChooseInfo.delayTime);

        this.scheduleOnce(() => {
            this.curSchedule++;
            if (this.curSchedule == this.duiListConfig.length) {
                if (this.playerLifeNum < (this.duiListConfig.length + 1)) {
                    this.endGameView(0);
                } else {
                    this.node.getChildByName("layerNode").getChildByName("caozuo").active = true;

                    CccGame.playAnimation(this.player, 'shuohua', true);
                    this.showDuiHua(this.duihuaArrayConfig[0], () => {
                        CccGame.playAnimation(this.player, 'daiji', true);
                    });
                }
            }
            else {
                this.showAsk();
            }
        }, duiChooseInfo.delayTime + 3);
    }

    normalTouchMoveCheckCallback(targetNode: any) {
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        if (targetNode.name == "麦克风") {
            CccGame.playAnimation(this.record, 'xunhuan', true);
            this.yinFu.active = true;
            CccGame.playAnimation(this.yinFu, 'xunhuan', true);
            CccGame.playAnimation(this.npc, 'changge', true);
            Common5.playEffectCustom("yingjingfangge", 'sound/每天都要气死你,我就是要气死你');
            this.scheduleOnce(() => {
                CccGame.playAnimation(this.record, 'jingzhi', true);
                this.yinFu.active = false;

                CccGame.playAnimation(this.role, 'paizhuozi', true);
                CccGame.playAnimation(this.npc, 'wulian', true);
                this.npcLifeNum--;

                this.scheduleOnce(() => {
                    // CccGame.playAnimation(this.role, 'daiji', true);
                    // CccGame.playAnimation(this.npc, 'daiji', true);
                    this.npcHp.width = this.maxHp * (this.npcLifeNum / (this.duiListConfig.length + 1));
                    this.fenShouZhi.getComponent(cc.Label).string = (this.npcLifeNum * 10).toString();
                    this.endGameView(1);
                }, 3);
            }, 5);
        }
    }

    //显示对话
    showDuiHua(curLog, func?) {
        console.log("curLog==", curLog);

        if (curLog.effectUrl) {
            Common5.playEffectCustom("yingjingfangge", curLog.effectUrl);
        }

        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos
        let delayTime_ = curLog.delayTime || 3;

        let node = this.node.getChildByName("layerNode");
        if (qiPaoPos_ != -1) {
            let qiPao = node.getChildByName("qiPao").getChildByName("qiPao_" + qiPaoPos_);
            qiPao.stopAllActions()
            qiPao.getChildByName("str").getComponent(cc.Label).string = string_
            qiPao.active = true
            qiPao.scale = 0
            cc.tween(qiPao)
                .to(0.2, { scale: 1 })
                .delay(delayTime_)
                .call(() => {
                    qiPao.active = false
                })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab')
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript')
            script_.setDailogShow(string_, delayTime_)
        }

        this.scheduleOnce(() => {
            if (func) {
                func();
            }
        }, delayTime_);
    }

    endGameView(touchIndex) {
        this.node.getChildByName("mask").active = true;
        Game.ins.stopTime();
        if (touchIndex == 0) {
            this.scheduleOnce(() => {
                Game.ins.showFail();;
            }, 3);
        } else {
            this.scheduleOnce(() => {
                 Game.ins.showSuccess();;
            }, 3);
        }
    }

    openTouchEvent(node) {
        node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.on(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    closeTouchEvent(node: cc.Node) {
        node.off(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }

    touchStartNode(event) {
        let target = event.target;
        console.log('touchStartNode');
    }

    touchMoveNode(event) {

    }

    touchEndNode(event) {
        let target = event.target;
    }

    //update(dt) { }
}