// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { getLv } from "../util_PK_Game";
import Player from "./player_PK_Game";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RankLayer extends cc.Component {

    @property(cc.Button)
    btn_close: cc.Button = null;

    @property(cc.Node)
    rank1Root: cc.Node = null;

    @property(cc.Node)
    rank2Root: cc.Node = null;

    @property(cc.Node)
    rank3Root: cc.Node = null;

    @property(cc.Node)
    itemTemp: cc.Node = null;

    @property(cc.ScrollView)
    sviewt: cc.ScrollView = null;

    @property(cc.SpriteFrame)
    iconList = [];

    private _data: any;
    private _staticeData: any;
    mainCom: any;

    onLoad() {
        this.itemTemp.active = false;
        this.btn_close.node.on("click", this.onClickClose, this)
    }
    onClickClose() {

        cc.tween(this.node).to(0.3, { y: - cc.winSize.height }, { easing: "sineOut" }).start();

        this.mainCom.hideRank();
    }
    init(data) {
        this._staticeData = data;

    }
    setMyInfo(data) {

        let tempData = [...this._staticeData, { score: data.score, name: data.name, isMe: true }]
        this._data = tempData.sort((a, b) => {
            return b.score - a.score;
        })

        let rank = 0;
        for (let i = 0; i < this._data.length; ++i) {
            if (this._data[i].isMe) {
                rank = i;
                break;
            }
        }

        return rank;
    }
    show() {


        this.node.y = - cc.winSize.height;

        cc.tween(this.node).to(0.3, { y: 0 }, { easing: "sineIn" }).start();


        this.rank1Root.getChildByName("lb_score").getComponent(cc.Label).string = this._data[0].score;
        this.rank1Root.getChildByName("lb_name").getComponent(cc.Label).string = this._data[0].name;
        this.rank1Root.getChildByName("head").getComponent(Player).init();

        this.rank2Root.getChildByName("lb_score").getComponent(cc.Label).string = this._data[1].score;
        this.rank2Root.getChildByName("lb_name").getComponent(cc.Label).string = this._data[1].name;
        this.rank2Root.getChildByName("head").getComponent(Player).init();

        this.rank3Root.getChildByName("lb_score").getComponent(cc.Label).string = this._data[2].score;
        this.rank3Root.getChildByName("lb_name").getComponent(cc.Label).string = this._data[2].name;
        this.rank3Root.getChildByName("head").getComponent(Player).init();

        this.sviewt.content.removeAllChildren();
        for (let i = 3; i < this._data.length; ++i) {
            let cell = cc.instantiate(this.itemTemp);
            cell.active = true;
            cell.parent = this.sviewt.content;
            cell.getChildByName("head").getComponent(Player).init();
            cell.getChildByName("lb_score").getComponent(cc.Label).string = this._data[i].score;
            cell.getChildByName("lb_name").getComponent(cc.Label).string = this._data[i].name;
            cell.getChildByName("lb_rank").getComponent(cc.Label).string = `${i + 1}`;
            cell.getChildByName("icon").getComponent(cc.Sprite).spriteFrame = this.iconList[getLv(this._data[i].score)];
        }
    }


}
