var cfg = require("Configure")

cc.Class({
    extends: cc.Component,

    properties: {
        scoreSp: {
            default: null,
            type: cc.Sprite
        },
        budingTypeSp: {
            default: null,
            type: cc.Sprite
        },

        obstacleType: 0,
        colorType: 0,

        obstacleNum: 0,
        curColorType: 0,
        updateTime: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    //初始化障碍物 
    //type:障碍物类型(随机)
    initObstacle: function (type, obstacleNum) {
        this.obstacleNum = obstacleNum;
        if (obstacleNum == 1) {
            this.budingTypeSp.node.active = false;
        }

        this.obstacleType = type;
        switch (type) {
            case cfg.obstacleType.circle0:
            case cfg.obstacleType.square:
                this.initCircle0(type);
                break;
            case cfg.obstacleType.circle1:
            case cfg.obstacleType.circle2:
            case cfg.obstacleType.circle3:
                this.initCircle1(type);
                break;
            case cfg.obstacleType.translation:
                this.initTranslation(type);
                break;

            default:
                break;
        }
    },

    initCircle0: function (type) {
        var d = this.randDir();
        var t = this.randType();

        var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].rotTime : cfg.obstacleProperty[type].rotTime;
        var config = {
            dir: d,
            type: t,
            angle: cfg.obstacleProperty[type].rotAngle,
            time: dt
        }

        var circleMode = this.node.getChildByName("Circle").getComponent("CircleMode");
        circleMode.initProperties(config);
    },

    initCircle1: function (type) {
        var t = this.randType();
        var d = this.randDir();

        var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].rotTime : cfg.obstacleProperty[type].rotTime;
        var config1 = {
            dir: d,
            type: t,
            angle: cfg.obstacleProperty[type].rotAngle,
            time: dt,
        }

        var config2 = {
            dir: -d,
            type: t,
            angle: cfg.obstacleProperty[type].rotAngle,
            time: dt,
        }

        var circleMode1 = this.node.getChildByName("Circle1").getComponent("CircleMode");
        circleMode1.initProperties(config1);

        var circleMode2 = this.node.getChildByName("Circle2").getComponent("CircleMode");
        circleMode2.initProperties(type == cfg.obstacleType.circle1 ? config1 : config2);

    },


    initTranslation: function (type) {
        var d = this.randDir();
        var dt = this.obstacleNum > 50 ? cfg.obstacleProperty2[type].moveTime : cfg.obstacleProperty[type].moveTime;
        var config1 = {
            dir: d,
            distance: cfg.obstacleProperty[type].moveDistance,
            time: dt,
        }

        var config2 = {
            dir: -d,
            distance: cfg.obstacleProperty[type].moveDistance,
            time: dt,
        }

        for (let i = 0; i < 4; i++) {
            var transMode = this.node.getChildByName("Translation" + i).getComponent("TranslationMode");
            transMode.initProperties((i == 0 || i == 1) ? config1 : config2);
        }
    },

    randDir: function () {
        return Math.round(Math.random()) == 0 ? 1 : -1;
    },

    randType: function () {
        if (this.obstacleNum >= 10) {
            return Math.round(Math.random());
        } else {
            return 0;
        }
    },


    pauseObstacleAction: function () {
        switch (this.obstacleType) {
            case cfg.obstacleType.circle0:
            case cfg.obstacleType.square:
                this.node.getChildByName("Circle").getComponent("CircleMode").pauseAction();
                break;
            case cfg.obstacleType.circle1:
            case cfg.obstacleType.circle2:
            case cfg.obstacleType.circle3:
                this.node.getChildByName("Circle1").getComponent("CircleMode").pauseAction();
                this.node.getChildByName("Circle2").getComponent("CircleMode").pauseAction();
                break;
            case cfg.obstacleType.translation:
                this.node.getChildByName("Translation0").getComponent("TranslationMode").pauseAction();
                this.node.getChildByName("Translation1").getComponent("TranslationMode").pauseAction();
                this.node.getChildByName("Translation2").getComponent("TranslationMode").pauseAction();
                this.node.getChildByName("Translation3").getComponent("TranslationMode").pauseAction();
                break;

            default:
                break;
        }
    },
    stopObstacleAction: function () {
        switch (this.obstacleType) {
            case cfg.obstacleType.circle0:
            case cfg.obstacleType.square:
                this.node.getChildByName("Circle").stopAllActions();
                break;
            case cfg.obstacleType.circle1:
            case cfg.obstacleType.circle2:
            case cfg.obstacleType.circle3:
                this.node.getChildByName("Circle1").stopAllActions();
                this.node.getChildByName("Circle2").stopAllActions();
                break;
            case cfg.obstacleType.translation:
                this.node.getChildByName("Translation0").stopAllActions();
                this.node.getChildByName("Translation1").stopAllActions();
                this.node.getChildByName("Translation2").stopAllActions();
                this.node.getChildByName("Translation3").stopAllActions();
                break;

            default:
                break;
        }
    },

    resumeObstacleAction: function () {
        switch (this.obstacleType) {
            case cfg.obstacleType.circle0:
            case cfg.obstacleType.square:
                this.node.getChildByName("Circle").getComponent("CircleMode").resumeAction();
                break;
            case cfg.obstacleType.circle1:
            case cfg.obstacleType.circle2:
            case cfg.obstacleType.circle3:
                this.node.getChildByName("Circle1").getComponent("CircleMode").resumeAction();
                this.node.getChildByName("Circle2").getComponent("CircleMode").resumeAction();
                break;
            case cfg.obstacleType.translation:
                this.node.getChildByName("Translation0").getComponent("TranslationMode").resumeAction();
                this.node.getChildByName("Translation1").getComponent("TranslationMode").resumeAction();
                this.node.getChildByName("Translation2").getComponent("TranslationMode").resumeAction();
                this.node.getChildByName("Translation3").getComponent("TranslationMode").resumeAction();
                break;

            default:
                break;
        }
    },


    getType: function () {
        return this.obstacleType;
    },

    getColorType: function () {
        return this.curColorType;
    },


    moveObstacle: function (movePosY) {
        this.node.runAction(cc.moveBy(0.5, cc.p(0, movePosY)));
    },

    outScreen: function () {
        switch (this.obstacleType) {
            case cfg.obstacleType.circle0:
            case cfg.obstacleType.circle1:
            case cfg.obstacleType.translation:
                return this.node.y < -1000;
                break;
            case cfg.obstacleType.circle2:
                return this.node.y < -1100;
                break;
            case cfg.obstacleType.circle3:
                return this.node.y < -1720;
                break;
            case cfg.obstacleType.square:
                return this.node.y < -920;
                break;
            default:
                return false;
                break;
        }
    },


    start() {

    },


    update(dt) {
        this.updateTime += dt;

        //每隔1秒变1次色
        if (this.updateTime >= 1) {
            //变色块变色
            this.colorType++;

            if (this.colorType > 3) {
                this.colorType = 0;
            }

            if (this.obstacleType == cfg.obstacleType.circle3 || this.obstacleType == cfg.obstacleType.circle2) {
                if (this.colorType == 1 || this.colorType == 3) {
                    this.colorType = Math.floor(Math.random() * 2) == 0 ? 0 : 2;
                }

            }


            var colorType = this.colorType;
            var self = this;
            cc.loader.loadRes(cfg.ColorTypePath[colorType], cc.SpriteFrame, function (err, spriteFrame) {
                self.budingTypeSp.spriteFrame = spriteFrame;
                self.curColorType = colorType;
            })

            this.updateTime = 0;
        }


        // if (this.node.active && this.outScreen()) 
        // {
        //     this.node.active = false;
        //     this.node.parent.getComponent("GameSence").destroyObstacle();
        // }

    }

});