// $Id: $
// Copyright (c) 2004 Kurt M. Brown, x2ii.info.
// This file is subject to the terms and conditions of the GNU General Public
// License. See the file COPYING for more details.

xi.Pager = function(onChoice)
{
    this.$itemsPerPage = 25;
    this.$pageOffset = 0;
    this.$totalCount = 25;
    this.$pageCount = 1;
    this.$onChoice = onChoice;
    this.$buttons = [];
}
xi.Pager.prototype.getArgs = function()
{
    return [this.$itemsPerPage, this.$pageOffset];
}
xi.Pager.prototype.preBuild = function(container)
{
    this.$vbox = container.appendChild(document.createElement('vbox'));
    this.$hbox = this.$vbox.appendChild(document.createElement('hbox'));
    this.$hbox.setAttribute('class', 'pager');
    this.$hbox.setAttribute('align', 'center');
    var perPage = this.addOne('textbox');
    perPage.setAttribute('size', 3);
    perPage.setAttribute('value', this.$itemsPerPage);
    perPage.pager = this;
    perPage.onChoice = this.$onChoice;
    perPage.onkeyup = function(event) {
        const val = parseInt(this.value, 10);
        if (!isNaN(val) && val > 1)
            this.pager.$itemsPerPage = val;
        if (event.keyCode == event.DOM_VK_RETURN)
            this.onChoice();
    }
    this.addButton('first', -2);
    this.addButton('prev',  -1);
    var pageLabel = this.addOne('label');
    pageLabel.setAttribute('value', 'Page');
    var toPage = this.addOne('textbox');
    toPage.setAttribute('value', this.$pageOffset + 1);
    toPage.setAttribute('size', 3);
    toPage.pager = this;
    toPage.onChoice = this.$onChoice;
    toPage.onkeyup = function(event) {
        const val = parseInt(this.value, 10);
        if (!isNaN(val) && val > 0)
            this.pager.$pageOffset = val - 1;
        if (event.keyCode == event.DOM_VK_RETURN)
            this.onChoice();
    }
    this.$toPage = toPage;
    var ofLabel = this.addOne('label');
    ofLabel.setAttribute('value', 'of');
    this.$lastPageLabel = this.addOne('label');
    this.$lastPageLabel.value = this.$pageCount;
    this.addButton('next', 1);
    this.addButton('last', 2);
    this.$details = this.addOne('hbox');
    this.$details.setAttribute('align', 'center');
}
xi.Pager.prototype.showProcessing = function()
{
    // disable buttons ?
    this.setDetail('processing', 'processing, please wait ...');
}
xi.Pager.prototype.showFinished = function(totalCount)
{
    // enable buttons ?
    this.$totalCount = totalCount;
    var pageCount = Math.round(totalCount/this.$itemsPerPage);
    if (totalCount > this.$itemsPerPage*pageCount)
        ++pageCount;
    this.$pageCount = pageCount;
    this.$lastPageLabel.value = this.$pageCount;
    if (this.$pageOffset  > pageCount - 1) {
        this.$pageOffset = pageCount - 1
        this.$toPage.value = pageCount;
    }
    const start = this.$pageOffset*this.$itemsPerPage + 1;
    var end = start + this.$itemsPerPage - 1;
    if (end > totalCount)
        end = totalCount;
    var image = this.setDetail(
        'reload',
        'Showing ' + start + ' - ' + end + ' of ' + totalCount
    );
    image.onclick = this.$onChoice;
}
xi.Pager.prototype.addButton = function(name, moveValue)
{
    var button = this.addOne('image');
    button.setAttribute('class', name);
    button.pager = this;
    button.moveValue = moveValue;
    button.onChoice = this.$onChoice;
    button.onclick = function() {
        var pageOffset = this.pager.$pageOffset;
        if (this.moveValue == -2)
            pageOffset = 0;
        else if (this.moveValue == 2)
            pageOffset = this.pager.$pageCount;
        else
            pageOffset += this.moveValue;

        if (pageOffset < 0)
            pageOffset = 0;
        else if (pageOffset > this.pager.$pageCount - 1)
            pageOffset = this.pager.$pageCount - 1;

        if (pageOffset != this.pager.$pageOffset) {
            this.pager.$pageOffset = pageOffset;
            this.pager.$toPage.value = pageOffset + 1;
            this.onChoice();
        }
    }
    this.$buttons.push(button);
}
xi.Pager.prototype.addOne = function(name)
{
    return this.$hbox.appendChild(document.createElement(name));
}
xi.Pager.prototype.setDetail = function(imageClass, labelValue)
{
    // disable buttons ?
    this.$details.removeAllChildren();
    var image = this.$details.appendChild(document.createElement('image'));
    image.setAttribute('class', imageClass);
    var label = this.$details.appendChild(document.createElement('label'));
    label.setAttribute('value', labelValue);
    return image;
}

