package com.ar.artraceartpro.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.widget.Toast;

import com.ar.artraceartpro.data.local.entity.ImageData;
import com.ar.artraceartpro.viewmodel.ImageViewModel;

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

public class ImportUtils {

    public static void saveImage(Uri uri, List<String> imagePaths, ImageViewModel imageViewModel, Context context, Activity activity) {
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            if (bitmap == null) {
                Toast.makeText(context, "Unable to load image", Toast.LENGTH_SHORT).show();
                return;
            }

            File internalStorageDir = context.getFilesDir();
            File imageFile = new File(internalStorageDir, System.currentTimeMillis() + ".jpg");
            FileOutputStream outputStream = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.close();
            inputStream.close();

            String imagePath = imageFile.getAbsolutePath();

            new Thread(() -> {
                if (ImportUtils.isImagePresent(imagePath, imagePaths, imageViewModel)) {
                    activity.runOnUiThread(() -> Toast.makeText(context, "The image already exists", Toast.LENGTH_SHORT).show());
                    imageFile.delete();
                    return;
                }
                imagePaths.add(imagePath);

                imageViewModel.insert(new ImageData(false, true, imagePath));

            }).start();

        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(context, "Failed to save picture: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    public static boolean isImageSizeValid(Uri uri, Context context) throws IOException {
        InputStream inputStream = context.getContentResolver().openInputStream(uri);
        if (inputStream == null) {
            return false;
        }

        long fileSize = inputStream.available();
        inputStream.close();
        long maxFileSize = 10 * 1024 * 1024;

        return fileSize <= maxFileSize;
    }

    private static boolean isImagePresent(String imagePath, List<String> imagePaths, ImageViewModel imageViewModel) {
        File newImageFile = new File(imagePath);

        for (String path : imagePaths) {
            File existingFile = new File(path);
            if (isFileSizeMatch(existingFile, newImageFile)) {
                return true;
            }
        }

        List<ImageData> imageEntries = imageViewModel.getAllImport();
        for (ImageData imageData : imageEntries) {
            File existingFile = new File(imageData.getImagePath());
            if (isFileSizeMatch(existingFile, newImageFile)) {
                return true;
            }
        }

        return false;
    }

    private static boolean isFileSizeMatch(File file1, File file2) {
        return file1.length() == file2.length();
    }

}
