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_suo:Node = null
    @property(Node) img_keduihuan:Node = null
    @property(Node) img_id:Node = null
    @property(SpriteFrame) sf_img_score:SpriteFrame = null
    @property(SpriteFrame) sf_img_suipian:SpriteFrame = 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_id.getComponent(Sprite).spriteFrame = this.sf_img_score
        } else {
            let string = `${data.temp_bag_list_item_data.quantity}/${data.unlock_points}碎片`
            this.lab_car_score.getComponent(Label).string = string
            this.img_id.getComponent(Sprite).spriteFrame = this.sf_img_suipian
        }
        this.img_keduihuan.active = false

        this.setSuipianOperation()
    }

    public setSuipianOperation() {
        if(this.m_data.stype==car_type.suipian) {
            if(this.m_data.temp_bag_list_item_data.quantity>=this.m_data.unlock_points) {
                this.img_suo.active = false
                this.img_keduihuan.active = true
            } else {
                this.img_suo.active = true
                this.img_keduihuan.active = false
            }   
        }   
    }

    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
        this.lab_car_score.active = false
    }

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

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

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

