import { _decorator, Component, Label, Node, Sprite, SpriteFrame } from 'cc';
import { base_ui } from '../fw/base_ui';
import { rankData } from '../data';
import { tools } from '../tools';
import { dataManager } from '../manager/dataManager';
import { car_info_shuxing } from './car_info/car_info_shuxing';
import { userDataManager } from '../manager/userDataManager';
const { ccclass, property } = _decorator;

@ccclass('user_info_view')
export class user_info_view extends base_ui {
    @property(Node) btn_close:Node = null
    @property(Node) img_avatar:Node = null
    @property(Node) img_avatar_border:Node = null
    @property(Node) lab_region:Node = null
    @property(Node) lab_nickname:Node = null
    @property(Node) lab_max_score:Node = null
    @property(Node) lab_car_pai:Node = null
    @property(Node) img_car:Node = null
    @property(Node) number_bg:Node = null
    @property(Node) number_img:Node = null
    @property(SpriteFrame) sf_number_rank_1:SpriteFrame = null
    @property(SpriteFrame) sf_number_rank_2:SpriteFrame = null
    @property(SpriteFrame) sf_number_rank_3:SpriteFrame = null
    @property(Node) lab_score:Node = null
    @property(Node) shuxing_info:Node = null
    @property(Node) lab_car_name:Node = null
    start() {
        this.onButtonListen(this.btn_close, ()=>{
            this.close()
        })
    }

    initView(data:rankData) {
        if(data.number>0&&data.number<=3) {
            this.number_bg.active = true
            switch (data.number) {
                case 1:
                    this.number_img.getComponent(Sprite).spriteFrame = this.sf_number_rank_1
                    break;
                case 2:
                    this.number_img.getComponent(Sprite).spriteFrame = this.sf_number_rank_2
                    break;
                case 3:
                    this.number_img.getComponent(Sprite).spriteFrame = this.sf_number_rank_3
                    break;
                default:
                    break;
            }
        } else {
            this.number_bg.active = false
        }

        tools.loadRemoteImg(data.avatarUrl, (d)=>{
            this.img_avatar.getComponent(Sprite).spriteFrame = d.sf
        })
        if(userDataManager.user_data.id==data.user_id) {
            tools.loadRemoteImg(userDataManager.user_data.photo_frame_img, (r)=>{
                this.img_avatar_border.getComponent(Sprite).spriteFrame = r.sf
            })
        } else {
            if(data.photo_frame_img && data.photo_frame_img.length>0) {
                tools.loadRemoteImg(data.photo_frame_img, (r)=>{
                    this.img_avatar_border.getComponent(Sprite).spriteFrame = r.sf
                })
            }
        }
        
        this.lab_region.getComponent(Label).string = `${data.province_name} - ${data.city_name}`
        this.lab_nickname.getComponent(Label).string = data.nickName
        this.lab_max_score.getComponent(Label).string = `${100} 分`
        this.lab_car_pai.getComponent(Label).string = data.license_code
        this.img_car.getComponent(Sprite).spriteFrame = dataManager.getUserRankCarSf(data.car_id, data.user_id)
        this.lab_score.getComponent(Label).string = `${data.score}分`
        let car_data = dataManager.getUserCarInfo(data.car_id)
        this.lab_car_name.getComponent(Label).string = car_data.name

        this.shuxing_info.getComponent(car_info_shuxing).initView(car_data)

    }
}

