package com.house.wallpapers.mytool;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Debug;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.house.wallpapers.App;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;

public class Comutils {
    public static final String key_info = "key_info";

    public static final String key_index = "key_index";

    public static String getCovertStr(InputStream stream) {
        String covertStr = "";
        try {
            StringWriter writer = new StringWriter();
            char[] buffer = new char[stream.available()];
            Reader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
            int a = 0;
            while ((a = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, a);
            }
            covertStr = writer.toString();
        } catch (IOException e) {
            return covertStr;
        }
        return covertStr;
    }

    public static int dp2Px(int dp) {
        float scale = App.mAppContext.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static void initFull(Activity activity, boolean light) {
        Window window = activity.getWindow();
        View decorView = window.getDecorView();
        View rootView = decorView.getRootView();
        if (light) {

        } else {

            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);



    }


    private static Bitmap getBitmap(Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable) drawable).getBitmap();
        }

        // 创建一个空白的 Bitmap
        Bitmap bitmap = Bitmap.createBitmap(
                drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);

        // 在 Canvas 上绘制 Drawable
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }


    public static String saveDrawableToFile( Drawable drawable,String saveFilePath) {
        // 将 Drawable 转换为 Bitmap
        Bitmap bitmap = getBitmap(drawable);


        // 创建要保存的文件
        File file = new File(saveFilePath);
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream(file);

            // 将 Bitmap 写入文件 (使用 PNG 格式，可改为 JPEG)
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return file.getAbsolutePath();
    }

    public static void printUsedMemory(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        Debug.MemoryInfo[] memoryInfoArray = activityManager.getProcessMemoryInfo(new int[]{android.os.Process.myPid()});
        for (Debug.MemoryInfo memoryInfo : memoryInfoArray) {
            int totalPss = memoryInfo.getTotalPss();
            int totalPrivateDirty = memoryInfo.getTotalPrivateDirty();
            int totalSharedDirty = memoryInfo.getTotalSharedDirty();

            Log.d("MemoryInfo", "Total PSS: " + totalPss + " KB");
            Log.d("MemoryInfo", "Total Private Dirty: " + totalPrivateDirty + " KB");
            Log.d("MemoryInfo", "Total Shared Dirty: " + totalSharedDirty + " KB");
        }
    }


    public static int[] getScreenWidthHeight(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        return new int[]{width, height};
    }
}
