package com.oo.seex.oo_statistics_lib.utils;

import android.os.Environment;
import android.os.StatFs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

/**
 * Created by xucon on 2017/11/3.
 */

public class FileUtil {

	private static String CACHE_PATH = "/.daemon";
	private static String CACHE_USER_FILE = "config_user.txt";
	private static String CACHE_OS_FILE = "config_os.txt";
	private static String CACHE_SOFT_FILE = "config_soft.txt";

	/**
	 * 保存用户id
	 * @param userId
	 * @return
	 */
	public static  boolean saveUserIdToFile(String userId) {
		if (!Environment.getExternalStorageState().equals("mounted")) {
			return false;
		} else {
			try {
				File e = Environment.getExternalStorageDirectory();
				File dir = new File(e.getAbsolutePath() + CACHE_PATH);
				if (!dir.exists()) {
					boolean file = dir.mkdir();
					if (!file) {
						return false;
					}
				}

				File file1 = new File(dir, CACHE_USER_FILE);
				FileOutputStream fos = new FileOutputStream(file1);
				fos.write(userId.getBytes());
				fos.close();
				return true;
			} catch (Exception var10) {
				var10.printStackTrace();
				return false;
			}
		}
	}

	/**
	 * 获取用户id
	 * @return
	 */
	public static String getUserIdToFile() {
		if (!Environment.getExternalStorageState().equals("mounted")) {
			return "";
		}
		File file = new File(Environment.getExternalStorageDirectory(),
				CACHE_PATH+File.separator+CACHE_USER_FILE);
		if(file == null || !file.exists()){
			return "";
		}
		FileInputStream e = null;
		BufferedReader br = null;
		try {
			e = new FileInputStream(file);
			br = new BufferedReader(new InputStreamReader(e));
			return br.readLine();

		} catch (Exception var8) {
			var8.printStackTrace();
		}finally {
			try {
				if (br != null) {
					br.close();
				}
			}catch (Exception ex){
				ex.printStackTrace();
			}
		}
		return "";
	}

	/**
	 * 保存soft
	 * @param soft
	 * @return
	 */
	public static  boolean saveSoftToFile(String soft) {
		if (!Environment.getExternalStorageState().equals("mounted")) {
			return false;
		} else {
			try {
				File e = Environment.getExternalStorageDirectory();
				File dir = new File(e.getAbsolutePath() + CACHE_PATH);
				if (!dir.exists()) {
					boolean file = dir.mkdir();
					if (!file) {
						return false;
					}
				}

				File file1 = new File(dir, CACHE_SOFT_FILE);
				FileOutputStream fos = new FileOutputStream(file1);
				fos.write(soft.getBytes());
				fos.close();
				return true;
			} catch (Exception var10) {
				var10.printStackTrace();
				return false;
			}
		}
	}

	/**
	 * 获取soft
	 * @return
	 */
	public static String getSoftToFile() {
		if (!Environment.getExternalStorageState().equals("mounted")) {
			return "";
		}
		File file = new File(Environment.getExternalStorageDirectory(),
				CACHE_PATH+File.separator+CACHE_SOFT_FILE);
		if(file == null || !file.exists()){
			return "";
		}
		FileInputStream e = null;
		BufferedReader br = null;
		try {
			e = new FileInputStream(file);
			br = new BufferedReader(new InputStreamReader(e));
			return br.readLine();

		} catch (Exception var8) {
			var8.printStackTrace();
		}finally {
			try {
				if (br != null) {
					br.close();
				}
			}catch (Exception ex){
				ex.printStackTrace();
			}
		}
		return "";
	}

	/**
	 * 获取OS版本
	 * @return
	 */
	public static String getOSToFile() {
		if (!Environment.getExternalStorageState().equals("mounted")) {
			return "";
		}
		File file = new File(Environment.getExternalStorageDirectory(),
				CACHE_PATH+File.separator+CACHE_OS_FILE);
		if(file == null || !file.exists()){
			return "";
		}
		FileInputStream e = null;
		BufferedReader br = null;
		try {
			e = new FileInputStream(file);
			br = new BufferedReader(new InputStreamReader(e));
			return br.readLine();

		} catch (Exception var8) {
			var8.printStackTrace();
		}finally {
			try {
				if (br != null) {
					br.close();
				}
			}catch (Exception ex){
				ex.printStackTrace();
			}
		}
		return "";
	}

	/**
	 * 保存OS版本
	 * @param OSVersion
	 * @return
	 */
	public static  boolean saveOSToFile(String OSVersion) {
		if (!Environment.getExternalStorageState().equals("mounted")) {
			return false;
		} else {
			try {
				File e = Environment.getExternalStorageDirectory();
				File dir = new File(e.getAbsolutePath() + CACHE_PATH);
				if (!dir.exists()) {
					boolean file = dir.mkdir();
					if (!file) {
						return false;
					}
				}

				File file1 = new File(dir, CACHE_OS_FILE);
				FileOutputStream fos = new FileOutputStream(file1);
				fos.write(OSVersion.getBytes());
				fos.close();
				return true;
			} catch (Exception var10) {
				var10.printStackTrace();
				return false;
			}
		}
	}
	/**
	 * 检查磁盘空间是否大于10mb
	 *
	 * @return true 大于
	 */
	public static boolean isDiskAvailable() {
		long size = getDiskAvailableSize();
		return size > 10 * 1024 * 1024; // > 10bm
	}

	/**
	 * 获取磁盘可用空间
	 *
	 * @return byte 单位 kb
	 */
	public static long getDiskAvailableSize() {
		if (!existsSdcard()) return 0;
		File path = Environment.getExternalStorageDirectory(); // 取得sdcard文件路径
		StatFs stat = new StatFs(path.getAbsolutePath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;
		// (availableBlocks * blockSize)/1024 KIB 单位
		// (availableBlocks * blockSize)/1024 /1024 MIB单位
	}
	public static Boolean existsSdcard() {
		return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
	}
}
