package com.foxx.androidcast.ui;

/*********************************************************************
 * AspectRatioFrameLayout.java
 * Created at: Fri 15 May 2026 16:49:02 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class AspectRatioFrameLayout
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (3 commits, 85 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	aff69d975c86ce78d354f5c93fdbc7fc120ef82bed50f71c89f9ac9c4837a9d0
**********************************************************************/
import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

/**
 * Centers a child and sizes it to fit inside the parent while preserving video aspect ratio.
 */
public class AspectRatioFrameLayout extends FrameLayout {
    private float aspectRatio;

    public AspectRatioFrameLayout(Context context) {
        super(context);
    }

    public AspectRatioFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setAspectRatio(int videoWidth, int videoHeight) {
        if (videoWidth <= 0 || videoHeight <= 0) {
            aspectRatio = 0f;
        } else {
            aspectRatio = (float) videoWidth / videoHeight;
        }
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int parentW = MeasureSpec.getSize(widthMeasureSpec);
        int parentH = MeasureSpec.getSize(heightMeasureSpec);
        if (getChildCount() == 0) {
            setMeasuredDimension(parentW, parentH);
            return;
        }
        if (aspectRatio <= 0f) {
            int fullW = MeasureSpec.makeMeasureSpec(parentW, MeasureSpec.EXACTLY);
            int fullH = MeasureSpec.makeMeasureSpec(parentH, MeasureSpec.EXACTLY);
            measureChild(getChildAt(0), fullW, fullH);
            setMeasuredDimension(parentW, parentH);
            return;
        }
        int childW;
        int childH;
        float parentRatio = (float) parentW / parentH;
        if (parentRatio > aspectRatio) {
            childH = parentH;
            childW = Math.round(childH * aspectRatio);
        } else {
            childW = parentW;
            childH = Math.round(childW / aspectRatio);
        }
        int wSpec = MeasureSpec.makeMeasureSpec(childW, MeasureSpec.EXACTLY);
        int hSpec = MeasureSpec.makeMeasureSpec(childH, MeasureSpec.EXACTLY);
        measureChild(getChildAt(0), wSpec, hSpec);
        setMeasuredDimension(parentW, parentH);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        if (getChildCount() == 0) {
            return;
        }
        if (aspectRatio <= 0f) {
            getChildAt(0).layout(left, top, right, bottom);
            return;
        }
        int parentW = right - left;
        int parentH = bottom - top;
        int childW = getChildAt(0).getMeasuredWidth();
        int childH = getChildAt(0).getMeasuredHeight();
        int childLeft = left + (parentW - childW) / 2;
        int childTop = top + (parentH - childH) / 2;
        getChildAt(0).layout(childLeft, childTop, childLeft + childW, childTop + childH);
    }
}
