package com.sheep.jiuyan.samllsheep.utils;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;

import com.sheep.gamegroup.util.LogUtil;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * Created by realicing on 2018/6/1.
 */

public class FileUtil {


    public static void deleteFile(File file) {
        deleteFile(file, true);
    }

    public static void deleteFile(File file, boolean isDeleteDir) {
        if (file != null && file.exists()) { // 判断文件是否存在
            if (file.isFile()) { // 判断是否是文件
                file.delete(); // delete()方法 你应该知道 是删除的意思;
            } else if (file.isDirectory()) { // 否则如果它是一个目录
                File[] files = file.listFiles(); // 声明目录下所有的文件 files[];
                if (files != null) {
                    for (int i = 0; i < files.length; i++) { // 遍历目录下所有的文件
                        deleteFile(files[i]); // 把每个文件 用这个方法进行迭代
                    }
                }
                if (isDeleteDir)
                    file.delete();
            }
        }
    }

    public static void copyFileToDir(@NonNull File fileFrom, @NonNull File dirTo) throws IOException {
        LogUtil.println("copyFile", fileFrom.getAbsolutePath(), dirTo.getAbsolutePath());
        if (!dirTo.isDirectory()) {
            return;
        }
        if (!dirTo.exists())
            dirTo.mkdirs();
        File dest = new File(dirTo, fileFrom.getName());
        if (dest.exists()) {
            if (dest.delete()) {
                LogUtil.println("copyFile", "删除文件失败");
            }
        }
        if (!dest.createNewFile()) {
            LogUtil.println("copyFile", "创建文件失败");
        }
        FileChannel outputChannel = null;
        FileChannel inputChannel = null;
        try {
            inputChannel = new FileInputStream(fileFrom).getChannel();
            outputChannel = new FileOutputStream(dest).getChannel();
            inputChannel.transferTo(0, inputChannel.size(), outputChannel);
            inputChannel.close();
        } finally {
            if (inputChannel != null) inputChannel.close();
            if (outputChannel != null) outputChannel.close();
        }
    }

    public static void copyFile(@NonNull File fileFrom, @NonNull File fileTo) throws IOException {
        LogUtil.println("copyFile", fileFrom.getAbsolutePath(), fileTo.getAbsolutePath());
        if (!fileTo.getParentFile().exists())
            fileTo.getParentFile().mkdirs();
        if (fileTo.exists()) {
            if (fileTo.delete()) {
                LogUtil.println("copyFile", "删除文件失败");
            }
        }
        if (!fileTo.createNewFile()) {
            LogUtil.println("copyFile", "创建文件失败");
        }
        FileChannel outputChannel = null;
        FileChannel inputChannel = null;
        try {
            inputChannel = new FileInputStream(fileFrom).getChannel();
            outputChannel = new FileOutputStream(fileTo).getChannel();
            inputChannel.transferTo(0, inputChannel.size(), outputChannel);
            inputChannel.close();
        } finally {
            if (inputChannel != null) inputChannel.close();
            if (outputChannel != null) outputChannel.close();
        }
    }

    public static void convertImage(String fromFilePath, String toFilePath, Bitmap.CompressFormat format) throws IOException {
        LogUtil.println("ScreenShotListenManager", "convertImage", fromFilePath, toFilePath);
        Bitmap bitmap = BitmapFactory.decodeFile(fromFilePath);
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(toFilePath));
        if (bitmap != null && bitmap.compress(format, 100, bos)) {
            bos.flush();
        }
    }

    /**
     * 获取图片配置信息
     * @param file
     * @return
     */
    public static BitmapFactory.Options getImageOptions(File file) {
        return getImageOptions(file.getAbsolutePath());
    }

    /**
     * 获取图片配置信息
     * @param filePath
     * @return
     */
    public static BitmapFactory.Options getImageOptions(String filePath) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        /**
         * 最关键在此，把options.inJustDecodeBounds = true;
         * 这里再decodeFile()，返回的bitmap为空，但此时调用options.outHeight时，已经包含了图片的高了
         */
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options); // 此时返回的bitmap为null
        return options;
    }
    /**
     * 图片是否已损毁或不完整
     * @param file
     * @return
     */
    public static boolean isNotImage(File file) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        /**
         * 最关键在此，把options.inJustDecodeBounds = true;
         * 这里再decodeFile()，返回的bitmap为空，但此时调用options.outHeight时，已经包含了图片的高了
         */
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options); // 此时返回的bitmap为null
        return options.mCancel || options.outWidth == -1 || options.outHeight == -1;
    }
}
