package com.platform.modules.video.util;

import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.platform.common.utils.FileUtils;
import com.platform.modules.video.constants.FileSizeUnitEnum;
import com.platform.modules.video.constants.VideoTypeEnum;

/**
 * 获取视频播放的时长
 *
 * @author yikaihong
 */
public class VideoUtils {

    private static final String SEMICOLON = ":";

    private static final String NUMBER_PATTERN = "00";

    /**
     * 获取视频的播放时间
     *
     * @param videoPath
     * @param pattern
     * @return
     */
    public static String getPlayTime(String videoPath, String pattern) {
        if (StringUtils.isEmpty(videoPath)) {
            return null;
        }
        if (StringUtils.isEmpty(pattern)) {
            pattern = NUMBER_PATTERN;
        }
        File file = new File(videoPath);
        if (!file.exists()) {
            return null;
        }
        // 文件后缀校验
        if (!VideoTypeEnum.isVideo(FileUtils.getSuffix(file))) {
            return null;
        }

        Encoder encoder = new Encoder();
        MultimediaInfo info = null;
        try {
            info = encoder.getInfo(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (info == null) {
            return null;
        }
        long longTime = info.getDuration() / 1000;
        int hour = (int) (longTime / 3600);
        int minute = (int) (longTime % 3600) / 60;
        int second = (int) (longTime - hour * 3600 - minute * 60);
        String result = DecimalFormatUtils.numberFormat(pattern, hour)
                + SEMICOLON + DecimalFormatUtils.numberFormat(pattern, minute)
                + SEMICOLON + DecimalFormatUtils.numberFormat(pattern, second);

        return result;
    }

    /**
     * 获取视频的播放时间
     *
     * @param file
     * @param pattern
     * @return
     */
    public static String getPlayTime(File file, String pattern) {
        if (file == null) {
            return null;
        }
        if (StringUtils.isEmpty(pattern)) {
            pattern = NUMBER_PATTERN;
        }
        if (!file.exists()) {
            return null;
        }
        // 文件后缀校验
        if (!VideoTypeEnum.isVideo(FileUtils.getSuffix(file))) {
            return null;
        }
        Encoder encoder = new Encoder();
        MultimediaInfo info = null;
        try {
            info = encoder.getInfo(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (info == null) {
            return null;
        }
        long longTime = info.getDuration() / 1000;
        int hour = (int) (longTime / 3600);
        int minute = (int) (longTime % 3600) / 60;
        int second = (int) (longTime - hour * 3600 - minute * 60);
        String result = DecimalFormatUtils.numberFormat(pattern, hour)
                + SEMICOLON + DecimalFormatUtils.numberFormat(pattern, minute)
                + SEMICOLON + DecimalFormatUtils.numberFormat(pattern, second);
        return result;
    }

    /**
     * 计算文件块儿大小
     *
     * @param videoPath
     * @return
     */
    public static String getFileSize(String videoPath, FileSizeUnitEnum unitEnum) {
        if (StringUtils.isEmpty(videoPath)) {
            return null;
        }
        File file = new File(videoPath);
        if (!file.exists()) {
            return null;
        }
        // 文件后缀校验
        if (!VideoTypeEnum.isVideo(FileUtils.getSuffix(file))) {
            return null;
        }
        long fileSize = file.length();
        return FileSizeCalculateUtils.calculateFileSizeString(fileSize,
                unitEnum);
    }

    /**
     * 计算文件块儿大小
     *
     * @param file
     * @param unitEnum
     * @return
     */
    public static String getFileSize(File file, FileSizeUnitEnum unitEnum) {
        if (!file.exists()) {
            return null;
        }
        // 文件后缀校验
        if (!VideoTypeEnum.isVideo(FileUtils.getSuffix(file))) {
            return null;
        }
        long fileSize = file.length();
        return FileSizeCalculateUtils.calculateFileSizeString(fileSize,
                unitEnum);
    }

    /**
     * 时间目录
     *
     * @return
     */
    public static String getDateDir() {
        DateFormat bf = new SimpleDateFormat("yyyyMMdd");
        Date currentDate = new Date();
        return bf.format(currentDate);
    }

}
