import Panel from "../views/Panel"
import { NetManager } from "../../libs/tw/manager/NetManager";
import { DataManager } from "../../libs/tw/manager/DataManager";
import { NetName } from "../../libs/tw/enum/NetName";
import RankListItem from "./RankListItem";
import Loading from "../loading/Loading";
import DebugMgr from "../Mgr/DebugMgr";

export default class RankListPanel extends Panel {
    get skinKey() { return "RankListPanel" }
    get groupName() { return "rank_list_panel" }
    public labelHighestScore: eui.Label;
    public labelHighestRank: eui.Label;
    public groupEmpty: eui.Group;
    public groupNotEmpty: eui.Group;
    public list: eui.List;
    public scroller: eui.Scroller;

    private rankDataArray = [];
    private isLoadingMore = false;
    private scrollerEnd = 0;
    private pageIndex = 0;
    private pageOver = false;

    onSkinComplete() {
        super.onSkinComplete();

        this.scroller.skinName = scrollerSkinXML;

        NetManager.ins.getRecentRankListNew((success: any) => {  // 查询排行榜
            if (!success) return;
            let rankData = DataManager.ins.getData(NetName.RANK_NEW_LIST).data.currentRankList;

            //我的数据
            let myData = rankData["currentUserRankInfo"];
            this.labelHighestRank.text = myData["rank"];
            this.labelHighestScore.text = myData["maxScore"];

            this.rankDataArray = rankData["userRankList"];
            if (this.rankDataArray.length <= 0) { //判断排行榜数据是否为空
                this.groupEmpty.visible = true;
                this.groupNotEmpty.visible = false;
                return;
            }
            this.list.dataProvider = new eui.ArrayCollection(this.rankDataArray);
            this.list.itemRenderer = RankListItem;

            //监听滑动事件
            /* this.scroller.addEventListener(egret.Event.CHANGE, () => {
                this.scrollerEnd = this.scroller.viewport.scrollV
                if (!this.pageOver && !this.isLoadingMore && (this.scroller.viewport.scrollV + this.scroller.height) >= (this.scroller.viewport.contentHeight)) {
                    this.isLoadingMore = true;
                    this.loadMore();
                }
            }, this); */
        }, window["rankId"], DataManager.ins.customCfgData.actId, false, this.pageIndex, 150);
    }

    /* private loadMore() {
        Loading.instace.show();
        NetManager.ins.getRecentRankListNew((success: any) => {  // 查询排行榜
            if (!success) return;
            let rankData = DataManager.ins.getData(NetName.RANK_NEW_LIST).data.currentRankList;

            if (rankData["userRankList"].length <= 0) {
                this.pageOver = true;
            } else {
                for (let i of rankData["userRankList"]) {
                    this.rankDataArray.push(i);
                }
                this.list.dataProvider = new eui.ArrayCollection(this.rankDataArray);
                this.scroller.viewport.validateNow();
                this.scroller.viewport.scrollV = this.scrollerEnd;
                this.isLoadingMore = false;
            }
            Loading.instace.hide();
        }, window["rankId"], DataManager.ins.customCfgData.actId, false, ++this.pageIndex, 12);
    } */
}

const scrollerSkinXML = `<?xml version="1.0" encoding="utf-8"?>
<e:Skin xmlns:e="http://ns.egret.com/eui">
    <e:VScrollBar id="verticalScrollBar" width="7" height="100%" right="0">
        <e:Skin>
            <e:Image id="thumb" width="7" height="77" source="rank_list_sliderbar_png" scale9Grid="2,5,3,67" />
        </e:Skin>
    </e:VScrollBar>
</e:Skin>`;