package app.timesafari.sharedimage;

import android.content.Context;
import android.content.SharedPreferences;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;

@CapacitorPlugin(name = "SharedImage")
public class SharedImagePlugin extends Plugin {
    
    private static final String SHARED_PREFS_NAME = "shared_image";
    private static final String KEY_BASE64 = "shared_image_base64";
    private static final String KEY_FILE_NAME = "shared_image_file_name";
    private static final String KEY_READY = "shared_image_ready";
    
    /**
     * Get shared image data from SharedPreferences
     * Returns base64 string and fileName, or null if no image exists
     * Clears the data after reading to prevent re-reading
     */
    @PluginMethod
    public void getSharedImage(PluginCall call) {
        try {
            SharedPreferences prefs = getSharedPreferences();
            
            String base64 = prefs.getString(KEY_BASE64, null);
            String fileName = prefs.getString(KEY_FILE_NAME, null);
            
            if (base64 == null || fileName == null) {
                // No shared image exists - return null values (not an error)
                JSObject result = new JSObject();
                result.put("base64", (String) null);
                result.put("fileName", (String) null);
                call.resolve(result);
                return;
            }
            
            // Clear the shared data after reading
            SharedPreferences.Editor editor = prefs.edit();
            editor.remove(KEY_BASE64);
            editor.remove(KEY_FILE_NAME);
            editor.remove(KEY_READY);
            editor.apply();
            
            // Return the shared image data
            JSObject result = new JSObject();
            result.put("base64", base64);
            result.put("fileName", fileName);
            call.resolve(result);
        } catch (Exception e) {
            android.util.Log.e("SharedImagePlugin", "Error in getSharedImage()", e);
            call.reject("Error getting shared image: " + e.getMessage());
        }
    }
    
    /**
     * Check if shared image exists without reading it
     * Useful for quick checks before calling getSharedImage()
     */
    @PluginMethod
    public void hasSharedImage(PluginCall call) {
        SharedPreferences prefs = getSharedPreferences();
        boolean hasImage = prefs.contains(KEY_BASE64) && prefs.contains(KEY_FILE_NAME);
        
        JSObject result = new JSObject();
        result.put("hasImage", hasImage);
        call.resolve(result);
    }
    
    /**
     * Get SharedPreferences instance for shared image data
     */
    private SharedPreferences getSharedPreferences() {
        Context context = getContext();
        if (context == null) {
            throw new IllegalStateException("Plugin context is null");
        }
        return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
    }
}

