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

@ccclass('rank_list_top')
export class rank_list_top extends Component {
    @property(Node) one_lab_name:Node = null;
    @property(Node) one_img_avatar:Node = null;
    @property(Node) one_lab_score:Node = null;
    @property(Node) one_img_car:Node = null;

    @property(Node) two_lab_name:Node = null;
    @property(Node) two_img_avatar:Node = null;
    @property(Node) two_lab_score:Node = null;
    @property(Node) two_img_car:Node = null;

    @property(Node) three_lab_name:Node = null;
    @property(Node) three_img_avatar:Node = null;
    @property(Node) three_lab_score:Node = null;
    @property(Node) three_img_car:Node = null;

    initView(data_list:rankData[]) {
        if(data_list.length>0) {
            let one_data = data_list[0]
            this.setLabName(this.one_lab_name, one_data)
            this.setImgAvatar(this.one_img_avatar, one_data)
            this.setLabScore(this.one_lab_score, one_data)
            this.setImgCar(this.one_img_car, one_data)
        } 
        if(data_list.length>1) {
            let two_data = data_list[1]
            this.setLabName(this.two_lab_name, two_data)
            this.setImgAvatar(this.two_img_avatar, two_data)
            this.setLabScore(this.two_lab_score, two_data)
            this.setImgCar(this.two_img_car, two_data)
        }
        if(data_list.length>2) {
            let three_data = data_list[2]
            this.setLabName(this.three_lab_name, three_data)
            this.setImgAvatar(this.three_img_avatar, three_data)
            this.setLabScore(this.three_lab_score, three_data)
            this.setImgCar(this.three_img_car, three_data)
        }
    }

    private setLabName(node:Node, data:rankData) {
        node.getComponent(Label).string = data.nickName
    }

    private setImgAvatar(node:Node, data:rankData) {
        tools.loadRemoteImg(data.avatarUrl, (r)=>{
            node.getComponent(Sprite).spriteFrame = r.sf
        })
    }

    private setLabScore(node:Node, data:rankData) {
        node.getComponent(Label).string = data.score + ''
    }

    private setImgCar(node:Node, data:rankData) {
        tools.loadRemoteImg(data.car_url, (r)=>{
            node.getComponent(Sprite).spriteFrame = r.sf
        })
    }
}

