package com.apps.chillscreen.tool;

import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.core.app.ActivityCompat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.Buffer;
import java.nio.charset.StandardCharsets;

import io.objectbox.flatbuffers.Utf8;

public class OpenUtil {
    public static String getConvert(InputStream inputStream){
        String covertStr="";
        try {
            StringBuilder stringBuilder=new StringBuilder();
            char [] buffer=new char[8192];
            Reader reader=new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            int a=0;
            while ((a=reader.read(buffer))!=-1){
                stringBuilder.append(buffer,0,a);
            }
            covertStr=stringBuilder.toString();
        }  catch (IOException e) {
            return covertStr;
        }
        return covertStr;
    }
    public static void initFull(Activity activity) {
        Window window = activity.getWindow();
        View decorView = window.getDecorView();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    }

    public static int dpToPx(Context context, float dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (dp * density);
    }


    public static boolean requestPermission(Activity context, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            return true;
        }
        if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(
                    context,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    requestCode
            );
            return false;
        } else {
            return true;
        }
    }

    public static Uri saveToGallery(Context context, File file){
        String name=System.currentTimeMillis()+".jpg";
        ContentValues contentValues=new ContentValues();
        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME,name);
        contentValues.put(MediaStore.Images.Media.MIME_TYPE,"image/jpeg");
        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.Q){
            contentValues.put(MediaStore.Images.Media.IS_PENDING,1);
        }
        Uri uri;
        if(Build.VERSION.SDK_INT>Build.VERSION_CODES.Q){
            uri=MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
        }else{
            uri=MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        }
        ContentResolver contentResolver=context.getContentResolver();
        Uri imgUri=contentResolver.insert(uri,contentValues);
        if(imgUri==null){
            Log.d("MediaSaver", "Failed to create media entry");
            return null;
        }
        try(OutputStream outputStream=contentResolver.openOutputStream(imgUri);
            InputStream inputStream=new FileInputStream(file)
        ){
            if (outputStream == null) {
                Log.d("MediaSaver", "Failed to open output stream");
                return null;
            }
            byte[] buffer=new byte[4096];
            int bytesRead;
            while ((bytesRead=inputStream.read(buffer))!=-1){
                outputStream.write(buffer,0,bytesRead);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                contentValues.clear();
                contentValues.put(MediaStore.Images.Media.IS_PENDING, 0);
                contentResolver.update(imgUri, contentValues, null, null);
            }
            return imgUri;
        } catch (IOException e) {
            Log.e("MediaSaver", "Error saving image to gallery", e);

            // 发生错误时删除创建的文件记录
            try {
                contentResolver.delete(imgUri, null, null);
            } catch (Exception deleteEx) {
                Log.e("MediaSaver", "Failed to delete failed media entry", deleteEx);
            }

            return null;
        }
    }
}




















