package com.infinite.focus.server.utils;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.LinkedList;

public class DigitUtils {
	
	public static void addTotalInMap(LinkedHashMap<String, LinkedList<Double>> map, Double totalCount) {
		for (String key : map.keySet()) {
			Double count = map.get(key).get(0);
			map.get(key).set(1, DigitUtils.getPercentage(count, totalCount));
		}

		/*LinkedList<Double> list = new LinkedList<Double>();
		list.add((double) totalCount);
		list.add(DigitUtils.getPercentage(totalCount, totalCount));

		map.put("Total", list);*/
	}
	
	public static void findTotalKeyAndAddPercentage(LinkedHashMap<String, LinkedHashMap<String, LinkedList<Double>>> map, Double totalCount) {
		
		for (String key : map.keySet()) {

			Double count = map.get(key).get("Total").get(0);

			if(count != 0) {

				map.get(key).get("Total").set(1,  getPercentage(count,totalCount));
			}
		}
	}

	public static Double getPercentage(Long count, Double totalCount) {
		return getPercentage((double) count, totalCount);
	}
	
	public static Double getPercentage(Double count, Long totalCount) {
		return getPercentage(count, (double) totalCount);
	}
	
	public static Double getPercentage(Long count, Long totalCount) {
		return getPercentage((double) count, (double) totalCount);
	}
	
	public static Double getPercentage(Double count, Double totalCount) {
		if (count != 0 && totalCount != 0) {
			Double percentage = ( count /  totalCount) * 100;
			return formatDouble(percentage);
		}

		return formatDouble(0.0);
	}

	public static Double formatDouble(Double double_) {
		try {
			return (Double) new BigDecimal(double_).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		} catch(Exception e) {
			e.printStackTrace();
		}
			
		return 0.0;
	}
	
	public static Double formatDoubleInTwoDigit(Double double_) {
		try {
			return (Double) new BigDecimal(double_).setScale(2, BigDecimal.ROUND_CEILING).doubleValue();
		} catch(Exception e) {
			e.printStackTrace();
		}
			
		return 0.00;
	}
	
	public static Double getAverage(Double count, Double totalCount) {
		if (count != 0 && totalCount != 0) {
			Double percentage =  count /  totalCount;
			return formatDouble(percentage);
		}

		return formatDouble(0.0);
	}
	
	//-------------------------------------------------------------------------------------------------------
	
	public static void addTotalInMapBigDecimal(LinkedHashMap<String, LinkedList<Object>> map, Double totalCount) {
		for (String key : map.keySet()) {
			long count = (long) map.get(key).get(0);
			map.get(key).set(1, DigitUtils.formatBigDecimalInTowDigit(DigitUtils.getPercentageBigDecimal(count, totalCount)));
		}

		/*LinkedList<Double> list = new LinkedList<Double>();
		list.add((double) totalCount);
		list.add(DigitUtils.getPercentage(totalCount, totalCount));

		map.put("Total", list);*/
	}
	
	public static BigDecimal getPercentageBigDecimal(Long count, Double totalCount) {
		return getPercentageBigDecimal((double) count, totalCount);
	}
	
	public static BigDecimal getPercentageBigDecimal(Double count, Long totalCount) {
		return getPercentageBigDecimal(count, (double) totalCount);
	}
	
	public static BigDecimal getPercentageBigDecimal(Long count, Long totalCount) {
		return getPercentageBigDecimal((double) count, (double) totalCount);
	}
	
	public static BigDecimal getPercentageBigDecimal(Double count, Double totalCount) {
		if (count != 0 && totalCount != 0) {
			return formatBigDecimal(new BigDecimal(( count /  totalCount) * 100));
		}

		return formateZeroToBigDecimal();
	}
	
	public static BigDecimal formatBigDecimal(BigDecimal bigDecimal) {
		try {
			return bigDecimal.setScale(4, BigDecimal.ROUND_HALF_UP);
		} catch(Exception e) {
			e.printStackTrace();
		}
			
		return formateZeroToBigDecimal();
	}
	
	public static BigDecimal formatBigDecimalInTowDigit(BigDecimal bigDecimal) {
		try {
			return bigDecimal.setScale(2, BigDecimal.ROUND_CEILING);
		} catch(Exception e) {
			e.printStackTrace();
		}
			
		return formateZeroToBigDecimalInTwoDigit();
	}
	
	public static BigDecimal getAverageBigDecimal(Double count, Double totalCount) {
		if (count != 0 && totalCount != 0) {
			Double percentage =  count /  totalCount;
			return formatBigDecimal(new BigDecimal(percentage));
		}

		return formateZeroToBigDecimal();
	}
	
	public static BigDecimal formateZeroToBigDecimal() {
		return new BigDecimal(0.0000).setScale(4, BigDecimal.ROUND_HALF_UP);
	}
	
	public static BigDecimal formateZeroToBigDecimalInTwoDigit() {
		return new BigDecimal(0.00).setScale(2, BigDecimal.ROUND_CEILING);
	}
}
