package com.yanzuoguang.util.table;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 表头计算
 */
public class TableHeadHelper {

    /**
     * 列头函数
     *
     * @param columns 计算列头
     * @return 返回计算结果
     */
    public static TableHead getTableHead(String... columns) {
        TableHead head = new TableHead();
        head.setTotalColumn(columns.length);

        // 父子级别关系映射
        Map<String, TableHeadItem> mapHead = new LinkedHashMap<String, TableHeadItem>();
        // 当前节点的子节点元素
        Map<String, List<TableHeadItem>> mapSub = new LinkedHashMap<String, List<TableHeadItem>>();
        // 最顶层的元素
        Map<String, TableHeadItem> mapTop = new LinkedHashMap<String, TableHeadItem>();
        for (String column : columns) {
            TableHeadItem item = setHead(mapHead, mapSub, mapTop, column);
            // 设置表格的最大行数
            head.setTotalRow(Math.max(item.getLevel(), head.getTotalRow()));
        }

        //  计算合并行的数量,第几行
        for (Map.Entry<String, TableHeadItem> item : mapHead.entrySet()) {
            setCellRow(head, item.getValue());
        }

        //  计算列合并的数量,第几列
        for (Map.Entry<String, TableHeadItem> item : mapTop.entrySet()) {
            setCellColumn(head, mapSub, item.getValue());
        }

        return head;
    }

    private static TableHeadItem setHead(Map<String, TableHeadItem> mapHead, Map<String, List<TableHeadItem>> mapSub,
                                         Map<String, TableHeadItem> mapTop, String columnFrom) {
        String[] columns = columnFrom.split(":");
        StringBuilder path = new StringBuilder();
        TableHeadItem last = null;
        for (int i = 0; i < columns.length; i++) {

            // 列名称
            String column = columns[i];

            // 父路径与当前节点路径
            String parentPath = path.toString();
            if (i > 0) {
                path.append(":");
            }
            path.append(column);
            String nowPath = path.toString();

            last = new TableHeadItem();
            last.setName(column);
            last.setPath(nowPath);
            last.setLevel(i + 1);
            last.setDataColumn(columnFrom.equals(nowPath));

            if (i == 0 && !mapTop.containsKey(nowPath)) {
                mapTop.put(nowPath, last);
            }

            // 添加到最顶层元素中
            if (!mapSub.containsKey(parentPath)) {
                mapSub.put(parentPath, new ArrayList<TableHeadItem>());
            }

            // 将元素添加到当前元素中
            if (!mapHead.containsKey(nowPath)) {
                mapHead.put(nowPath, last);

                // 添加到父级元素的下级元素
                mapSub.get(parentPath).add(last);
            }

            last = mapHead.get(nowPath);
            last.setLevelMax(Math.max(last.getLevelMax(), columns.length));
        }
        return last;
    }

    private static void setCellRow(TableHead head, TableHeadItem value) {
        //  合并行数量
        int rowCell = head.getTotalRow() - value.getLevelMax() + 1;
        value.setRow(value.getLevel());
        value.setRowCell(1);

        // 计算第几行,合并几行
        if (value.getLevel() == 1) {
            // 第一行的规则
            if (value.getLevelMax() == 1) {
                value.setRowCell(rowCell);
            }
        } else if (value.getLevel() == value.getLevelMax()) {
            // 最后一行的规则
            if (value.getLevelMax() == 2) {
                value.setRowCell(rowCell);
            } else {
                value.setRow(head.getTotalRow());
            }
        } else {
            // 其他行
            if (value.getLevel() == value.getLevelMax() - 1) {
                value.setRowCell(rowCell);
            }
        }
    }

    private static void setCellColumn(TableHead head, Map<String, List<TableHeadItem>> mapSub, TableHeadItem value) {
        // 设置当前列号
        value.setColumn(head.getDataColumn() + 1);
        // 设置默认合并单元格数量
        value.setColumnCell(1);
        // 将列添加到当前列集合中
        head.getColumns().add(value);
        // 计算合并几列
        if (value.isDataColumn()) {
            head.setDataColumn(head.getDataColumn() + 1);
        }
        // 假如有子节点,则合并的单元格等于子节点的想加
        List<TableHeadItem> subColumns = mapSub.containsKey(value.getPath()) ?
                mapSub.get(value.getPath()) : new ArrayList<TableHeadItem>();
        if (subColumns != null && subColumns.size() > 0) {
            value.setColumnCell(0);

            for (TableHeadItem subColumn : subColumns) {
                setCellColumn(head, mapSub, subColumn);
                value.setColumnCell(subColumn.getColumnCell());
            }
        }
    }
}