package com.terrylinla.rnsketchcanvas;

import android.graphics.RectF;
import android.util.Log;

public final class Utility {
    /**
     * Calculates the destination rectangle for drawing an image based on the content mode
     * 
     * @param imgWidth Width of the source image
     * @param imgHeight Height of the source image
     * @param targetWidth Width of the target area
     * @param targetHeight Height of the target area
     * @param mode Content mode ("AspectFill", "AspectFit", or "ScaleToFill")
     * @return A RectF representing the destination rectangle for drawing
     */
    public static RectF fillImage(float imgWidth, float imgHeight, float targetWidth, float targetHeight, String mode) {
        // Handle invalid dimensions
        if (imgWidth <= 0 || imgHeight <= 0 || targetWidth <= 0 || targetHeight <= 0) {
            Log.w("Utility", "Invalid dimensions for fillImage: " + 
                  imgWidth + "x" + imgHeight + " -> " + targetWidth + "x" + targetHeight);
            return new RectF(0, 0, targetWidth, targetHeight);
        }
        
        float imageAspectRatio = imgWidth / imgHeight;
        float targetAspectRatio = targetWidth / targetHeight;
        
        RectF result;
        
        switch (mode) {
            case "AspectFill": {
                // Scale to fill the target area while maintaining aspect ratio
                // Some parts of the image may be cropped
                float scaleFactor = targetAspectRatio < imageAspectRatio ? 
                    targetHeight / imgHeight : 
                    targetWidth / imgWidth;
                
                float w = imgWidth * scaleFactor;
                float h = imgHeight * scaleFactor;
                
                // Center the image in the target area
                result = new RectF(
                    (targetWidth - w) / 2, 
                    (targetHeight - h) / 2,
                    w + (targetWidth - w) / 2, 
                    h + (targetHeight - h) / 2
                );
                break;
            }
            case "ScaleToFill": {
                // Scale to fill the target area without maintaining aspect ratio
                result = new RectF(0, 0, targetWidth, targetHeight);
                break;
            }
            case "AspectFit":
            default: {
                // Scale to fit the target area while maintaining aspect ratio
                // The entire image will be visible
                float scaleFactor = targetAspectRatio > imageAspectRatio ? 
                    targetHeight / imgHeight : 
                    targetWidth / imgWidth;
                
                float w = imgWidth * scaleFactor;
                float h = imgHeight * scaleFactor;
                
                // Center the image in the target area
                result = new RectF(
                    (targetWidth - w) / 2, 
                    (targetHeight - h) / 2,
                    w + (targetWidth - w) / 2, 
                    h + (targetHeight - h) / 2
                );
                break;
            }
        }
        
        // Log the calculation for debugging
        Log.d("Utility", "fillImage: " + imgWidth + "x" + imgHeight + 
              " -> " + targetWidth + "x" + targetHeight + 
              " mode: " + mode + 
              " result: " + result.toString());
        
        return result;
    }
}