// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property, } = cc._decorator;



export enum moduleState {
        INIT1 = 0,
        INIT2 = 1,
        Ready = 2,
        Spining = 3,
        Stopping = 4,

    };

@ccclass("gameMY")
export class gameMY extends cc.Component {

    @property
    columnCount:number = 5;

    @property
    itemSize:number = 240;

    @property(cc.Label)
    betString: cc.Label = null;

    @property(cc.Label)
    bigFrameString: cc.Label = null;

    @property(cc.Label)
    balanceString: cc.Label = null;

    @property({type: cc.Prefab})
    columnPrefab: cc.Prefab | null = null;

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

    


    private betNumber: number;
    private balanceNumber: number;
    private bigFrameNumber: number;

    private stopCount:number;

    nowState: moduleState = 0;

    // item 圖片資源存放區
    itemNameMapping = {
        'item_01':1, 'item_02':2, 'item_03':3, 'item_04':4, 'item_9':5, 
        'item_10':6, 'item_j':7,  'item_k':8,  'item_q':9,  'item_a':10, 
        'item_scatter':51, 'item_wild':52
    } as const;

    item_spriteFrame = {};
    itemBlur_spriteFrame = {};

    

    protected onLoad () {

        this.unitTestInit(); // for engineer use

        this.onColumnsSignal()

        // 讀取 item 圖片資源
        cc.resources.loadDir('item',cc.SpriteFrame, (err: any, sF) => {
            sF.forEach( (eachSpriteFrame)=>{
                this.item_spriteFrame[ this.itemNameMapping[eachSpriteFrame.name] ] = eachSpriteFrame
            })
        } );
        cc.resources.loadDir('item_blur',cc.SpriteFrame, (err: any, sF) => {
            sF.forEach( (eachSpriteFrame)=>{
                this.itemBlur_spriteFrame[ this.itemNameMapping[eachSpriteFrame.name] ] = eachSpriteFrame
            })
        } );

    }



    protected start () {
        this.betNumber = 50;
        
        this.slotBoard.addChild( cc.instantiate(this.columnPrefab), 1 )
        this.slotBoard.addChild( cc.instantiate(this.columnPrefab), 2 )
        this.slotBoard.addChild( cc.instantiate(this.columnPrefab), 3 )
        this.slotBoard.addChild( cc.instantiate(this.columnPrefab), 4 )

    }

    protected update (dt) {

        switch(this.nowState){
            case moduleState.INIT1:
                // 檢查圖片載入完成後
                // 將必要資訊餵給 column 後再 active column 
                // 再完成初始化
                if((Object.keys(this.item_spriteFrame).length === Object.keys(this.itemNameMapping).length)){
                    for(let i=0; i<this.columnCount; i++){
                        this.slotBoard.children[i].getComponent('columnControl').gameModule = this.node;
                        this.slotBoard.children[i].active = true;
                        this.nowState = moduleState.INIT2;
                    }
                }
                break;
            case moduleState.INIT2:
                for(let i=0; i<this.columnCount; i++){
                    cc.log(this.slotBoard.children[i].x)
                }
                this.nowState = moduleState.Ready;
                break;
            case moduleState.Ready:
                break;
            case moduleState.Spining:
                this.stopCount = 0;
                break;
            case moduleState.Stopping:
                if(this.stopCount == 5){
                    this.nowState = moduleState.Ready;
                }
                break;
            default:
                break;
        }

    } 


    onBetBtnClick(e: cc.Event.EventTouch): void {
        // let a = e.getCurrentTarget().name
        // cc.log(a)
        if(e.getCurrentTarget().name == 'btn_plus'){
            this.betNumber += 50;
        }
        else{
            this.betNumber -= 50;
        }
        this.betString.string = "" + this.betNumber

    }


    onSpinBtnClick():void {

        switch(this.nowState){
            case moduleState.Ready:
                this.nowState = moduleState.Spining;
                break;
            case moduleState.Spining:
                this.nowState = moduleState.Stopping;
                break;
            default:
                cc.log("do nothing");
                break;
        }

    }

    onColumnsSignal():void {
        this.node.on('AColumnStop', ()=>{
            this.stopCount += 1;
        }, this)
    }









    unitTestInit():void {
        // for unitTest 1
        this.node.on('Bob', (arg1)=>{
            cc.log("gameModule Recieve Bob say " + arg1)
        }, this)
    }




}
