import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { car_item_data, car_type } from '../../data';
import { uiManager } from '../../manager/uiManager';
import { dataManager } from '../../manager/dataManager';
const { ccclass, property } = _decorator;

@ccclass('car_lib_list_item')
export class car_lib_list_item extends Component {
    @property(SpriteFrame) sf_bg_select:SpriteFrame = null
    @property(SpriteFrame) sf_bg_unselect:SpriteFrame = null
    @property(Node) img_car:Node = null
    @property(Node) lab_car_name:Node = null
    @property(Node) lab_car_score:Node = null
    @property(Node) suo_node:Node = null
    @property(Node) img_kuang:Node = null
    private m_data:car_item_data = null
    private m_index:number = 0
    private m_cb = null
    private is_jiesuo:boolean = false
    start() {
        uiManager.Instance().onButtonListen(this.node, ()=>{
            if(this.m_cb!=null) {
                this.m_cb(this)
            }
        })
    }

    initView(data:car_item_data, index, cb) {
        this.m_data = data
        this.m_index = index
        this.m_cb = cb
        this.img_car.getComponent(Sprite).spriteFrame = dataManager.getUserRankCarSf(data.id, -1)
        this.lab_car_name.getComponent(Label).string = data.name
        if(data.stype==car_type.score) {
            this.lab_car_score.getComponent(Label).string = data.unlock_points + '分'
            this.img_kuang.active = false
        } else {
            let string = `${data.temp_bag_list_item_data.quantity}/${data.unlock_points}碎片`
            this.lab_car_score.getComponent(Label).string = string
            this.img_kuang.active = true
        }
    }

    public getData():car_item_data{
        return this.m_data
    }

    public getIsJiesuo():boolean {
        return this.is_jiesuo
    }

    public getIndex():number {
        return this.m_index
    }

    public setJiesuoSelectedStatus() {
        this.is_jiesuo = true
        this.suo_node.active = false
    }

    public setJiesuoUnselectedStatus() {
        this.is_jiesuo = false
        this.suo_node.active = true
    }

    public setSelectedStatus() {
        this.node.getComponent(Sprite).spriteFrame = this.sf_bg_select
    }

    public setUnselectedStatus() {
        this.node.getComponent(Sprite).spriteFrame = this.sf_bg_unselect
    }
}

