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/WenZiRes/ui/Game";
// import NvHaiDeYiShengConfig from "./NvHaiDeYiShengConfig";

enum YearsOldEnum{
    yo_2 = 0,
    yo_6,
    yo_12,
    yo_17,
    yo_23,
    yo_27,
    yo_35_1,
    yo_35_2,
    yo_60_1,
    yo_60_2,
    yo_80_1,
    yo_80_2,
    yo_80_3,
    Finish
}
const { ccclass, property } = cc._decorator;
@ccclass
export default class NvHaiDeYiSheng extends WordGameBaseComponent {
    
    @property(cc.Node)
    heiPin: cc.Node = null;

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

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

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

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

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    unitIconListConfig: any = null;

    //初始化节点
    
    curFinishNum:number = 0
    curZIndex:number = 100;
    
    curYearsOrdIndex = YearsOldEnum.yo_2
    isAction:boolean = false
    isWrong:boolean = false
    
    start() {
        //Common5.playMusicCustom('nvhaideyisheng','sound/背景音' );
        super.start();
        DaDianScript.userEnterDaDian();
        
        Common5.getJsonFromBundle(Common5.selectGameInfo.bundle,'script/NvHaiDeYiShengConfig',(assest)=>{
            this.jsonData = assest.json
            this.initParameters();
            this.initComponent();
            this.initView()
        })

    }

    //初始化参数
    initParameters() {
        EventMgr.onEvent_custom(ryw_Event.timeOut, (tab) => {
            this.endGameView(0);
        }, this);
        // this.fireNodeInfoArray = [];

        this.titleArrayConfig = this.jsonData.titleArray//NvHaiDeYiShengConfig.getInstance().getGameConfig('titleArray') as string[];
        this.duihuaArrayConfig = this.jsonData.duihuaArray//NvHaiDeYiShengConfig.getInstance().getGameConfig('duihuaArray') as any[];
        this.unitIconListConfig = this.jsonData.unitIconList//NvHaiDeYiShengConfig.getInstance().getGameConfig('unitIconList') as any[];

        Common.Type = 0
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.tipsArray//NvHaiDeYiShengConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray//NvHaiDeYiShengConfig.getInstance().getGameConfig('answersArray');

    }

    //初始化组件
    initComponent() {
        let lab = this.node.getChildByName("标题").getChildByName("lab");
        lab.getComponent(cc.Label).string = this.titleArrayConfig[0];
        let title = this.titleArrayConfig[0];
        Game.ins.setGameTitle(title)
    
    }
    initView(){
        this.setIconListData()
        this.setCurYearsInfo()
    }
    

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

        Common5.playEffectCustom("nvhaideyisheng", curLog.effectUrl);
        let qiPao = this.node.getChildByName("qiPao").getChildByName("qiPao_" + curLog.qiPaoPos);
        qiPao.stopAllActions()
        qiPao.getChildByName("str").getComponent(cc.Label).string = curLog.dialogStr
        qiPao.active = true
        qiPao.scale = 0
        cc.tween(qiPao)
            .to(0.2, { scale: 1 })
            .delay(curLog.delayTime)
            .call(() => {
                qiPao.active = false
                if (func) {
                    func();
                }
            })
        .start();
    }
    //设置图标列表数据
    setIconListData() {
        Common5.playMusicCustom('nvhaideyisheng','sound/场景二背景音' );

      
        this.itemIconList.active = true;
        this.itemIcon.active = false;

        for (let index = 0; index < this.unitIconListConfig.length; index++) {
            const element = this.unitIconListConfig[index];

            let itemIconNode = cc.instantiate(this.itemIcon);
            itemIconNode.active = true;
            this.itemIconContent.addChild(itemIconNode);

            let touchNode = itemIconNode.getChildByName('touchNode');
            Common5.getSpriteFrameFromBundle("nvhaideyisheng", element.url, touchNode.getComponent(cc.Sprite));
            if (element.name) {
                itemIconNode.getChildByName('name').getComponent(cc.Label).string = element.name;
            }

            this.onTouchEvent(touchNode,element.targetInfo)
        }

        for (let index = 0; index < this.itemIconContent.children.length; index++) {
            let aInt = Math.trunc(Math.random() * this.itemIconContent.children.length);
            let random = this.itemIconContent.children[aInt];
            this.itemIconContent.children[aInt] = this.itemIconContent.children[index];
            this.itemIconContent.children[index] = random;
        }
    }
    
    endGameView(touchIndex) {
        Game.ins.stopTime();
        this.scheduleOnce(()=>{
            
        // this.node.getChildByName("mask").active = true;
            if (touchIndex == 0) {
                Game.ins.showFail();
            } else {
                Game.ins.showSuccess();
            }
        },3)
        
    }
    onTouchEvent(node, iconInfo) {
        node.attr({
            iconInfo: iconInfo,
            recoveposi: node.getPosition(),//初始位置
            recoveparent: node.parent,//初始父节点
        });
        this.openTouchEvent(node)
    }
    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) {
        if(this.isAction){
            return
        }
        let target = event.target as cc.Node;
        target.parent = this.node;
        if (target['_touchListener']) {
            target['_touchListener'].setSwallowTouches(true);
        }
        let posi = event.getLocation()//世界坐标
        posi = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi)
    }

    touchMoveNode(event) {
        if(this.isAction){
            return
        }
        let posi = event.getLocation()//世界坐标
        let target = event.target
        posi = target.parent.convertToNodeSpaceAR(posi)
        target.setPosition(posi)
    }

    touchEndNode(event) {
        if(this.isAction){
            return
        }
        let target = event.target;
        if (target['_touchListener']) {
            target['_touchListener'].setSwallowTouches(false);
        }
        let iconInfo = target.iconInfo
        for(let i=0;i<iconInfo.length;i++){
            let personNode = this.node.getChildByName(iconInfo[i].scenes).getChildByName(iconInfo[i].targetYears+'Years')
            if(personNode.active && Common5.checkContainsNode(personNode, target)){
                target.active = false;
                target.recoveparent.active = false;
                for(let j=0;j<iconInfo[i].openNode.length;j++){
                    personNode.getChildByName(iconInfo[i].openNode[j]).active = true
                }
                for(let j=0;j<iconInfo[i].closeNode.length;j++){
                    personNode.getChildByName(iconInfo[i].closeNode[j]).active = false
                }
                this.finishCurStep(iconInfo[i])
                return
            }
        }
        target.parent = target.recoveparent
        target.setPosition(target.recoveposi)
    }
    finishCurStep(iconInfo){
        if(iconInfo.isWrongStep){
            this.isWrong = true
        }
        this.isAction = true
        this.curYearsOrdIndex ++
        this.showQiPao(iconInfo,()=>{
            this.setCurYearsInfo()
        })
        
    }
    setCurYearsInfo(){
        this.isAction = false
        let yearOld = 2
        if(this.curYearsOrdIndex == YearsOldEnum.yo_2){
            this.node.getChildByName('scene1').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene1').getChildByName('2Years'),true)
            yearOld = 2
        }else if(this.curYearsOrdIndex == YearsOldEnum.yo_6){
            this.node.getChildByName('scene1').active = true
            // this.node.getChildByName('scene1').getChildByName('2Years').active = false
            // this.node.getChildByName('scene1').getChildByName('6Years').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene1').getChildByName('2Years'),false)
            this.setNodeFadeInOrOut(this.node.getChildByName('scene1').getChildByName('6Years'),true)
            yearOld = 6
        }else if(this.curYearsOrdIndex == YearsOldEnum.yo_12){
            this.node.getChildByName('scene1').active = true
            // this.node.getChildByName('scene1').getChildByName('6Years').active = false
            // this.node.getChildByName('scene1').getChildByName('12Years').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene1').getChildByName('6Years'),false)
            this.setNodeFadeInOrOut(this.node.getChildByName('scene1').getChildByName('12Years'),true)
            yearOld = 12
        }else if(this.curYearsOrdIndex == YearsOldEnum.yo_17){
            this.node.getChildByName('scene1').active = true
            // this.node.getChildByName('scene1').getChildByName('12Years').active = false
            // this.node.getChildByName('scene1').getChildByName('17Years').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene1').getChildByName('12Years'),false)
            this.setNodeFadeInOrOut(this.node.getChildByName('scene1').getChildByName('17Years'),true)
            yearOld = 17
        }else if(this.curYearsOrdIndex == YearsOldEnum.yo_23){
            this.node.getChildByName('scene1').active = false
            this.node.getChildByName('scene2').active = true
            // this.node.getChildByName('scene2').getChildByName('23Years').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene2').getChildByName('23Years'),true)
            
            yearOld = 23
        }else if(this.curYearsOrdIndex == YearsOldEnum.yo_27){
            this.node.getChildByName('scene2').active = true
            // this.node.getChildByName('scene2').getChildByName('23Years').active = false
            // this.node.getChildByName('scene2').getChildByName('27Years').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene2').getChildByName('23Years'),false)
            this.setNodeFadeInOrOut(this.node.getChildByName('scene2').getChildByName('27Years'),true)
            yearOld = 27
        }else if(this.curYearsOrdIndex == YearsOldEnum.yo_35_1 || this.curYearsOrdIndex == YearsOldEnum.yo_35_2){
            this.node.getChildByName('scene2').active = true
            // this.node.getChildByName('scene2').getChildByName('27Years').active = false
            // this.node.getChildByName('scene2').getChildByName('35Years').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene2').getChildByName('27Years'),false)
            this.setNodeFadeInOrOut(this.node.getChildByName('scene2').getChildByName('35Years'),true)
            yearOld = 35
        }else if(this.curYearsOrdIndex == YearsOldEnum.yo_60_1 || this.curYearsOrdIndex == YearsOldEnum.yo_60_2){
            this.node.getChildByName('scene2').active = false
            this.node.getChildByName('scene3').active = true
            // this.node.getChildByName('scene3').getChildByName('60Years').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene3').getChildByName('60Years'),true)
            yearOld = 60
        }else if(this.curYearsOrdIndex == YearsOldEnum.yo_80_1 || this.curYearsOrdIndex == YearsOldEnum.yo_80_2 || this.curYearsOrdIndex == YearsOldEnum.yo_80_3){
            this.node.getChildByName('scene3').active = true
            // this.node.getChildByName('scene3').getChildByName('60Years').active = false
            // this.node.getChildByName('scene3').getChildByName('80Years').active = true
            this.setNodeFadeInOrOut(this.node.getChildByName('scene3').getChildByName('60Years'),false)
            this.setNodeFadeInOrOut(this.node.getChildByName('scene3').getChildByName('80Years'),true)
            yearOld = 80
        }else if(this.curYearsOrdIndex == YearsOldEnum.Finish){
            yearOld = 80
            this.scheduleOnce(()=>{
                console.log('进入黑屏???')
                this.heiPin.active = true
                if(this.isWrong){
                    this.showQiPao(this.duihuaArrayConfig[1],()=>{
                        this.endGameView(0)
                    })
                }else{
                    this.showQiPao(this.duihuaArrayConfig[0],()=>{
                        this.endGameView(1)
                    })
                }
            },1)
            
        }
        this.yearStr.getComponent(cc.Label).string = `${yearOld}岁`
    }
    setNodeFadeInOrOut(node,bIn){
        if(bIn){
            if(node.active){
                return
            }
            node.active = true
            node.opacity = 0
            cc.tween(node)
                .to(0.5,{opacity:255})
            .start()
        }else{
            if(!node.active){
                return
            }
            cc.tween(node)
                .to(0.5,{opacity:0})
                .call(()=>{
                    node.active = false
                })
            .start()
        }
    }
}