package me.km.utils;

public class TableUtils 
{
    public static String getTable(int width, String color, String... args)
    {
        //int leftSpace = (9 * width - 6 - 10 * args.length) % args.length;
        width = 9 * width - 1;     
        
        StringBuilder sb = new StringBuilder(color + "│ ");
        //int bonus;
        for(String s : args)
        {
            //bonus = leftSpace > 0 ? 9 : 0;
            sb.append("§r");
            sb.append(getStringWithMaxLength(s, width));
            //leftSpace -= 9;
            sb.append(color);
            sb.append("│ ");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }
    
    public static String getTableMiddle(int width, int columns, String color)
    {
        StringBuilder sb = new StringBuilder(color + "├");
        for(int i = 0; i < columns; i++)
        {
            for(int j = 0; j < width; j++)
            {
                sb.append("─");
            }
            sb.append("┼");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("┤");
        return sb.toString();
    }
    
    public static String getTableStart(int width, int columns, String color)
    {
        StringBuilder sb = new StringBuilder(color + "┌");
        for(int i = 0; i < columns; i++)
        {
            for(int j = 0; j < width; j++)
            {
                sb.append("─");
            }
            sb.append("┬");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("┐");
        return sb.toString();
    }
    
    public static String getTableEnd(int width, int columns, String color)
    {
        StringBuilder sb = new StringBuilder(color + "└");
        for(int i = 0; i < columns; i++)
        {
            for(int j = 0; j < width; j++)
            {
                sb.append("─");
            }
            sb.append("┴");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("┘");
        return sb.toString();
    }
    
    private static String getStringWithMaxLength(String s, int max)
    {
        // 9 Pixel: └,─,┬
        // 8 Pixel:
        // 7 Pixel:
        // 6 Pixel: normal, │
        // 5 Pixel: f, k
        // 4 Pixel: I, t
        // 3 Pixel: l
        // 2 Pixel: is
        int sum = 0;
        String r = "";
        int should;
        boolean onColor = false;
        for(char c : s.toCharArray())
        {
            switch(c)
            {
                case 'f':
                case 'k':
                    should = 5;
                    break;
                case 'I':
                case 't':
                case ' ':
                    should = 4;
                    break;
                case 'l':
                    should = 3;
                    break;
                case 'i':
                    should = 2;
                    break;
                case '§':
                    should = 0;
                    onColor = true;
                    break;
                default:
                    should = 6;
            }
            if(!onColor || should == 0)
            {
                sum += should;
            }
            else
            {
                onColor = false;
            }
            //System.out.println(sum + "  " + max);
            if(sum > max)
            {
                sum -= should;
                r += "§0";
                while(sum < max)
                {
                    if(max - sum == 1)
                    {
                        r += "ˈ";
                        break;
                    }
                    if(max - sum >= 4)
                    {
                        r += " ";
                        sum += 4;
                        continue;
                    }
                    r += "˼";
                    sum += 2;
                }
                return r;
            }
            else if(sum == max)
            {
                r += c;
                return r;
            }
            r += c;
        }
        r += "§0";
        while(sum < max)
        {
            if(max - sum == 1)
            {
                r += "ˈ";
                break;
            }
            if(max - sum >= 4)
            {
                r += " ";
                sum += 4;
                continue;
            }
            r += "˼";
            sum += 2;
        }
        return r;
    }
}
