package com.kfzs.duanduan.utils.dlg;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import com.android.volleyplus.Response;
import com.android.volleyplus.VolleyError;
import com.kfzs.appstore.utils.restful.KFZSRestfulError;
import com.kfzs.appstore.utils.restful.KFZSRestfulErrorCallBack;
import com.kfzs.duanduan.view.DialogStorageLow;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.kfzs.duanduan.KFZSApp;

import java.io.File;
import java.util.List;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  HelperUtils</p>
 * @ <p>Description: 生成各种经常要使用的实例</p>
 * @ date:  2017/6/13 14:51
 * @ QQ:    315096953
 */

public class HelperUtils {

    public static int H = 1080;//屏幕高
    public static int W = 720;//屏幕宽
    public static float density = 2;//屏幕解析度
    public static int BAR_HEIGH = 50;//状态栏高度


    /**
     * 程序运行载入成功后执行此初始化
     */
    public static void init() {
        WindowManager wm = (WindowManager) KFZSApp.context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        H = outMetrics.heightPixels;
        W = outMetrics.widthPixels;
        density = outMetrics.density;
        int resourceId = KFZSApp.context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            BAR_HEIGH = KFZSApp.context.getResources().getDimensionPixelSize(resourceId);
        } else {
            BAR_HEIGH = (int) (25 * density);//不准确的方法
        }
    }

    public static int getDpiToPix(int dpis) {
        return (int) (density * dpis);
    }


    /**
     * 默认的网络回调错误监听
     *
     * @param showMsg
     * @return
     */
    public static Response.ErrorListener makeErr(final String showMsg) {
        return KFZSRestfulError.job(new KFZSRestfulErrorCallBack() {
            @Override
            public void callErrorJob(boolean flag, int code, VolleyError error) {
                //Log.e("HtppError-->", new String(error.networkResponse.data));
                if (TextUtils.isEmpty(showMsg)) {
                    return;
                }
                G.showToast(showMsg);
            }
        });
    }


    /**
     * 判断当前Activity处于前台还是后台
     */
    public static boolean isActivityOnTop(Activity activity) {
        ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (topActivity.getClassName().equals(activity.getClass().getName())) {
                return true;
            }
        }
        return false;
    }


    /**
     * 获取剩余空间
     *
     * @return
     */
    public static long getFreeSpace() {
        File file = Environment.getDataDirectory();
        StatFs dataFs = new StatFs(file.getPath());
        long sizes;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            sizes = dataFs.getFreeBlocksLong() * dataFs.getBlockSizeLong();
        } else {
            sizes = (long) dataFs.getFreeBlocks() * (long) dataFs.getBlockSize();
        }
        return sizes / ((1024 * 1024));
    }
    public static long getFreeSpaceB() {
        File file = Environment.getDataDirectory();
        StatFs dataFs = new StatFs(file.getPath());
        long sizes;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            sizes = dataFs.getFreeBlocksLong() * dataFs.getBlockSizeLong();
        } else {
            sizes = (long) dataFs.getFreeBlocks() * (long) dataFs.getBlockSize();
        }
        return sizes ;
    }


    public static int getPercent(long allcount, long calcount) {
        int percent = 0;
        if (allcount > 0 && calcount <= allcount) {
            percent = 100 - (int) (calcount / (float) allcount * 100.0f);
            if (percent < 0) {
                percent = 0;
            }
        }
        return percent;
    }

    /**
     * 判断sd卡是否可用
     * return true 可用  false 不可用
     */
    public static boolean isSDCardEnable(){
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }

    /**
     * 获取SD卡剩余空间
     *
     * @return SD卡剩余空间
     */
    public static long getSdkFreeSpace(){
        try{
            if(!isSDCardEnable()){
                return 0;
            }//获取SDCard根目录
            File path = Environment.getExternalStorageDirectory();
            StatFs statFs = new StatFs(path.getPath());
            long blockSize, availableBlocks;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                availableBlocks = statFs.getAvailableBlocksLong();
                blockSize = statFs.getBlockSizeLong();
            } else {
                availableBlocks = statFs.getAvailableBlocks();
                blockSize = statFs.getBlockSize();
            }

            return availableBlocks * blockSize;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }

    /**
     * 判断文件空间大小
     */
    public static void showInstallToast(Context context, File file){
        try{
            if (0 <= file.length() && (
                    file.length() > getFreeSpaceB() / 2
                    && file.length() > getSdkFreeSpace() / 2
                    )) {
                DialogStorageLow.showPopupWindow(context);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}





