package com.tjzhxx.union.public_store;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UtilSD {

    /**
     * 判断SD卡是否可用
     */
    public static boolean isSDcardOK() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取SD卡跟路径。SD卡不可用时，返回null
     */
    public static String getSDcardRoot() {
        if (isSDcardOK()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }

        return null;
    }

    public static void showToast(Context context, String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public static void showToast(Context context, int msgId) {
        Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show();
    }

    /**获取字符串中某个字符串出现的次数。*/
    public static int countMatches(String res, String findString) {
        if (res == null) {
            return 0;
        }

        if (findString == null || findString.length() == 0) {
            throw new IllegalArgumentException("The param findString cannot be null or 0 length.");
        }

        return (res.length() - res.replace(findString, "").length()) / findString.length();
    }

    /**判断该文件是否是一个图片。*/
    public static boolean isImage(String fileName) {
        return fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") || fileName.endsWith(".png");
    }

    /**
     * 获取年月日时分秒作为文件名
     * @return
     */
    public static String getStrDate() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String strDate = format.format(date);
        return strDate;
    }



    public static String forNewName(){
        String fileName = UtilSD.getStrDate()+".jpg";
        String newImgPath = AppDataFile.getPictureFile().getAbsoluteFile() + File.separator + fileName;
        return newImgPath;
    }

    public static String reGetNewFile(String filepath, String uid){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        // 获取这个图片的宽和高
        Bitmap bitmap = BitmapFactory.decodeFile(filepath, options); //此时返回bm为空
        options.inJustDecodeBounds = false;
        //计算缩放比
        int be = (int)(options.outHeight / (float)200);
        if (be <= 0)
            be = 1;
        options.inSampleSize = be;
        //重新读入图片，注意这次要把options.inJustDecodeBounds 设为 false哦
        bitmap= BitmapFactory.decodeFile(filepath,options);
//        int w = bitmap.getWidth();
//        int h = bitmap.getHeight();
//        System.out.println(w + "   " + h);

        String fileName = uid+UtilSD.getStrDate()+".jpg";
        String newImgPath = AppDataFile.getPictureFile().getAbsoluteFile() + File.separator + fileName;
        File newFile = new File(newImgPath);
        try {
            FileOutputStream out=new FileOutputStream(newFile);
            if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){
                out.flush();
                out.close();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return newImgPath;
    }

    public static boolean delete(File file) {
        if (file == null || !file.exists() || file.isDirectory()){
            return false;
        }
        file.delete();
        return true;
    }
}
