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 GameReport from "../../../../FrameWork/Report/ZyZyReport";
import Common from "../../../../FrameWork/Util/Common";
import Common5 from "../../../../Platform/th/Common5";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
import CccGame from "../../../WordGame/gameComScript/CccGame/CccGame";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";

// import BangLaoNaiNaiGuoHeConfig from "./BangLaoNaiNaiGuoHeConfig";


const { ccclass, property } = cc._decorator;

@ccclass
export default class BangLaoNaiNaiGuoHe extends WordGameBaseComponent {

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

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

    @property(cc.Node)
    checkNodeList: cc.Node[] = [];

    @property(cc.Node)
    animation: cc.Node[] = [];

    @property(cc.Node)
    touchNodeList: cc.Node[] = [];

    @property(cc.Node)
    operatedNodeList: cc.Node[] = [];

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

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

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

  
    onLoad(): void {
   
    }

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

            this.jsonData =assest.json

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

    //初始化参数
    initParameters() {
        this.curSchedule = 0;

        this.titleArrayConfig = this.jsonData.titleArray//BangLaoNaiNaiGuoHeConfig.getInstance().getGameConfig('titleArray') as string[];
        this.chadianArrayConfig = this.jsonData.chadianArray//BangLaoNaiNaiGuoHeConfig.getInstance().getGameConfig('chadianArray') as any[];
        this.duihuaArrayConfig = this.jsonData.duihuaArray//BangLaoNaiNaiGuoHeConfig.getInstance().getGameConfig('duihuaArray') as any[];
        Common.Type = 0
        Common.subLevel = 0;
        Common.GameSubTipConfigs = this.jsonData.tipsArray//BangLaoNaiNaiGuoHeConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = this.jsonData.answersArray//BangLaoNaiNaiGuoHeConfig.getInstance().getGameConfig('answersArray');

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

        EventMgr.onEvent_custom(ryw_Event.NormalTouchEndCheck, (data_) => {
            this.nodeTouchCallback(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.titleArrayConfig[0];
    
        let title = this.titleArrayConfig[0];
        Game.ins.setGameTitle(title)
        
        for (const iterator of this.animation) {
            iterator.active = false;
        }

        this.initTouchEvent(this.role, this.checkNodeList, this.node.getChildByName('logPrefab'));

    }



    nodeTouchCallback(targetNode: any) {

    
   
        switch (targetNode.name) {
            case '虾':
                this.node.getChildByName("mask").active = true;
                this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                CccGame.playAnimation(this.animation[0], '虾');
                Common5.playEffectCustom("banglaonainaiguohe", 'sound/皮皮虾过河');
                this.scheduleOnce(() => {
                    this.showDuiHua(this.chadianArrayConfig[0][0], () => {
                        this.animation[0].active = false;
                        this.node.getChildByName("mask").active = false;
                        this.determine();
                    });
                }, 0);
                break;
            case '老奶奶':
                GameReport.BtnsReport(targetNode.name, Common5.selectGameInfo.titleUrl);
                console.log("[GameReport]++++++++++++++++++++++>" + targetNode.name);
                if (targetNode.checkNode.name == '电杆box') {
                    if (this['closeSwitch']) {
                        this.node.getChildByName("mask").active = true;
                        this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                        CccGame.playAnimation(this.animation[1], '电线');
                        this.scheduleOnce(() => {
                            this.showDuiHua(this.chadianArrayConfig[1][0], () => {
                                this.animation[1].active = false;
                                this.node.getChildByName("mask").active = false;
                                this.determine();
                            });
                        }, 1);
                    } else {
                        this.node.getChildByName("mask").active = true;
                        this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                        CccGame.playAnimation(this.animation[1], '电线被劈中');
                        Common5.playEffectCustom("banglaonainaiguohe", 'sound/电糊');
                        this.scheduleOnce(() => {
                            this.animation[1].active = false;
                            this.node.getChildByName("mask").active = false;
                            this.endGameView(0);
                            return;
                        }, 3);
                    }
                } else if (targetNode.checkNode.name == '木头box') {
                    this.node.getChildByName("mask").active = true;
                    this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                    this.operatedNodeList[0].active = false;
                    CccGame.playAnimation(this.animation[0], '木头2');
                    this.scheduleOnce(() => {
                        this.showDuiHua(this.chadianArrayConfig[3][0], () => {
                            this.animation[0].active = false;
                            this.node.getChildByName("mask").active = false;
                            this.determine();
                        });
                    }, 1);
                }
                else if (targetNode.checkNode.name == '丝袜1') {
                    this.node.getChildByName("mask").active = true;
                    this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                    CccGame.playAnimation(this.animation[0], '丝袜');
                    Common5.playEffectCustom("banglaonainaiguohe", 'sound/丝袜弹射');
                    this.scheduleOnce(() => {
                        this.showDuiHua(this.chadianArrayConfig[6][0], () => {
                            this.animation[0].active = false;
                            this.node.getChildByName("mask").active = false;
                            this.determine();
                        });
                    }, 1);
                }
                break;
            case '鸟':
                this.node.getChildByName("mask").active = true;
                this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                CccGame.playAnimation(this.animation[0], '老鹰');
                this['hasBird'] = true;
                this.scheduleOnce(() => {
                    this.showDuiHua(this.chadianArrayConfig[2][0], () => {
                        this.animation[0].active = false;
                        this.node.getChildByName("mask").active = false;
                        this.determine();
                    });
                }, 1);
                break;
            case '关电闸':
                this['closeSwitch'] = true;
                break;
            case '巴豆':
                this.node.getChildByName("mask").active = true;
                this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                CccGame.playAnimation(this.animation[0], '巴豆');
                Common5.playEffectCustom("banglaonainaiguohe", 'sound/放屁');
                this.scheduleOnce(() => {
                    this.showDuiHua(this.chadianArrayConfig[4][0], () => {
                        this.animation[0].active = false;
                        this.node.getChildByName("mask").active = false;
                        this.determine();
                    });
                }, 1);
                break;
            case '吸管':
                this.node.getChildByName("mask").active = true;
                this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                CccGame.playAnimation(this.animation[0], '吸管');
                this.scheduleOnce(() => {
                    this.showDuiHua(this.chadianArrayConfig[5][0], () => {
                        this.animation[0].active = false;
                        this.node.getChildByName("mask").active = false;
                        this.determine();
                    });
                }, 1);
                break;
            case '大爷':
                this.node.getChildByName("mask").active = true;
                CccGame.playAnimation(this.animation[2], '大爷待机', true);
                this.showDuiHua(this.duihuaArrayConfig[1], () => {
                    this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                    CccGame.playAnimation(this.animation[2], '大爷');
                    this.scheduleOnce(() => {
                        this.showDuiHua(this.chadianArrayConfig[7][0], () => {
                            this.animation[2].active = false;
                            this.node.getChildByName("mask").active = false;
                            this.determine();
                        });
                    }, 1);
                });
                break;
            case '游泳圈':
                this.node.getChildByName("mask").active = true;
                this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                CccGame.playAnimation(this.animation[0], '游泳圈');
                this.scheduleOnce(() => {
                    this.showDuiHua(this.chadianArrayConfig[8][0], () => {
                        this.animation[0].active = false;
                        this.node.getChildByName("mask").active = false;
                        this.determine();
                    });
                }, 1);
                break;
            case '鱼':
                this.node.getChildByName("mask").active = true;
                this.showDuiHua(this.duihuaArrayConfig[0], () => {
                    targetNode['attrNodeArray'][0].active = false;
                    CccGame.playAnimation(this.animation[3], '土拨鼠');
                    this.scheduleOnce(() => {
                        this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
                        CccGame.playAnimation(this.animation[3], '土拨鼠2');
                        this.scheduleOnce(() => {
                            this.showDuiHua(this.chadianArrayConfig[9][0], () => {
                                this.animation[3].active = false;
                                this.node.getChildByName("mask").active = false;
                                this.determine();
                            });
                        }, 1);
                    }, 1);
                });
                break;
            default:
                break;
        }
    }

    determine() {
        this.curSchedule++;
        if (this.curSchedule == this.chadianArrayConfig.length - 1 && !this['hasBird']) {
            this.endGameView(0);
        } else if (this.curSchedule == this.chadianArrayConfig.length) {
            this.endGameView(1);
        }
    }

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

        if (curLog.effectUrl) {
            Common5.playEffectCustom("banglaonainaiguohe", 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);
        }
    }

    initTouchEvent(node, checkNodeList, moveNode) {
        var attrs = {
            checkNodeList: checkNodeList,
            moveNode: moveNode,
            recoveposi: node.getPosition(),//初始位置
            recoveparent: node.parent,
            scr: this   //脚本
        };
        node.attr(attrs);
        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) {
        Common5.playEffect("点击音效");
        let target = event.target;
        let posi = event.getLocation();//世界坐标
        posi = target.parent.convertToNodeSpaceAR(posi);
        target.setPosition(posi);
        target.opacity = 255;
        target.scaleX = 1;
        if (target.moveNode) {
            target.parent = target.moveNode;
        }

        if (target['_touchListener']) {
            target['_touchListener'].setSwallowTouches(true);
        }

        this.props.children[this.props.children.length - 1 - this.curSchedule].active = false;
    }

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

    touchEndNode(event) {
        let target = event.target;
        let checkNodeList = target.checkNodeList as cc.Node[];
        let checkNode = null;

        for (let iterator of checkNodeList) {
            if (iterator.active && Common5.checkContainsNode(iterator, target)) {
                checkNode = iterator;
                break;
            }
        }

        if (checkNode) {
            checkNode.active = false;
            target.opacity = 0;
            target['checkNode'] = checkNode;
            this.nodeTouchCallback(target);
        }
        else {
            this.props.children[this.props.children.length - 1 - this.curSchedule].active = true;
        }

        if (target['_touchListener']) {
            target['_touchListener'].setSwallowTouches(false);
        }

        if (target["recoveposi"]) {
            if (target["recoveparent"]) {
                this.nodeMoveToRecovery(target, target["recoveposi"], target["recoveparent"])
            } else {
                this.nodeMoveToRecovery(target, target["recoveposi"])
            }
        }
    }

    nodeMoveToRecovery(node, oldPosi: cc.Vec2, recoveparent?: cc.Node, func?: Function) {
        cc.tween(node)
            .to(0.1, { x: oldPosi.x, y: oldPosi.y })
            .set({ parent: recoveparent, opacity: 0, scaleX: 3 })
            .call(() => {
                if (func) {
                    func()
                }
            })
            .start();
    }

    onClick(event) {
        let target = event.target as cc.Node;
        console.log("+++++++++++++++++++++>" + target.name);
        Common5.playEffect("点击音效");
        switch (target.name) {
            case "BtnAddTimes":
            default:
                break;
        }
    }
    //update(dt) { }
}