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 DuiChoosePrefab from "../../../WordGame/gameComScript/DuiChoosePrefab";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";
// import GongJiaoRangZuoConfig from "./GongJiaoRangZuoConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GongJiaoRangZuoScript 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)
    yinxiang: cc.Node = null;

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

    //当前进度
    curSchedule: number = 0;
    playerLifeNum: number = 0;
    npcLifeNum: number = 0;
    roleState: Map<string, number> = null;

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

    
    start() {
        super.start();
        DaDianScript.userEnterDaDian();
        this.openTouchEvent(this.node.getChildByName('bg'));
        Common5.stopMusic()
        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/GongJiaoRangZuoConfig',(assest)=>{
            //this.jsonData = JSON.stringify(assest.json)
            this.jsonData =assest.json
            this.initParameters();
            this.initComponent();
        })
        Common5.playEffectCustom("gongjiaorangzuo", "sound/背景音乐")
    }

    //初始化参数
    initParameters() {
        this.curSchedule = 0;
        this.roleState = new Map();
        this.roleState.set("npc", 0);
        this.roleState.set("player", 0);
        this.maxHp = this.playerHp.width;
        this.duihuaArrayConfig = this.jsonData.duihuaArray //GongJiaoRangZuoConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.duiListConfig = this.jsonData.duiList //GongJiaoRangZuoConfig.getInstance().getGameConfig('duiList') as any[];
        Common.Type = 0;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.tipsArray //GongJiaoRangZuoConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs =this.jsonData.answersArray // GongJiaoRangZuoConfig.getInstance().getGameConfig('answersArray');

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

        EventMgr.onEvent_custom(ryw_Event.touchChaDianCheck, (data_) => {
            this.touchChaDianCheckCallback(data_.touchIndex);
        }, this);

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

        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            this.endGameView(0);
        }, this);
    }


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

        Game.ins.setGameTitle('选择正确的话回怼对方')


        this.playerLifeNum = this.duiListConfig.length;
        this.npcLifeNum = this.duiListConfig.length;
        this.showAsk();
    }

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

        if (dui_) {
            let npcAsk_ = dui_.npcAsk;
            this.npc.getComponent(sp.Skeleton).setAnimation(0, 'shuohua', true);
            this.showQiPao(npcAsk_[0], () => {
                if (this.roleState.get("npc") != 1) {
                    this.npc.getComponent(sp.Skeleton).setAnimation(0, '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);

        this.player.getComponent(sp.Skeleton).setAnimation(0, 'shuohua', true);
        this.showQiPao(duiChooseInfo, () => {
            Common5.playEffectCustom("gongjiaorangzuo", "sound/雷劈");
            if (duiChooseInfo.isCorrect) {
                this.player.getComponent(sp.Skeleton).setAnimation(0, 'daiji', true);
                this.npc.getComponent(sp.Skeleton).setAnimation(0, 'beileipi', false);
                this.roleState.set("npc", 1);
                this.scheduleOnce(() => {
                    this.npc.getComponent(sp.Skeleton).setAnimation(0, 'daiji', true);
                    this.roleState.set("npc", 0);
                }, 2);
                this.npcLifeNum--;
            }
            else {
                this.player.getComponent(sp.Skeleton).setAnimation(0, 'beileiji', false);
                this.scheduleOnce(() => {
                    this.player.getComponent(sp.Skeleton).setAnimation(0, 'daiji', true);
                }, 2);
                this.playerLifeNum--;
            }
        });

        this.scheduleOnce(() => {
            this.curSchedule++;

            this.playerHp.width = this.maxHp * (this.playerLifeNum / this.duiListConfig.length);
            this.npcHp.width = this.maxHp * (this.npcLifeNum / this.duiListConfig.length);

            if (this.curSchedule == this.duiListConfig.length) {
                if (this.playerLifeNum < this.duiListConfig.length) {
                    this.endGameView(0);
                } else {
                    this.touchChaDianCheckCallback(0);
                }
            } else if (this.curSchedule == this.duiListConfig.length - 1) {
                this.node.getChildByName("chadian").active = true;
                this.showAsk();
            }
            else {
                this.showAsk();
            }
        }, 6);
    }


    touchChaDianCheckCallback(touchIndex: any) {
        if (touchIndex == 0) {
            this.npc.getComponent(sp.Skeleton).setAnimation(0, 'tiaowu', true);
            this.roleState.set("npc", 1);
            Common5.playEffectCustom("gongjiaorangzuo", "sound/恐龙扛狼");
            this.duiChoosePrefab.active = false;
            this.npcLifeNum--;
            let tween = cc.tween(this.yinxiang)
                .to(0.2, { scale: 0.99 })
                .to(0.2, { scale: 1.01 })
                .union()
                .repeatForever()
                .start();
            this.scheduleOnce(() => {
                tween.stop();
                Common5.playEffectCustom("gongjiaorangzuo", "sound/雷劈");
                this.npc.getComponent(sp.Skeleton).setAnimation(0, 'beileipi', false);
                this.roleState.set("npc", 1);
                this.scheduleOnce(() => {
                    this.curSchedule++;
                    this.npcHp.width = this.maxHp * (this.npcLifeNum / this.duiListConfig.length);

                    if (this.playerLifeNum < this.duiListConfig.length) {
                        this.endGameView(0);
                    } else {
                        this.npc.getComponent(sp.Skeleton).setAnimation(0, 'shuohuapenzifu', true);
                        this.roleState.set("npc", 0);
                        this.node.getChildByName("caozuo").active = true;
                    }
                }, 2);
            }, 7);
        }
    }

    normalTouchMoveCheckCallback(targetNode: any) {
        console.log("targetNodeName+++++++++++++++++++++>" + targetNode.name);
        if (targetNode.name == "2") {
            this.npc.getComponent(sp.Skeleton).setAnimation(0, 'shuohua', true);
            this.showQiPao(this.duihuaArrayConfig[0], () => {
                if (this.roleState.get("npc") != 1) {
                    this.npc.getComponent(sp.Skeleton).setAnimation(0, 'daiji', true);
                }
                this.endGameView(1);
            });
        }
    }

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

    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;
    }

    showQiPao(curLog, func?) {
        console.log("curLog==", curLog);

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

        let string_ = curLog.str
        let qiPaoPos_ = curLog.qiPaoPos

        if (qiPaoPos_ != -1) {
            let qiPao = this.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(3)
                .call(() => {
                    qiPao.active = false
                    if (func) {
                        func();
                    }
                })
                .start();
        }
        else {
            let logPrefab = this.node.getChildByName('logPrefab')
            let script_: logPrefabScript = logPrefab.getComponent('logPrefabScript')
            script_.setDailogShow(string_)
            this.scheduleOnce(() => {
                if (func) {
                    func();
                }
            }, 3);
        }
    }

    //update(dt) { }
}