/**
* Created by rockyl on 2019-01-03.
*
* 砖块动画单体
*/

import ScillaComponent from 'components/base/ScillaComponent';
import { RectRenderer, Transform } from 'scilla-components/src';
import { Entity, resource, instantiate, dynamic } from 'scilla/src';
import RankItem from './RankItem';

export default class RankList extends ScillaComponent {
    item: resource;
    content: Entity;
    private _list: Entity[];
    private _bg: RectRenderer;

    rankInfo: dynamic;
    onAwake() {
        super.onAwake();
        // console.log('RankList onAwake')
        this.updateRankList(this.rankInfo);
    }

    updateRankList(rankInfo) {
        const [, , , ...data] = rankInfo.list;
        this._bg = this.content.getComponent(RectRenderer);
        if (this._list) {
            this._list.forEach(item => {
                this.content.removeChild(item);
            })
        }
        const itemHeight = 73;
        const totalHeight = itemHeight * data.length;
        this._list = [];
        this._bg.height = totalHeight;

        data.forEach((item, index) => {
            const rank = instantiate(this.item);
            rank.getComponent(RankItem).setdata(item);
            this.content.addChild(rank);
            // rank.getComponent(Transform).position.y = index * itemHeight;
            rank.getComponent(Transform).position.y = index * itemHeight - totalHeight / 2 + itemHeight / 2 + 10;
            this._list.push(rank);
        });
    }

}
