package com.ericsson.uecontrol.core.util;

/**
 * Created by ezivkoc on 2015-01-16.
 */
public class StringUtil {

    private static final String[] BIT_DATA_SIZE = new String[]{"b", "kbit", "Mbit", "Gbit", "Tbit"};
    public static String getBitThroughputString(double bitsPerSec){
        return getSizeString(bitsPerSec, 1000, BIT_DATA_SIZE) +"/s";
    }

    private static final String[] BYTE_DATA_SIZE = new String[]{"B", "KB", "MB", "GB", "TB"};
    public static String getByteString(double value){
        return getSizeString(value, 1024, BYTE_DATA_SIZE);
    }

    private static String getSizeString(double value, int division, String[] sizeList){
        int index = 0;
        for(; value > division && index < sizeList.length-1 ;index++) {
            value /= division;
        }

        value = (int)(value*10) / 10.0;
        return value+" "+ sizeList[index];
    }


    public static String getTimeString(long time){
        int timeSec = (int)(time / 1000);
        int hours = timeSec / (60*60);
        int minutes = (timeSec-(hours*60*60)) / 60;
        int seconds = timeSec-(hours*60*60)-(minutes*60);

        String ret = "";
        if(hours > 0) ret += hours+"h ";
        if(minutes > 0) ret += minutes+"m ";
        if(seconds > 0) ret += seconds+"s ";
        return ret;
    }
}
