import { _decorator, Component, instantiate, Label, Node, Prefab, ScrollView } from 'cc';
import { base_ui } from '../../fw/base_ui';
import { quarter_rank_kuang_item } from './quarter_rank_kuang_item';
import { http } from '../../http';
import { config } from '../../config';
import { quarter_rank_item_data, rankData } from '../../data';
import { GameManager } from '../../GameManager';
import { uiManager } from '../../manager/uiManager';
import { rank_list_item } from '../rank/rank_list_item';
import { rank_my_info } from '../rank/rank_my_info';
const { ccclass, property } = _decorator;

@ccclass('quarter_rank_view')
export class quarter_rank_view extends base_ui {
    @property(Node) btn_close:Node = null
    @property(Node) btn_refresh:Node = null
    @property(Node) lab_date_name:Node = null
    @property(Node) kuang_content:Node = null
    @property(Prefab) kuang_item:Prefab = null
    @property(Node) rank_scrollView:Node = null
    @property(Node) rank_content:Node = null
    @property(Prefab) rank_item:Prefab = null
    @property(Node) my_info_node:Node = null
    
    private quarter_rank_data:Map<number,quarter_rank_item_data> = new Map
    private rank_list_data:rankData[] = []
    private page:number = 1
    private is_loading:boolean = false
    private is_request:boolean = false
    start() {
        this.onButtonListen(this.btn_close,()=>{
            this.close()
        })
        this.onButtonListen(this.btn_refresh,()=>{
            this.requestListData()
        })

        GameManager.requestDelay(()=>{
            this.requestListData()
        })
        
        this.rank_scrollView.on(ScrollView.EventType.SCROLL_TO_BOTTOM, this.onScrollToBottom, this)
    }

    private requestListData() {
        uiManager.Instance().showLoading()
        http.post(config.API.user_quarter_rank, null, (err,d)=>{
            if(!err){
                let data = JSON.parse(d)
                if(data.code===config.status.SUCCESS){
                    // console.log('data=',data.content)
                    this.lab_date_name.getComponent(Label).string = data.content.date_name
                    this.kuang_content.removeAllChildren()
                    for (let index = 0; index < data.content.quarter_rank.length; index++) {
                        const element = data.content.quarter_rank[index];
                        this.quarter_rank_data.set(element.quarter_rank_id,element)
                        let item = instantiate(this.kuang_item)
                        item.parent = this.kuang_content
                        item.getComponent(quarter_rank_kuang_item).initView(element) 
                    }
                    this.requestRankList(false, false)
                    this.requestMyRankData()
                } else {
                    uiManager.Instance().hideLoading()
                }
            } else {
                uiManager.Instance().hideLoading()
            }
        },'GET')
    }

    private onScrollToBottom() {
        if(this.is_request==false) { 
            return
        }
        if(this.is_loading) { 
            return 
        }
        // console.log('滑动到底部开始加载数据=',this.page)
        this.requestRankList(true)
    }

    private requestRankList(is_load_more:boolean, show_loading:boolean = true) {
        if(is_load_more==false) {
            this.page = 1
        }
        this.is_loading = true
        this.is_request = true
        if(show_loading) {
            uiManager.Instance().showLoading()
        }
        let opt = {'page': this.page}
        http.post(config.API.user_quarter_ranking, opt, (err,d)=>{
            setTimeout(()=>{
                this.is_loading = false
                uiManager.Instance().hideLoading()
            },500)
            if(!err){
                let data = JSON.parse(d)
                // console.log('赛季排行=',data.content)
                if(data.code===config.status.SUCCESS){
                    let total_page_number = data.content.total_page_number
                        
                    if(this.page==1) {
                        this.rank_list_data = []
                        this.rank_content.removeAllChildren()
                    }
                    this.rank_list_data = this.rank_list_data.concat(data.content.list)
                    this.rank_content.removeAllChildren()
                    for (let index = 0; index < this.rank_list_data.length; index++) {
                        const element = this.rank_list_data[index]
                        let item = instantiate(this.rank_item)
                        item.parent = this.rank_content;
                        let quarter_rank_data = this.quarter_rank_data.get(element.quarter_rank_id)
                        item.getComponent(rank_list_item).initQuarterRank(element,index,quarter_rank_data)
                    }

                    if(this.page==1) {
                        this.rank_scrollView.getComponent(ScrollView).scrollToTop()
                    }

                    if(this.page>=total_page_number) {
                        this.is_request = false
                    } else {
                        this.page += 1
                    }
                }
            }
        })
    }

    private requestMyRankData() {
        GameManager.requestMineRank(0, (d_content)=>{
            // console.log('个人排行 stype=',stype,'数据=',d_content)
            let data = d_content
            let quarter_rank_data = this.quarter_rank_data.get(data.quarter_rank_id)
            this.my_info_node.getComponent(rank_my_info).initQuarterRank(data,quarter_rank_data)
        })
    }
}

