/*******************************************************************************
 * Copyright 2017 Bstek
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License.  You may obtain a copy
 * of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 ******************************************************************************/
package com.bstek.ureport.build.paging;

import com.bstek.ureport.definition.Band;
import com.bstek.ureport.definition.Paper;
import com.bstek.ureport.model.Report;
import com.bstek.ureport.model.Row;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jacky.gao
 * @since 2017年1月17日
 */
public class FixRowsPagination extends BasePagination implements Pagination {
    @Override
    public List<Page> doPaging(Report report) {
        Paper paper = report.getPaper();
        List<Row> rows = report.getRows();
        List<Row> headerRows = report.getHeaderRepeatRows();
        List<Row> footerRows = report.getFooterRepeatRows();
        List<Row> titleRows = report.getTitleRows();
        List<Row> summaryRows = report.getSummaryRows();
        int fixRows = paper.getFixRows() - headerRows.size() - footerRows.size();
        if (fixRows < 0) {
            fixRows = 1;
        }
        List<Row> pageRepeatHeaders = new ArrayList<>(headerRows);
        List<Row> pageRepeatFooters = new ArrayList<>(footerRows);
        List<Page> pages = new ArrayList<>();
        List<Row> pageRows = new ArrayList<>();
        int pageIndex = 1;
        for (Row row : rows) {
            int height = row.getRealHeight();
            if (height == 0) {
                continue;
            }
            Band band = row.getBand();
            if (band != null) {
                String rowKey = row.getRowKey();
                int index = -1;
                if (band.equals(Band.headerrepeat)) {
                    for (int j = 0; j < pageRepeatHeaders.size(); j++) {
                        Row headerRow = pageRepeatHeaders.get(j);
                        if (headerRow.getRowKey().equals(rowKey)) {
                            index = j;
                            break;
                        }
                    }
                    pageRepeatHeaders.remove(index);
                    pageRepeatHeaders.add(index, row);
                } else if (band.equals(Band.footerrepeat)) {
                    for (int j = 0; j < pageRepeatFooters.size(); j++) {
                        Row footerRow = pageRepeatFooters.get(j);
                        if (footerRow.getRowKey().equals(rowKey)) {
                            index = j;
                            break;
                        }
                    }
                    pageRepeatFooters.remove(index);
                    pageRepeatFooters.add(index, row);
                }
                continue;
            }
            pageRows.add(row);
            row.setPageIndex(pageIndex);
            if (pageRows.size() >= fixRows) {
                Page newPage = buildPage(pageRows, pageRepeatHeaders, pageRepeatFooters, titleRows, pageIndex, report);
                pageIndex++;
                pages.add(newPage);
                pageRows = new ArrayList<>();
            }
        }
        if (pageRows.size() > 0) {
            Page newPage = buildPage(pageRows, pageRepeatHeaders, pageRepeatFooters, titleRows, pageIndex, report);
            pageIndex++;
            pages.add(newPage);
        }
        report.getContext().setTotalPages(pages.size());
        buildPageHeaderFooter(pages, report);
        buildSummaryRows(summaryRows, pages);
        return pages;
    }
}
