package hf.lskj.com.carstockcount.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import hf.lskj.com.carstockcount.MyApplication;
import hf.lskj.com.carstockcount.activity.LoginActivity;
import hf.lskj.com.carstockcount.db.InitData;
import hf.lskj.com.carstockcount.db.User;
import hf.lskj.com.carstockcount.db.UserDaoUtils;

public class FileUtils {
    public static final String FILE_INIT = "用户数据";
    public static final String FILE_CARS="车辆数据";
    public static final String FILE_RECORD="盘点记录";
    public static final String SDPATH = Environment.getExternalStorageDirectory()
            + "/hplk/";

    public static String saveBitmap(Bitmap bm, String picName) {
        Log.i("TAG", "保存图片");
        try {
            if (!isFileExist("")) {
                File tempf = createSDDir("");
            }
            File f = new File(SDPATH, picName + ".jpeg");
            if (f.exists()) {
                f.delete();
            }
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            Log.i("TAG", "已经保存");
            return SDPATH + picName + ".jpeg";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static File createSDDir(String dirName) throws IOException {
        File dir = new File(SDPATH + dirName);
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {

            System.out.println("createSDDir:" + dir.getAbsolutePath());
            System.out.println("createSDDir:" + dir.mkdir());
        }
        return dir;
    }

    public static boolean isFileExist(String fileName) {
        File file = new File(SDPATH + fileName);
        file.isFile();
        return file.exists();
    }

    public static void delFile(String fileName) {
        File file = new File(SDPATH + fileName);
        if (file.isFile()) {
            file.delete();
        }
        file.exists();
    }

    public static void deleteDir(String path) {
        if (TextUtils.isEmpty(path)) {
            return;
        }
        File dir = new File(path);
        if (!dir.exists() || !dir.isDirectory())
            return;

        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete(); // 删除所有文件
            else if (file.isDirectory())
                deleteDir(path); // 递规的方式删除文件夹
        }
        dir.delete();// 删除目录本身
    }

    public static boolean fileIsExists(String path) {
        try {
            File f = new File(path);
            if (!f.exists()) {
                return false;
            }
        } catch (Exception e) {

            return false;
        }
        return true;
    }
    public static void refreshFile(String filePath) {
        File file = new File(filePath);
        MimeTypeMap mtm = MimeTypeMap.getSingleton();
        MediaScannerConnection.scanFile(MyApplication.getInstances(),
                new String[] { file.toString() },
                new String[] { mtm.getMimeTypeFromExtension(file.toString().substring(file.toString().lastIndexOf(".")+1)) },
                new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(final String path, final Uri uri) {
                        LogUtils.i("刷新完毕");
                    }
                });
    }
    /**
     * 出发扫描 mtp下的文件，在保存文件到 sd卡下后，不能显示，故这里触发一下扫描机制，让手机连上电脑后，就可以读出文件了
     *
     * @param fName，文件的完整路径名
     */
    public static void fileScan(String fName) {
        Uri data = Uri.parse("file:///" + fName);
        MyApplication.getInstances().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, data));
    }
}

