import { _decorator, Component, Node, Sprite } from 'cc';
import { ui_att_item } from '../../../data/data';
import { gameManager } from '../gameManager';
const { ccclass, property } = _decorator;

@ccclass('rule_view')
export class rule_view extends Component {
    @property(Node) btn_rule_left:Node = null;
    @property(Node) btn_rule_right:Node = null;
    @property(Node) btn_rule_close:Node = null;
    @property(Node) icon:Node = null;
    private m_page = 0;
    private mData :ui_att_item[] = null;
    public initView(rule_list:ui_att_item[]){
        this.mData = rule_list;
        this.btn_rule_left.on(Node.EventType.TOUCH_END,()=>{
            if( (this.m_page-1)<0){
                
            }else{
                this.m_page-=1;
            }
            this.update_func()
            this.updateBtnStatus()
            gameManager.Singleton.sys_click_button_music()
        })
        this.btn_rule_right.on(Node.EventType.TOUCH_END,()=>{
            if( (this.m_page+1)>=this.mData.length){
 
            }else{
                this.m_page+=1;
            }
            this.update_func()
            this.updateBtnStatus()
            gameManager.Singleton.sys_click_button_music()
        })
        this.btn_rule_close.on(Node.EventType.TOUCH_END,()=>{
            this.close()
            gameManager.Singleton.sys_click_button_music()
        })
        if(this.mData.length<=0){
            this.m_page =0
        }else{
            this.m_page = this.mData.length-1;
        }
        this.update_func()
        this.updateBtnStatus()
    }

    show(){
        this.node.active = true;
    }

    close(){
        this.node.active = false;
    }
    updateBtnStatus(){
        if(this.mData.length<=1){
            this.btn_rule_right.active = false;
            this.btn_rule_left.active = false;
        }else{
            this.btn_rule_right.active = true;
            this.btn_rule_left.active = true;
            if(this.m_page>=this.mData.length-1){
                this.btn_rule_right.active = false;
            }
            if(this.m_page<=0){
                this.btn_rule_left.active = false;
            }
        }
    }

    update_func(){
        let curSelectItem = this.mData[this.m_page];
        this.icon.getComponent(Sprite).spriteFrame = gameManager.getCacheSpriteFrameByName(curSelectItem.res)
    }
}


