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

@ccclass('car_lib_top')
export class car_lib_top extends Component {
    @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) btn_region: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_region,()=>{
            if(userDataManager.user_data.update_region_status==1) {
                uiManager.Instance().showUi(config.UI.ui_tips_view)
                return
            }
            uiManager.Instance().showUi(config.UI.ui_select_area, null, (node:Node)=>{
                node.getComponent(select_area).initView(true, this.onSelectArea.bind(this))
            })
        })
    }

    init() {
        let user_data = userDataManager.user_data
        tools.loadRemoteImg(user_data.avatarUrl, (r)=>{
            this.img_avatar.getComponent(Sprite).spriteFrame = r.sf
        })
        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
            })
        }
        tools.labelCutString(this.lab_nickname,user_data.nickName,13,false)

        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()
    }

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

    private onSelectArea(obj:select_area, data:area_item_data) {
        GameManager.requestUserSetRegion(data.id, 2, (d)=>{
            uiManager.showToast('修改成功')
            //更改数据
            userDataManager.user_data.update_region_status = d.update_region_status
            //关闭
            obj.closeSelf()
        },true)
    }
    
}

