package com.persagy.cameractl.utils;

import com.persagy.cameractl.utils.EnumTools.OperatingSystem;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OtherTools {
	static Logger logger = LoggerFactory.getLogger(OtherTools.class);
//	播放mp4文件的url根地址
	public static String playMp4RootUrl = "/vplayf/";
	public static String dahuaDllPath = "";
	public static String fileSperator = File.separator;

	/*
	 * 获取系统的操作系统类型
	 */
	public static OperatingSystem getSystemName() {
		String os = System.getProperty("os.name").toLowerCase();
		if (os.indexOf(OperatingSystem.windows.toString()) > -1)
			return OperatingSystem.windows;
		else if (os.indexOf(OperatingSystem.linux.toString()) > -1)
			return OperatingSystem.linux;
		return OperatingSystem.other;
	}

	/*
	 * 获取摄像头厂家dll的路径
	 */
	public static String getDllPath(String dllName) {
		String newPath = "";
		OperatingSystem systemName = OtherTools.getSystemName();
		String dllPath = "./config/resourcesFile/cameraDll/" + systemName.toString() + "/" + dllName;
		try {
			File file1 = new File(dllPath);
			newPath = file1.getCanonicalPath();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return newPath;
	}
	
	public static String getLunxDllPath() {
		String dllPath="";
		try {
			String xiangduiDllPath="./config/resourcesFile/cameraDll/windows/dssSdk_lunxun/DPSDK_Core";
			File file1 = new File(xiangduiDllPath);
			dllPath = file1.getCanonicalPath();
		}catch(Exception e) {
			dllPath="";
		}
		return dllPath;
	};

	/* 操作失败时，拼接错误信息 */
	public static String joinErrStr(String errCode, String prefix) {
		return (prefix != null && prefix != "" ? prefix + "，" : "") + "厂家返回错误码：" + errCode;
	}

	public static String getVideoFilePath() {
		String token = StringTools.getUUID();
		return OtherTools.getVideoFilePathByT(token);
	};

	public static String getVideoFilePathByT(String token) {
		try {
			String playFileName = OtherTools.getMp4FullName(token);
			String playFileDir = OtherTools.getVideoFileDir();
			return playFileDir + fileSperator + playFileName;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "";
		}
	};

	public static String getVideoFileDir() {
		String dllPath = "./config/resourcesFile/tempVideo";
		String newPath = "";
		try {
			File file1 = new File(dllPath);
			newPath = file1.getCanonicalPath();
		} catch (Exception e) {
			// TODO: handle exception
		}

		return newPath;
	}

	public static String getVideoFilePath(Camera _camera) {
		try {
			String startTimeStr = _camera.startDateStr.replaceAll("[-:\\s]", "");
			String endTimeStr = _camera.endDateStr.replaceAll("[-:\\s]", "");
			String ipStr = _camera.cameraIp.replace(".", "_");
			String playFileName = OtherTools.getMp4FullName(startTimeStr + "_" + endTimeStr + "_" + _camera.channel);
			String playFileDir = getVideoFileDir() + fileSperator + ipStr;
			return playFileDir + fileSperator + playFileName;
		} catch (Exception e) {
			logger.error(e.getMessage());
			return "";
		}
	};

	public static String getMp4FullName(String prefix) {
		return prefix + ".mp4";
	};

	public static String getMp4NamePrefix(String fullName) {
		return fullName.substring(0, fullName.lastIndexOf("."));
	};

	public static ResultClass executeErr(String reason) {
		return new ResultClass(false, reason);
	};

	public static ResultClass executeSuccess(Object resultData) {
		return new ResultClass(true, resultData);
	};

	/**
	 * 读取标准输入流、输出流，防止进程阻塞
	 */
	public static String consumeInputStream(InputStream istream) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(istream));
			String s;
			StringBuilder sb = new StringBuilder();
			while ((s = br.readLine()) != null) {
				sb.append(s);
			}
			return sb.toString();
		} catch (Exception e) {
			return e.getMessage();
		}
	}
}
