
const {ccclass, property} = cc._decorator;

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




@ccclass
export default class NewClass extends cc.Component {


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

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

    private secCounter: number = 0;
    private fpsCount: number = 0;

    private gameModuleScript;
    private columnControlScript;

    spinCondition:columnCondition = {        
        finalResult:[3,2,1],
        delayTime:100,
        accelerateTime:0.5,
        slowdownTime:0.5,
        holdspeedTime:0.5,
        N:2}; 

    protected onLoad () {
        cc.log("this is ee onLoad");
        // 呼叫其他 script 的方法或資源
        this.gameModuleScript  = this.gameModule.getComponent('gameMY')
        this.columnControlScript = this.columnControl.getComponent('columnControl')
    }

    protected onEnable(){
        cc.log("this is ee onEnable");
    }

    protected start () {
        cc.log("this is ee start");
        this.secCounter = 0; 
        // this.columnControlScript.spinCondition = this.spinCondition;
    }

    tempValue = true

    protected update (dt) {
        this.secCounter += dt;
        this.fpsCount += 1;

        // 測試 cc.resources.loadDir 載入時間 ==> 結論: 約需要 0.2~0.5秒
        if(this.tempValue){
            if(!(Object.keys(this.gameModuleScript.item_spriteFrame).length === 0)){
                cc.log(" .loadDir Complete : " + this.secCounter)
                this.tempValue = false
            }
        }

        // if(this.secCounter >= 5) {
        //     cc.log("engineer Function Update : " + this.fpsCount)
        //     this.secCounter = 0
        //     this.fpsCount = 0
        // }   
        // cc.log(dt)

        if(this.test7on){

            if(this.secCounter > 2){
                this.columnControlScript.nowState = columnState.Slowing;
                this.secCounter = 0;
            }

            let a = this.columnControlScript.updateSpeedFactor(this.secCounter);
            cc.log("SF:" + a + ". T:" + this.secCounter);

        }else if(this.secCounter >= 5){
            cc.log("engineer Function Update : " + this.fpsCount)
            this.secCounter = 0
            this.fpsCount = 0
        }

    } 

    protected onDisable(): void {
        cc.log("this is ee onDisable")
    }

    unitTest1():void {
        this.gameModule.emit('Bob', 'unitTest1 Success')
    }

    disableColumn():void {
        // this.node.active = !this.node.active;
        this.columnControl.active = !this.columnControl.active;
    }

    unitTest3():void {

        // @ts-ignore
        this.itemPool = Object.values(this.gameModuleScript.itemNameMapping)

        for(let i=0;i<this.columnControl.childrenCount;i++){
            let a = this.itemPool[Math.floor(Math.random() * 12)];
            this.columnControlScript.currentList.pop( a );
            this.columnControlScript.currentList.unshift( a );
            let c = this.gameModuleScript.item_spriteFrame[a]
            this.columnControl.children[i].getComponent(cc.Sprite).spriteFrame = c
        }
        
    }

    test4():void {
        this.node.getChildByName('UT4').y += 100;
        cc.log("markA")
    }

    tempColumnState:any;
    test5():void {
        if(this.columnControlScript.nowState == 99){
            this.columnControlScript.nowState = this.tempColumnState;
        }else{
            this.tempColumnState = this.columnControlScript.nowState;
            this.columnControlScript.nowState = 99;
        }
        
    }

    test6():void {
        // this.columnControl.active = false;
        this.columnControlScript.spinCondition = this.spinCondition;
        this.columnControlScript.finalList = [...this.spinCondition.finalResult];
        this.columnControlScript.N = this.spinCondition.N;
        this.columnControlScript.nowState = columnState.Delay; 
        setTimeout(()=>{ 
            this.columnControlScript.nowState = columnState.Spining; 
        }, this.spinCondition.delayTime);
    }

    test7on:boolean = false;
    test7():void {
        this.test7on = !this.test7on;
        this.secCounter = 0
    }


    otherLoadingMethod(){

        const symbol = {
            1: 'item_01',
            2: 'item_02',
            3: 'item_03',
            4: 'item_04',
            5: 'item_a',
            6: 'item_k',
            7: 'item_q',
            8: 'item_j',
            9: 'item_9',
            10: 'item_10',
            11: 'item_scatter',
            12: 'item_wild',
        }

        const symbolSpriteList: Map<string, cc.SpriteFrame> = new Map()

        Object.keys(symbol).forEach((k, i) => {
            cc.resources.load(`poker/cat/${symbol[k]}`, cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
                symbolSpriteList.set(k, sp)
            })
        })
    }



}



    // 棄置的扣

    // 動畫相關

    // actionSet = {
    //     "stayPosition": cc.moveTo(0, new cc.Vec2(0, 240)),
    //     "rollingUp2Button": cc.sequence(cc.moveTo(0, new cc.Vec2(0, 240)), cc.moveBy(1.5, new cc.Vec2(0, -240))),
    // };

            // if(this.nowAction.isDone() == true) {
            // let a = this.itemList[Math.floor(Math.random() * 12)];
            // this.columnItem.unshift( a );
            // this.columnItem.pop();

            // //換圖
            // 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.nowAction = this.node.runAction(this.actionSet["rollingUp2Button"]);
        // }else{

        // }

            // 用 tween 做動畫
        // cc.tween(b)
        //     .to(1, { position: cc.v2(100, 0), rotation: 360 })
        //     .to(1, { position: cc.v2(0, 0), rotation: 0 })
        //     // .to(1, { scale: 2 })
        //     .start()
        
        // 用 Action 做動畫
        // var seq = cc.sequence(cc.moveBy(1.5, cc.Vec2(100, 50)), cc.moveBy(1.5, cc.Vec2(-100, -50)));
        // b.runAction(this.seq)
        // c.runAction(this.seq) // 失敗 seq 不能一次賦予兩個物件一樣動作
        // this.bigFrameString.node.runAction(seq)


