import { _decorator, Component, Game, Label, Node, PageView } from 'cc';
import { Player } from './Player';
import { GameConstant } from './GameConstant';
import { SoundManager } from './SoundManager';
const { ccclass, property } = _decorator;

@ccclass('GameRule')
export class GameRule extends Component {
    @property(PageView)
    pageView: PageView = null;

    @property(Node)
    iconPaytableLayout1: Node = null;

    @property(Node)
    iconPaytableLayout2: Node = null;

    @property(SoundManager)
    soundManager: SoundManager = null;
    
    public curPageIndex = 0;
    start() {
        // this.pageView.enabled = false;
    }

    show(curBetIndex) {
        this.node.active = true;
        for (let i = 0; i < this.iconPaytableLayout1.children.length; i++) {
            let paytable = this.iconPaytableLayout1.children[i].getChildByName("paytableLayout");
            for (let j = 0; j < paytable.children.length; j++) {
                let scoreNode = paytable.children[j].getChildByName("score").getComponent(Label);
                let score = Player.getInstance().betConfig[curBetIndex] * GameConstant.ICON_TYPE_TIMES[3 - j][i];
                scoreNode.string = GameConstant.formatNumber(score, {currency: Player.getInstance().currencyType});
            }
        }

        for (let i = 0; i < this.iconPaytableLayout2.children.length; i++) {
            let paytable = this.iconPaytableLayout2.children[i].getChildByName("paytableLayout");
            for (let j = 0; j < paytable.children.length; j++) {
                let scoreNode = paytable.children[j].getChildByName("score").getComponent(Label);
                let score = Player.getInstance().betConfig[curBetIndex] * GameConstant.ICON_TYPE_TIMES[3 - j][5 + i];
                scoreNode.string = GameConstant.formatNumber(score, {currency: Player.getInstance().currencyType});
            }
        }
    }

    onClickGameRuleLeft() {
        this.soundManager.playClick();
        this.curPageIndex = this.pageView.getCurrentPageIndex();
        this.curPageIndex--;
        if (this.curPageIndex < 0) {
            this.pageView.setCurrentPageIndex(this.pageView.content.children.length - 1);
        } else {
            this.pageView.scrollToPage(this.curPageIndex);
        }
    }

    onClickGameRuleClose() {
        this.soundManager.playClick();
        this.node.active = false;
    }

    onClickGameRuleRight() {
        this.soundManager.playClick();
        this.curPageIndex = this.pageView.getCurrentPageIndex();
        this.curPageIndex++;
        if (this.curPageIndex > this.pageView.content.children.length - 1) {
            this.pageView.setCurrentPageIndex(0);
        } else {
            this.pageView.scrollToPage(this.curPageIndex);
        }
    }
}



