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

@ccclass('home_top')
export class home_top extends Component {
    @property(Node) lab_region: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) guangbo:Node = null
    protected start(): void {
    }

    public init(){
        let user_data = userDataManager.user_data
        this.lab_region.getComponent(Label).string = user_data.province_name + ' - ' + user_data.city_name
        tools.loadRemoteImg(user_data.avatarUrl, (r)=>{
            this.img_avatar.getComponent(Sprite).spriteFrame = r.sf
        })
        if(user_data.photo_frame_img && 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_nickname.getComponent(Label).string = user_data.nickName
        this.lab_car_pai.getComponent(Label).string = user_data.license_code
        this.requestGuangboData()
    }

    public requestGuangboData() {
        GameManager.requestGuangbo((d_content)=>{
            this.reloadGuangboData(d_content)
        })
    }

    public reloadGuangboData(d_content) {
        this.guangbo.getComponent(home_guangbo).init(d_content)
    }

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


