package com.foxx.androidcast.ui;

/*********************************************************************
 * PlaybackTextureTransform.java
 * Created at: Sat 16 May 2026 18:50:36 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class PlaybackTextureTransform
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 48 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	aacbd51ce31e50fcb30b9707ea24f855192575b317348689c2773e176ed3aaec
**********************************************************************/
import android.graphics.Matrix;
import android.view.TextureView;

/**
 * Letterboxes decoded video in a {@link TextureView}.
 * Uses view-space scale around center (same approach as ExoPlayer), not pixel {@link Matrix#setRectToRect}.
 */
public final class PlaybackTextureTransform {
    private PlaybackTextureTransform() {}

    public static void applyFitCenter(TextureView view, int videoWidth, int videoHeight) {
        if (view == null || videoWidth <= 0 || videoHeight <= 0) {
            return;
        }
        int viewW = view.getWidth();
        int viewH = view.getHeight();
        if (viewW <= 0 || viewH <= 0) {
            return;
        }
        float videoAspect = (float) videoWidth / videoHeight;
        float viewAspect = (float) viewW / viewH;
        float scaleX = 1f;
        float scaleY = 1f;
        if (videoAspect > viewAspect) {
            scaleY = viewAspect / videoAspect;
        } else {
            scaleX = videoAspect / viewAspect;
        }
        Matrix matrix = new Matrix();
        matrix.setScale(scaleX, scaleY, viewW / 2f, viewH / 2f);
        view.setTransform(matrix);
    }

    public static void clearTransform(TextureView view) {
        if (view != null) {
            view.setTransform(new Matrix());
        }
    }
}
