import { _decorator, Button, Component, Label, Node, Sprite } from 'cc';
import { userDataManager } from '../../manager/userDataManager';
import { tools } from '../../tools';
import { GameManager } from '../../GameManager';
import { uiManager } from '../../manager/uiManager';
import { imageCacheManager } from '../../manager/imageCacheManager';
const { ccclass, property } = _decorator;

@ccclass('car_lib_top')
export class car_lib_top extends Component {
    @property(Node) btn_edit:Node = null
    @property(Node) img_avatar:Node = null
    @property(Node) img_avatar_border:Node = null
    @property(Node) lab_nickname:Node = null
    @property(Node) lab_car_pai:Node = null
    @property(Node) lab_region:Node = null
    @property(Node) lab_max_score:Node = null
    @property(Node) img_car:Node = null

    protected start(): void {
        uiManager.Instance().onButtonListen(this.btn_edit, ()=>{
            GameManager.showEditUserInfoView()
        })
    }

    public init() {
        let user_data = userDataManager.user_data
        this.reloadUserInfo()
        if(user_data.photo_frame_img!=undefined && user_data.photo_frame_img.length>0) {
            tools.loadRemoteImg(user_data.photo_frame_img, (r)=>{
                this.img_avatar_border.getComponent(Sprite).spriteFrame = r.sf
            })
        }

        this.lab_car_pai.getComponent(Label).string = user_data.license_code
        this.lab_region.getComponent(Label).string = user_data.province_name + ' - ' + user_data.city_name
        this.lab_max_score.getComponent(Label).string = userDataManager.user_car_list.max_integral + '分'
        this.reloadUserCar()
    }

    public reloadUserInfo() {
        let user_data = userDataManager.user_data
        tools.loadRemoteImg(user_data.avatarUrl, (r)=>{
            this.img_avatar.getComponent(Sprite).spriteFrame = r.sf
        })
        tools.labelCutString(this.lab_nickname,user_data.nickName,12,false)

    }

    public reloadUserCar() {
        this.img_car.getComponent(Sprite).spriteFrame = imageCacheManager.getRankCarImageById(userDataManager.user_car_list.default_car_id)
    }
}

