import { Item } from './Item';
import { ShapeRect, MySprite } from './Unit';

export class PageBar extends Item {
    bg: ShapeRect;

    lastBtn: MySprite;

    nextBtn: MySprite;

    data;

    _pageSize = 4;

    _totalSize = 0;

    curPage = 0;

    totalPage = 0;

    showPageBtn = false;

    set pageSize(pageSize: number) {
        if (pageSize <= 0) {
            pageSize = 1;
        }
        this._pageSize = pageSize;

        this.show();
    }

    get pageSize() {
        return this._pageSize;
    }

    set totalSize(totalSize){
        this._totalSize = totalSize;

        this.show();
    }

    get totalSize(){
        return this._totalSize;
    }

    set x(x){
        this._x = x;

        if(this.bg){
            this.bg.x = this._x;
        }
    }


    set y(y){
        this._y = y;

        if(this.bg){
            this.bg.y = this._y;
        }
    }


    init(data, pageSize = 1) {
        this.data = data;

        this._pageSize = pageSize;

        this.width = 91 * this.scaleX;
        this.height = 40 * this.scaleY;
        const bg = new ShapeRect();
        bg.setSize(this.width, this.height);
        bg.alpha = 0;
        bg.x = this._x;
        bg.y = this._y;
        this.bg = bg;

        const lastBtn = this.getPic('last', 0, 0);
        lastBtn.x = 0;
        lastBtn.y = 0;
        lastBtn.alpha = 0;
        this.lastBtn = lastBtn;
        bg.addChild(lastBtn);

        const nextBtn = this.getPic('next', 1, 0);
        nextBtn.x = this.width;
        nextBtn.y = lastBtn.y;
        nextBtn.alpha = 0;
        this.nextBtn = nextBtn;
        bg.addChild(nextBtn);
    }

    show() {

        if (this.pageSize <= 0) {
            this.pageSize = 1;
        }

        if (this.data) {
            this._totalSize = this.data.length;
        }
        else {
            this._totalSize = 0;
        }

        this.totalPage = Math.ceil(this.data.length / this.pageSize);

    }

    protected showBtn() {
       
        if(this.showPageBtn && this.totalPage > 1){
            this.lastBtn.alpha = 1;
            this.nextBtn.alpha = 1;
        }
        else{
            if (this.curPage > 1) {
                this.lastBtn.alpha = 1;
            }
            else {
                this.lastBtn.alpha = 0;
            }
    
            if (this.curPage < this.totalPage) {
                this.nextBtn.alpha = 1;
            }
            else {
                this.nextBtn.alpha = 0;
            }
        }    
    }

    next(callback) {

        this.curPage = this.curPage + 1;

        if(this.curPage > this.totalPage){
            this.curPage = this.totalPage;
        }

        let tempData = this.getData(this.curPage);

        this.showBtn();

        if (callback) {
            callback(tempData);
        }
    }

    last(callback) {

        this.curPage = this.curPage - 1;

        if(this.curPage <= 0){
            if(this.totalPage === 0){
                this.curPage = 0;
            }
            else{
                this.curPage = 1;
            }
        }

        let tempData = this.getData(this.curPage);

        this.showBtn();

        if (callback) {
            callback(tempData);
        }
    }

    protected getData(pageNum : number){
        
        if(this.totalPage === 0){
            return [];
        }

        let _pageNum = pageNum;

        if(_pageNum > this.totalPage){
            _pageNum = this.totalPage;
        }

        let start = (_pageNum - 1) * this.pageSize;

        let end = _pageNum * this.pageSize;

        end = end > this.totalSize ? this.totalSize : end;

        let tempData = [];

        for(let i = start; i < end; ++ i){
            tempData.push(this.data[i]);
        }

        return tempData;
    }
}

export class CyclePageBar extends PageBar{
    next(callback) {

        this.curPage = this.curPage + 1;

        if(this.curPage > this.totalPage){
            if(this.totalPage === 0){
                this.curPage = 0;
            }
            else{
                this.curPage = 1;
            }
            
        }

        let tempData = this.getData(this.curPage);

        this.showBtn();

        if (callback) {
            callback(tempData);
        }
    }

    last(callback) {

        this.curPage = this.curPage - 1;

        if(this.curPage <= 0){
            this.curPage = this.totalPage;
        }

        let tempData = this.getData(this.curPage);

        this.showBtn();

        if (callback) {
            callback(tempData);
        }
    }
}