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 WordGameBaseComponent from "../../../../FrameWork/Base/WordGameBaseComptent";
import logPrefabScript from "../../../WordGame/gameComScript/logPrefabScript";
import DaDianScript from "../../../../FrameWork/Base/DaDianScript";
import Game from "../../../../Scripts/WenZiRes/ui/Game";
import CccGame from "../../../WordGame/gameComScript/CccGame/CccGame";
import ChaoSuPoSanLunConfig from "./ChaoSuPoSanLunConfig";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ChaoSuPoSanLun extends WordGameBaseComponent {

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

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

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

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

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

    //文本配置
    titleArrayConfig: string[] = [];
    duihuaArrayConfig: any[] = [];
    chadianArrayConfig: any[] = [];
    bundle = null
    start() {
        this.bundle = Common5.gameConfig.zmGameConfig[Common5.selectGameNum].bundle
        super.start();
        DaDianScript.userEnterDaDian();
        this.initParameters();
        this.initComponent();

        //Common5.playMusic('nainaideai/背景音感人2', 'WordGame');
    }

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

        this.titleArrayConfig = ChaoSuPoSanLunConfig.getInstance().getGameConfig('titleArray') as string[];
        this.chadianArrayConfig = ChaoSuPoSanLunConfig.getInstance().getGameConfig('chadianArray') as any[];
        this.duihuaArrayConfig = ChaoSuPoSanLunConfig.getInstance().getGameConfig('duihuaArray') as any[];
        Common.Type = 0;
        Common.subLevel = 0;
        Common.GameSubTipConfigs = ChaoSuPoSanLunConfig.getInstance().getGameConfig('tipsArray');
        Common.GameSubAnswerConfigs = ChaoSuPoSanLunConfig.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.DirectTouchMoveCheck, (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];
        Game.ins.setGameTitle(this.titleArrayConfig[0])

        this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";

        this.node.getChildByName("mask").active = true;
        cc.tween(this.operatedNodeList[0])
            .set({ active: true, position: cc.v3(-1500) })
            .to(1, { position: cc.v3(0) })
            .call(() => {
                this.showDuiHua(this.duihuaArrayConfig[0], () => {
                    this.showDuiHua(this.duihuaArrayConfig[1], () => {
                        this.showDuiHua(this.duihuaArrayConfig[2], () => {
                            cc.tween(this.operatedNodeList[0])
                                .to(1, { position: cc.v3(1500) })
                                .call(() => {
                                    this.node.getChildByName("mask").active = false;
                                })
                                .start();
                        })
                    })
                })
            })
            .start();

        this.scheduleOnce(this.setTimeNodePos, 0);
    }

    setTimeNodePos() {
        // let timeNode = this.node.getChildByName("timeNode")
        // let addtimeNode = this.node.getChildByName("tipsBtn").getChildByName("addtimeNode")
        // let timeNodePos = timeNode.getPosition()
        // let addtimeNodePos = addtimeNode.getPosition()
        // Game.ins.setTimePos(timeNodePos.x, timeNodePos.y);
        // Game.ins.setTimeScale(0.7)
        // Game.ins.setTimeBtnPos(addtimeNodePos.x, addtimeNodePos.y);
    }

    nodeTouchCallback(targetNode: cc.Node) {
        switch (targetNode.name) {
            case '伞':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[0].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[0], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '榴莲':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[1].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[1], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '脚':
                this.node.getChildByName('mask').active = true;
                CccGame.playAnimation2(this.skeletonNodeList[2], '女没伞抬脚', true);
                this.curSpeed += this.chadianArrayConfig[2].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[2], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '亲嘴':
                this.node.getChildByName('mask').active = true;
                CccGame.playAnimation2(this.skeletonNodeList[2], '女亲嘴没伞抬脚');
                this.scheduleOnce(() => {
                    CccGame.playAnimation2(this.skeletonNodeList[2], '女没伞抬脚', true);
                    this['qinzui'] = true;
                    if (this['zhenglunzi']) {
                        CccGame.playAnimation2(this.skeletonNodeList[0], '被亲正轮子', true);
                    } else {
                        CccGame.playAnimation2(this.skeletonNodeList[0], '被亲歪轮子', true);
                    }
                    this.curSpeed += this.chadianArrayConfig[5].addSpeed;
                    this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                    this.showDuiHua(this.chadianArrayConfig[5], () => {
                        this.node.getChildByName('mask').active = false;
                        this.curSchedule++;
                        this.determine();
                    });

                    const speedThreshold = [400, 800, 1200, 1600, 2000, 9999]
                    if (this.curSpeed >= speedThreshold[this.curRoleState]) {
                        let circulateNode: any = this.operatedNodeList[1];
                        circulateNode.setSpeed(speedThreshold[this.curRoleState] / 2);
                        this.curRoleState++;
                    };
                }, 1);
                break;
            case '美女':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[3].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[3], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '剪刀':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[4].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[4], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '骑车围巾':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[6].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[6], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '骑车衣服':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[7].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[7], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '废品1':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[8].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[8], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '废品2':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[9].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[9], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '打气筒':
                this.node.getChildByName('mask').active = true;
                this['zhenglunzi'] = true;
                if (this['qinzui']) {
                    CccGame.playAnimation2(this.skeletonNodeList[0], '被亲正轮子', true);
                } else {
                    CccGame.playAnimation2(this.skeletonNodeList[0], '速度1正轮子', true);
                }
                this.curSpeed += this.chadianArrayConfig[10].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[10], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '箱子关':
                this.node.getChildByName('mask').active = true;
                this.curSpeed += this.chadianArrayConfig[11].addSpeed;
                this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                this.showDuiHua(this.chadianArrayConfig[11], () => {
                    this.node.getChildByName('mask').active = false;
                    this.curSchedule++;
                    this.determine();
                });
                break;
            case '狗坐':
                if (targetNode['checkNode'].name == "丢弃碰撞箱") {
                    this.curSchedule++;
                    this.determine();
                } else {
                    this.skeletonNodeList[4].active = true;
                    this.node.getChildByName('mask').active = true;
                    this.curSpeed += 300;
                    this.curSpeedNode.getComponent(cc.Label).string = "速度:" + this.curSpeed + "km/h";
                    this.showDuiHua(this.chadianArrayConfig[12], () => {
                        this.node.getChildByName('mask').active = false;
                        this.curSchedule++;
                        this.determine();
                    });
                }
                break;
            default:
                break;
        }

        const speedThreshold = [400, 800, 1200, 1600, 2000, 9999]
        if (this.curSpeed >= speedThreshold[this.curRoleState]) {
            let circulateNode: any = this.operatedNodeList[1];
            circulateNode.setSpeed(speedThreshold[this.curRoleState] / 2);
            this.curRoleState++;
        }
    }

    determine() {
        if (this.curSchedule == 12) {
            if (this.curSpeed >= 2000) {
                cc.tween(this.operatedNodeList[0])
                    .set({ active: true })
                    .to(1, { position: cc.v3(0) })
                    .call(() => {
                        let circulateNode: any = this.operatedNodeList[1];
                        circulateNode.setSpeed(500);
                        this.showDuiHua(this.duihuaArrayConfig[3], () => {
                            let circulateNode: any = this.operatedNodeList[1];
                            circulateNode.setSpeed(1000);
                            cc.tween(this.operatedNodeList[0])
                                .to(1, { position: cc.v3(-1500) })
                                .call(() => {
                                    this.endGameView(1);
                                })
                                .start();
                        });
                    })
                    .start();
            } else {
                this.endGameView(0);
            }
        }
    }

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

        if (curLog.effectUrl) {
            Common5.playEffectCustom(this.bundle, 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();
            }, 1.5);
        } else {
            this.scheduleOnce(() => {
                Game.ins.showSuccess();
            }, 1.5);
        }
    }

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