import { _decorator, Component, instantiate, Node, Prefab, ScrollView } from 'cc';
import { uiManager } from '../../manager/uiManager';
import { rank_list_item } from './rank_list_item';
import { rank_my_info } from './rank_my_info';
import { rank_list_top } from './rank_list_top';
import { userDataManager } from '../../manager/userDataManager';
import { GameManager } from '../../GameManager';
import { rank_classify } from './rank_classify';
import { config } from '../../config';
const { ccclass, property } = _decorator;

@ccclass('rank')
export class rank extends Component {
    @property(Node) btn_back:Node = null;
    @property(Node) classify_node:Node = null;
    @property(Node) list:Node = null;
    @property(Node) list_top:Node = null;
    @property(Node) list_content:Node = null;
    @property(Prefab) rank_list_item:Prefab = null;
    @property(Node) my_info_node:Node = null;
    @property(Node) btn_quarter_box:Node = null;

    private m_onReloadCountryDataCallback = null;

    onReloadCountryDataCallback(cb) {
        this.m_onReloadCountryDataCallback = cb
    }

    start() {
        uiManager.Instance().onButtonListen(this.btn_back, ()=>{
            this.close()
        })
        uiManager.Instance().onButtonListen(this.btn_quarter_box, ()=>{
            uiManager.Instance().showUi(config.UI.ui_quarter_rank_view)
        })

        let type = 1
        this.classify_node.getComponent(rank_classify).init(type,this.onClassifyItem.bind(this))
        this.loadView(type)
    }

    onClassifyItem(type:number) {
        this.loadView(type)
    }

    loadView(type:number) {
        let id = 0
        if(type==1) {
            id = 0
        } else if(type==2) {
            id = userDataManager.user_data.region_pid
        } else if(type==3) {
            id = userDataManager.user_data.region_id
        }
        GameManager.requestRankList(id,(d_content)=>{
            // console.log('排行列表 id=',id, '数据=',d_content)
            if(id==0) {
                if(this.m_onReloadCountryDataCallback!=null) {
                    this.m_onReloadCountryDataCallback(d_content)
                }
            }
            this.reloadListTop(d_content, type)
            this.reloadListContent(d_content, type)
            this.reloadMyInfo(type)
        }, true)
    }

    reloadListTop(data_list, type:number) {
        this.list_top.getComponent(rank_list_top).initView(data_list, type)
    }

    reloadListContent(data_list, type:number) {
        this.list_content.removeAllChildren()
        if(data_list.length < 3) {
            return
        }
        for (let index = 3; index < data_list.length; index++) {
            const element = data_list[index]
            let item = instantiate(this.rank_list_item)
            item.parent = this.list_content;
            item.getComponent(rank_list_item).initView(element,index,type)
        }
        this.list.getComponent(ScrollView).scrollToTop()
    }

    reloadMyInfo(type:number) {
        let stype = 0
        if(type==1) {
            stype = 0
        } else if(type==2) {
            stype = 1
        } else if(type==3) {
            stype = 2
        }
        GameManager.requestMineRank(stype, (d_content)=>{
            // console.log('个人排行 stype=',stype,'数据=',d_content)
            this.my_info_node.getComponent(rank_my_info).initView(d_content)
        })
    }

    close() {
        this.node.removeFromParent()
    }

}

