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

@ccclass('level_list_item')
export class level_list_item extends Component {
    @property(Node) icon:Node = null;
    @property(Node) game_unlock:Node = null;
    @property(Node) img_game_list_new:Node = null;
    @property(Node) img_category_id:Node = null;
    @property(Node) img_game_re:Node = null;
    @property(Node) lab_name:Node = null;

    @property(SpriteFrame) kong_bu:SpriteFrame = null;
    @property(SpriteFrame) ju_qing:SpriteFrame = null;
    @property(SpriteFrame) huai_jiu:SpriteFrame = null;
    @property(SpriteFrame) nao_dong:SpriteFrame = null;
    @property(SpriteFrame) tui_li:SpriteFrame = null;


    private m_click_cb = null;
    private m_data:LevelItemData = null;
    private isUnLock:boolean = true;

    public off_Touch(){
        this.node.off(Node.EventType.TOUCH_END)
    }

    public getIsUnLock(){
        return this.isUnLock;
    }

    public UnLock(){
        this.isUnLock = true;
        gameManager.Singleton.unLockLevel(this.m_data.id)
        this.updateLockStatus()
    }

    public on_Touch(){
        this.node.off(Node.EventType.TOUCH_END)
        this.node.on(Node.EventType.TOUCH_END,(et:TouchEvent)=>{
            if(this.m_click_cb!=null){
                this.m_click_cb(this)
            }
       
        })
    }

    public updateLockStatus(){
        if(this.isUnLock){
            this.game_unlock.active = false;
        }else{
            this.game_unlock.active = true;
        }
    }
    public initView(call,data:LevelItemData){
        this.m_data = data;
        this.m_click_cb = call;
        this.isUnLock = gameManager.Singleton.levleIsUnLock(this.m_data.id)
        tools.loadUrl(this.m_data.cover,this.icon.getComponent(Sprite))
        this.lab_name.getComponent(Label).string = this.m_data.name;
        this.on_Touch()
        switch (this.m_data.category_id) {
            case 0:
                this.img_category_id.active = false;
            break;
            case 1:
                this.img_category_id.getComponent(Sprite).spriteFrame = this.kong_bu;
            break;
            case 2:
                this.img_category_id.getComponent(Sprite).spriteFrame = this.ju_qing;
            break;
            case 3:
                this.img_category_id.getComponent(Sprite).spriteFrame = this.huai_jiu;
            break;
            case 4:
                this.img_category_id.getComponent(Sprite).spriteFrame = this.nao_dong;
            break;
            case 5:
                this.img_category_id.getComponent(Sprite).spriteFrame = this.tui_li;
            break;
        }

        if(this.m_data.label_id===0){
            this.img_game_re.active = false;
            this.img_game_list_new.active = false;
        }else if(this.m_data.label_id==1){
            this.img_game_re.active = false;
            this.img_game_list_new.active = true;
        }else if(this.m_data.label_id==2){
            this.img_game_re.active = true;
            this.img_game_list_new.active = false;
        }
        this.updateLockStatus()
    }

    public getData(){
        return this.m_data;
    }
}


