
const {ccclass, property} = cc._decorator;

import {gameMY, moduleState} from './gameModule';


export enum columnState {
    Ready = 1,
    Spining = 2,
    Stopping = 3,


    EngStop = 99,

}


@ccclass
export class columnControl extends cc.Component {

    // @property({type: gameMY}) 這句不行
    // public gameModule: gameMY | null = null;

    @property((cc.Node))
    gameModule: cc.Node;

    @property(Number)
    rollSpeed:number = 1;

    // private nowAction = null;
    private columnItem: Array<number> = [];
    private itemList:Array<number>;
    private gameModuleScript:gameMY;
    nowState:columnState;


    protected onLoad () {
        this.gameModuleScript = this.gameModule.getComponent('gameMY');
    }

    protected start () {

        this.itemList = Object.values(this.gameModuleScript.itemNameMapping);

        // column 位置初始化
        this.node.y = this.gameModuleScript.itemSize;

        // 隨機初始化 item 圖片
        for(let i=0;i<this.node.childrenCount;i++){
            let a = this.itemList[Math.floor(Math.random() * 12)];
            this.columnItem.push( a );
            this.node.children[i].getComponent(cc.Sprite).spriteFrame = this.gameModuleScript.item_spriteFrame[a]
        }

        
        this.nowState = columnState.Ready;
    }

    protected update (dt) {

        let dY = Math.ceil(this.rollSpeed * this.gameModuleScript.itemSize * dt)

        switch(this.nowState){
            case columnState.Ready:
                //狀態檢查與更新
                if(this.gameModuleScript.nowState == moduleState.Spining){
                    this.nowState = columnState.Spining;
                }
                break;
            case columnState.Spining:
                if(this.node.y - dY <= 0){
                    this.node.y = this.node.y + this.gameModuleScript.itemSize - dY;
                    //更新圖片
                    this.prepareNextItem();
                    for(let i=0;i<this.columnItem.length;i++){
                        this.node.children[i].getComponent(cc.Sprite).spriteFrame = this.gameModuleScript.itemBlur_spriteFrame[this.columnItem[i]];
                    }
                    //狀態檢查與更新
                    if(this.gameModuleScript.nowState == moduleState.Stopping){
                        this.nowState = columnState.Stopping;
                    }
                }else{this.node.y -= dY;}
                break;
            case columnState.Stopping:
                if(this.node.y - dY <= 0){
                    this.node.y = this.gameModuleScript.itemSize;
                    //更新圖片
                    this.prepareNextItem();
                    for(let i=0;i<this.columnItem.length;i++){
                        this.node.children[i].getComponent(cc.Sprite).spriteFrame = this.gameModuleScript.item_spriteFrame[this.columnItem[i]];
                    }
                    this.gameModule.emit("AColumnStop")
                    //狀態檢查與更新
                    this.nowState = columnState.Ready; // 狀態復歸

                }else{this.node.y -= dY;}
                break;
            case columnState.EngStop:
                // To Nothing just stop whole column
                break;
        }
    }

    prepareNextItem() {
        let a = this.itemList[Math.floor(Math.random() * this.itemList.length)];
        this.columnItem.unshift( a );
        this.columnItem.pop();
    }


}



        