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

@ccclass('rank_list_item')
export class rank_list_item extends base_ui {
    @property(Node) img_number:Node = null
    @property(Node) lab_number:Node = 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;

    @property(SpriteFrame) sf_quater_bg:SpriteFrame = null
    @property(SpriteFrame) sf_quater_region_bg_rank:SpriteFrame = null
    @property(SpriteFrame) sf_quater_rank_1:SpriteFrame = null
    @property(SpriteFrame) sf_quater_rank_2:SpriteFrame = null
    @property(SpriteFrame) sf_quater_rank_3:SpriteFrame = null

    private m_data:rankData = null

    protected start(): void {
        this.onButtonListen(this.node, ()=>{
            uiManager.Instance().showUi(config.UI.ui_user_info_view, null, (node:Node)=>{
                node.getComponent(user_info_view).initView(this.m_data)
            })
        })
    }

    initView(data:rankData, index, type:number) {
        this.m_data = data
        this.lab_number.getComponent(Label).string = index+1 + ''
        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
            })
        }
        if(type==3) { this.region_bg.active = false } 
        else { this.region_bg.active = true }
        tools.substringRankRegionName(this.lab_region, data.city_name)
        tools.labelCutString(this.lab_nickname,data.nickName,8,false) //this.lab_nickname.getComponent(Label).string = data.nickName
        this.lab_score.getComponent(Label).string = data.score + ' 分'
        this.img_car.getComponent(Sprite).spriteFrame = dataManager.getUserRankCarSf(data.car_id, data.user_id)
    }

    initQuarterRank(data:rankData, index, quarter_rank_data:quarter_rank_item_data) {
        this.m_data = data
        this.node.getComponent(Sprite).spriteFrame = this.sf_quater_bg
        if(index<3) {
            this.lab_number.active = false
            switch (index) {
                case 0:
                    this.img_number.getComponent(Sprite).spriteFrame = this.sf_quater_rank_1
                    break;
                case 1:
                    this.img_number.getComponent(Sprite).spriteFrame = this.sf_quater_rank_2
                    break;
                case 2:
                    this.img_number.getComponent(Sprite).spriteFrame = this.sf_quater_rank_3
                    break;    
                default:
                    break;
            }
        } else {
            this.lab_number.active = true
        }
        this.lab_number.getComponent(Label).string = index+1 + ''
        tools.loadRemoteImg(data.avatarUrl, (r)=>{
            this.img_avatar.getComponent(Sprite).spriteFrame = r.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.region_bg.getComponent(Sprite).spriteFrame = this.sf_quater_region_bg_rank
        tools.substringRankRegionName(this.lab_region, data.city_name)
        tools.labelCutString(this.lab_nickname,data.nickName,8,false) //this.lab_nickname.getComponent(Label).string = data.nickName
        this.lab_score.getComponent(Label).string = data.score + ' 分'
        if(quarter_rank_data) {
            tools.loadRemoteImg(quarter_rank_data.icon, (r)=>{
                this.img_car.getComponent(Sprite).spriteFrame = r.sf
            })
        }
        
    }
}

