/*-
 * ~~~~~~licensing~~~~~~
 * platform-manager-core
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.manager.shell;

import java.util.Arrays;
import java.util.stream.Collectors;

import org.apache.karaf.shell.support.table.Col;

public class ShellTableColFixed extends Col {

	protected int maxSizeLocal = -1;
	
	public ShellTableColFixed(String header) {
		super(header);
	}

	@Override
	protected String cut(String content, int size) {
        if (content.length() <= size) {
            return content;
        } else {
        	
        	String[] contentSplitted = content.split("\n");
        	for (int i=0; i<contentSplitted.length; i++)
        		contentSplitted[i] = super.cut(contentSplitted[i], size);
        	
        	if (contentSplitted.length == 1)
        		return contentSplitted[0];
        	
            return Arrays.asList(contentSplitted).stream().collect(Collectors.joining("\n"));
        }
	}
	
	@Override
	public ShellTableColFixed maxSize(int maxSize) {
		this.maxSizeLocal = maxSize;
		return (ShellTableColFixed)super.maxSize(maxSize);
	}
	
    private int getClippedSize(int cellSize) {
        return this.maxSizeLocal == -1 ? cellSize : Math.min(cellSize, this.maxSizeLocal);
    }
    /*
    	@ENTAXY_FIX:
    		when calculating cell size we must take into account not LENGTH, but WIDTH of the data
    		'cause data may contain "\n" so the LENGTH of data won't be equal to it's WIDTH 
     */
	public String format(Object cellData) {
        if (cellData == null) {
            cellData = "";
        }
        String fullContent = String.format("%s", cellData);
        if (fullContent.length() == 0) {
            return "";
        }
        String finalContent = cut(fullContent, getClippedSize(fullContent.length()));
        
        int finalContentWidth = 0;
        for (String s: Arrays.asList(finalContent.split("\n")))
        	finalContentWidth = Math.max(finalContentWidth, s.length());
        
        updateSize(finalContentWidth);
        return finalContent;
    }
}

