import { _decorator, Component, Label, Node, Sprite, UITransform } from 'cc';
import { rankData } from '../../data';
import { tools } from '../../tools';
import { imageCacheManager } from '../../manager/imageCacheManager';
import { uiManager } from '../../manager/uiManager';
import { config } from '../../config';
import { base_ui } from '../../fw/base_ui';
import { user_info_view } from '../user_info_view';
import { userDataManager } from '../../manager/userDataManager';
import { GameManager } from '../../GameManager';
const { ccclass, property } = _decorator;

@ccclass('rank_list_item')
export class rank_list_item extends base_ui {
    @property(Node) lab_number:Node = null;
    @property(Node) img_avatar:Node = null;
    @property(Node) lab_nickname:Node = null;
    @property(Node) lab_score:Node = null;
    @property(Node) img_car:Node = null;
    private m_data:rankData = null

    protected start(): void {
        this.onButtonListen(this.node, ()=>{
            uiManager.Instance().showUi(config.UI.ui_user_info_view, null, (node:Node)=>{
                node.getComponent(user_info_view).initView(this.m_data)
            })
        })
    }

    initView(data:rankData, index) {
        this.m_data = data
        this.lab_number.getComponent(Label).string = index+1 + ''
        tools.loadRemoteImg(data.avatarUrl, (r)=>{
            this.img_avatar.getComponent(Sprite).spriteFrame = r.sf
        })
        this.lab_nickname.getComponent(Label).string = data.nickName
        this.lab_score.getComponent(Label).string = data.score + ' 分'
        if(data.user_id==userDataManager.user_data.id) {
            this.img_car.getComponent(Sprite).spriteFrame = GameManager.getUserDefaultRankCarSf()
        } else {
            this.img_car.getComponent(Sprite).spriteFrame = imageCacheManager.getRankCarImageById(data.car_id)
        }
    }
}

