package com.bikeapp_demo;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.IOException;

public class MediaScannerModule extends ReactContextBaseJavaModule {
    private final ReactApplicationContext reactContext;
    private static final String TAG = "MediaScannerModule";

    public MediaScannerModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "MediaScanner";
    }

    @ReactMethod
    public void scanFile(String sourcePath, String mimeType, final Callback callback) {
        try {
            File sourceFile = new File(sourcePath);
            if (!sourceFile.exists()) {
                Log.e(TAG, "Source file does not exist: " + sourcePath);
                callback.invoke("Source file does not exist: " + sourcePath);
                return;
            }

            ContentResolver resolver = reactContext.getContentResolver();
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DISPLAY_NAME, sourceFile.getName());
            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
                values.put(MediaStore.Images.Media.IS_PENDING, 1);
            }

            Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            if (imageUri == null) {
                Log.e(TAG, "Failed to create media store entry");
                callback.invoke("Failed to create media store entry");
                return;
            }

            try (OutputStream os = resolver.openOutputStream(imageUri);
                 FileInputStream fis = new FileInputStream(sourceFile)) {
                if (os == null) {
                    Log.e(TAG, "Failed to open output stream");
                    callback.invoke("Failed to open output stream");
                    return;
                }

                byte[] buffer = new byte[8192];
                int bytesRead;
                while ((bytesRead = fis.read(buffer)) != -1) {
                    os.write(buffer, 0, bytesRead);
                }
                os.flush();

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    values.clear();
                    values.put(MediaStore.Images.Media.IS_PENDING, 0);
                    resolver.update(imageUri, values, null, null);
                }

                callback.invoke(null);
            } catch (IOException e) {
                Log.e(TAG, "IO Error: " + e.getMessage());
                resolver.delete(imageUri, null, null);
                callback.invoke("IO Error: " + e.getMessage());
            }
        } catch (Exception e) {
            Log.e(TAG, "Error: " + e.getMessage());
            callback.invoke("Error: " + e.getMessage());
        }
    }
}