package com.platform.modules.plm.util;

import com.platform.common.exception.RRException;

import java.math.BigDecimal;


/**
 * 文件大小、时长计算
 *
 * @author weiyan
 */
public class FileCalculateUtils {

    //
    private static final String DEFAULT_SIZE_STRING = "0B";
    
    private static final String SEMICOLON = ":";

    private static final String NUMBER_PATTERN = "00";

    // 不涉及线程并发， StringBuilder效率高
    private static StringBuilder builder = new StringBuilder();

    /**
     * 文件大小单位的转化
     *
     * @param fileSize
     * @param type
     * @return
     */
    public static String calculateFileSizeString(Long fileSize,
                                                 FileSizeUnitEnum type) {

        if (fileSize == null) {
            return DEFAULT_SIZE_STRING;
        }
        if (type == null) {
            type = FileSizeUnitEnum.MB;
        }

        //builder.delete(0, builder.length());
        builder.setLength(0);
        switch (type) {
            case B:
                builder.append(fileSize).append(type.getUnit());
                break;
            case KB:
                Double sizeOfKB = new BigDecimal((double) fileSize / 1024)
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                builder.append(sizeOfKB).append(type.getUnit());
                break;
            case MB:
                Double sizeOfMB = new BigDecimal((double) fileSize
                        / Math.pow(1024, 2))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                builder.append(sizeOfMB).append(type.getUnit());
                break;
            case GB:
                Double sizeOfGB = new BigDecimal((double) fileSize
                        / Math.pow(1024, 3))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                builder.append(sizeOfGB).append(type.getUnit());
                break;
            case BT:
                Double sizeOfBT = new BigDecimal((double) fileSize
                        / Math.pow(1024, 4))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                builder.append(sizeOfBT).append(type.getUnit());
                break;
            case PB:
                Double sizeOfPB = new BigDecimal((double) fileSize
                        / Math.pow(1024, 5))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                builder.append(sizeOfPB).append(type.getUnit());
                break;
            default:
                throw new RRException("文件大小计算异常");
        }

        return builder.toString();
    }

    /**
     * 文件大小单位的转化
     *
     * @param fileSize
     * @param type
     * @return
     */
    public static Double calculateFileSizeDouble(Long fileSize,
                                                 FileSizeUnitEnum type) {

        if (fileSize == null) {
            return 0.00;
        }
        if (type == null) {
            type = FileSizeUnitEnum.MB;
        }
        Double calculateFileSize = 0.00;
        switch (type) {
            case B:
                calculateFileSize = fileSize.doubleValue();
                break;
            case KB:
                calculateFileSize = new BigDecimal((double) fileSize / 1024)
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                break;
            case MB:
                calculateFileSize = new BigDecimal((double) fileSize
                        / Math.pow(1024, 2))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                break;
            case GB:
                calculateFileSize = new BigDecimal((double) fileSize
                        / Math.pow(1024, 3))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                break;
            case BT:
                calculateFileSize = new BigDecimal((double) fileSize
                        / Math.pow(1024, 4))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                break;
            case PB:
                calculateFileSize = new BigDecimal((double) fileSize
                        / Math.pow(1024, 4))
                        .setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
                break;
            default:
                throw new RRException("文件大小计算异常");
        }

        return calculateFileSize;
    }
    
    public static String calculateFilePlayTimeString(Long longTime) {
    	
        int minute = (int) (longTime/1000 / 60);
        int second = (int) (longTime/1000 - minute * 60);
        String result = DecimalFormatUtils.numberFormat(NUMBER_PATTERN, minute)
                + SEMICOLON + DecimalFormatUtils.numberFormat(NUMBER_PATTERN, second);
        return result;
    	
    }
    public static String calculateFileDurationString(Long longTime) {
    	
        int minute = (int) (longTime / 60);
        int second = (int) (longTime - minute * 60);
        String result = DecimalFormatUtils.numberFormat(NUMBER_PATTERN, minute)
                + SEMICOLON + DecimalFormatUtils.numberFormat(NUMBER_PATTERN, second);
        return result;
    	
    }
}
