package com.paint.trace.drawing.other;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.net.Uri;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.Range;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.camera.core.Camera;
import androidx.core.app.ActivityCompat;


import com.paint.trace.drawing.view.MyApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Utils {


    public static int getRange(Camera camera) {
        Range<Integer> exposureCompensationRange = camera.getCameraInfo().getExposureState().getExposureCompensationRange();
        Integer upper = exposureCompensationRange.getUpper();
        Integer lower = exposureCompensationRange.getLower();
        return upper;
    }

    public static void setStatusBarTextColor(Activity activity, boolean dark) {
        Window window = activity.getWindow();
        View decor = window.getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

    public static Point getScreen(Context context) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
        Point point = new Point();
        point.x = width;
        point.y = height;
        return point;
    }




    public static Bitmap loadImage(Context context, String path) {
        Bitmap bitmap = loadImageFromAssets(context, path);
        if (bitmap == null) {
            Bitmap bitmap1 = BitmapFactory.decodeFile(path);
            Log.d(MyApplication.TAG,"--------import");
            return bitmap1;
        } else {
            Log.d(MyApplication.TAG,"--------Assets");
            return bitmap;
        }
    }


    public static Bitmap loadImageFromAssets(Context context, String fileName) {
        Bitmap bitmap = null;
        AssetManager assetManager = context.getAssets();
        try (InputStream is = assetManager.open(fileName)) {
            bitmap = BitmapFactory.decodeStream(is);
        } catch (IOException e) {
            return bitmap;
        }

        return bitmap;

    }

    public static List<String> fileExistsInAssets(Context context, String dir) {

        List<String> pathList = new ArrayList<String>();
        try {
            AssetManager assetManager = context.getAssets();
            String[] files = assetManager.list(dir);

            if (files != null) {
                for (String name : files) {
                    pathList.add(dir + "/" + name);
                }
                return pathList;
            }
        } catch (IOException e) {

            return pathList;
        }
        return pathList;
    }


    public static List<Bitmap> getAnimalsName(Context context, String dir) {
        List<Bitmap> list = new ArrayList<>();
        for (int i = 0; i < 40; i++) {

            Bitmap bitmap = loadImageFromAssets(context, dir + "/" + (i + 1) + ".jpg");
            if (bitmap == null) {
                break;
            } else {
                list.add(bitmap);
            }

        }
        return list;
    }

    public static PackageInfo getInfo(Context context) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException exception) {

            return null;
        }
        return packageInfo;
    }


    public static float getDistance(MotionEvent event) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    }


    public static void initFont(TextView tv) {
        tv.setTypeface(MyApplication.defaultFont);
    }


    public static boolean saveToInternalStorage(String fileName, Uri fileUri, Context context) {
        try {
            // 获取输入流
            Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(fileUri));
            // 创建图片文件
            File file = new File(context.getFilesDir(), fileName);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            // 将Bitmap图片进行存储
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            fileOutputStream.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String getImportPath(String name) {
        File filesDir = MyApplication.app.getFilesDir();
        String s = filesDir + "/" + name;
        return s;
    }

    public static float dpToPx(float dpValue) {
        float density = MyApplication.app.getResources().getDisplayMetrics().density;
        return density * dpValue + 0.5f;
    }


    public static boolean checkPermission(Activity activity,String[] permissions) {
        boolean result = true;
        for (String per : permissions) {
            if (ActivityCompat.checkSelfPermission(activity, per) != PackageManager.PERMISSION_GRANTED) {
                result = false;
            }
        }
        return result;

    }

    public static double getUriFileSize(Context context, Uri uri) {
        double fileSize = 0;
        ContentResolver contentResolver = context.getContentResolver();

        // 如果Uri是文件类型
        if (uri.getScheme().equals("file")) {
            File file = new File(uri.getPath());
            fileSize = file.length();
        } else { // 如果Uri不是文件类型，则通过ContentResolver获取大小
            try {
                fileSize = contentResolver.openFileDescriptor(uri, "r").getStatSize();

            } catch (Exception e) {
                Log.e("UriUtils", "Error while retrieving file size: " + e.getMessage());
            }
        }
        fileSize = fileSize/1048576.0;
        return fileSize;
    }


}
