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

@ccclass('rank_my_info')
export class rank_my_info extends Component {
    @property(Node) bg:Node = null
    @property(Node) img_weishangbang:Node = null
    @property(Node) icon_number_bg:Node = null
    @property(Node) icon_number_img:Node = null
    @property(Node) icon_number_other_bg:Node = null
    @property(Node) lab_number_other:Node = null
    @property(SpriteFrame) sf_rank_icon_1:SpriteFrame = null
    @property(SpriteFrame) sf_rank_icon_2:SpriteFrame = null
    @property(SpriteFrame) sf_rank_icon_3:SpriteFrame = null

    @property(Node) img_avatar:Node = null
    @property(Node) img_avatar_border:Node = null
    @property(Node) lab_nickname:Node = null
    @property(Node) lab_score:Node = null
    @property(Node) img_car:Node = null
    @property(Node) region_bg:Node = null
    @property(Node) lab_region:Node = null

    initView(data:rankData) {
        this.setBasiData(data)
        this.img_car.getComponent(Sprite).spriteFrame = dataManager.getUserDefaultRankCarSf()
    }

    initQuarterRank(data:rankData, quarter_rank_data:quarter_rank_item_data) {
        this.setBasiData(data)
        if(quarter_rank_data) {
            tools.loadRemoteImg(quarter_rank_data.icon, (r)=>{
                this.img_car.getComponent(Sprite).spriteFrame = r.sf
            })
        }
    }

    private setBasiData(data:rankData){
        if(data.number==0) {
            this.img_weishangbang.active = true
            this.icon_number_bg.active = false
            this.icon_number_other_bg.active = false
        } else {
            this.img_weishangbang.active = false
            if(data.number<=3) {
                this.icon_number_bg.active = true
                switch (data.number) {
                    case 1:
                        this.icon_number_img.getComponent(Sprite).spriteFrame = this.sf_rank_icon_1
                        break;
                    case 2:
                        this.icon_number_img.getComponent(Sprite).spriteFrame = this.sf_rank_icon_2
                        break;
                    case 3:
                        this.icon_number_img.getComponent(Sprite).spriteFrame = this.sf_rank_icon_3
                        break;
                    default:
                        break;
                }
            } else {
                this.icon_number_other_bg.active = true
                this.lab_number_other.getComponent(Label).string = data.number + ''
            }
        }

        tools.loadRemoteImg(data.avatarUrl, (r)=>{
            this.img_avatar.getComponent(Sprite).spriteFrame = r.sf
        })
        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
            })
        }
        tools.substringRankRegionName(this.lab_region, data.city_name)
        tools.labelCutString(this.lab_nickname,data.nickName,7,false)
        this.lab_score.getComponent(Label).string = data.score + ' 分'
    }

}

