package com.android.common.utils;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import android.util.Log;

import com.orhanobut.logger.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.android.common.utils.IOUtils.closeIo;

/**
 * @author 910040
 * @version 1.0
 * @date 2018/11/8
 */
public class FileUtils {

    public static String readFile2String(String filePath) {
        String result = "";
        if (filePath != null) {
            File file = new File(filePath);
            if (file.exists()) {
                FileReader fileReader = null;
                BufferedReader bufferedReader = null;
                try {
                    fileReader = new FileReader(file);
                    bufferedReader = new BufferedReader(fileReader);
                    String line;
                    int lineNum = 0;
                    while ((line = bufferedReader.readLine()) != null) {
                        result += line + "\n";
                        lineNum++;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    closeIo(fileReader);
                    closeIo(bufferedReader);
                }
            }
        }
        return result;
    }

    public static List<String> readFile2List(String filePath) {
        List<String> stringList = new ArrayList<>();
        if (!TextUtils.isEmpty(filePath)) {
            File file = new File(filePath);
            if (file.exists()) {
                stringList = readFile2List(file);
            }
        }
        return stringList;
    }

    public static List<String> readFile2List(File file) {
        List<String> stringList = new ArrayList<>();
        if (file.exists()) {
            FileReader fileReader = null;
            BufferedReader bufferedReader = null;
            try {
                fileReader = new FileReader(file);
                bufferedReader = new BufferedReader(fileReader);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringList.add(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                closeIo(fileReader);
                closeIo(bufferedReader);
            }
        }
        return stringList;
    }


    public static void writeFile(String filePath, String content, boolean isAppend) {
        if (!TextUtils.isEmpty(filePath)) {
            FileWriter fileWriter = null;
            try {
                File file = new File(filePath);
                if (!file.exists()) {
                    file.createNewFile();
                }
                fileWriter = new FileWriter(file, isAppend);
                fileWriter.write(content);
                fileWriter.flush();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                IOUtils.closeIo(fileWriter);
            }
        } else {
            Log.e("jglee", "filePath 不能为空");
        }

    }


    /**
     * 删除方法:删除某个文件夹下的文件，如果传入的directory是个文件，将不做处理
     *
     * @param path
     */
    public static long deleteDirFiles(String path) {
        long size = 0;
        File directory = new File(path);
        //return getTotalSizeOfFilesInDir(directory);
        if (directory.exists() && directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    size += getTotalSizeOfFilesInDir(files[i]);
                    files[i].delete();
                }
            }
        }
        return size;
    }


    // 递归方式 计算文件的大小
    private static long getTotalSizeOfFilesInDir(final File file) {
        if (file.isFile()) {
            return file.length();
        }
        final File[] children = file.listFiles();
        long total = 0;
        if (children != null) {
            for (final File child : children) {
                total += getTotalSizeOfFilesInDir(child);
            }
        }
        Logger.d(file.getAbsolutePath() + " total:" + total);
        return total;
    }


    /**
     * 获取应用files目录地址
     *
     * @return
     */
    public static String getFilesDir() {
        return AppUtils.getAppContext().getFilesDir().getPath() + File.separator;
    }



    public static long getSDAvailableSize(Context context) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSizeLong();
        long availableBlocks = stat.getAvailableBlocksLong();
        return (availableBlocks * blockSize) / 1024/1024;
    }
}
