package com.foxx.androidcast.stats.ui;

/*********************************************************************
 * SessionBarChartView.java
 * Created at: Tue 19 May 2026 19:43:06 +0200
 * Updated at: Wed 20 May 2026 15:17:13 +0200 by Anton Afanasyeu <a.afanasieff@gmail.com>
 * Commit: 5d8e82d2e60a21fff3138d2a394ee4e8b4c6dcb8
 * class SessionBarChartView
 * Contributors:
 *    - Anton Afanasyeu <a.afanasieff@gmail.com> (2 commits, 117 lines)
 *    - Cursor Agent (project assistant)
 * Digest: SHA256	1c4f6cdf1b341bd492b386a2d7ce51da0bcf76a4c8c52843d7fc1eeb8c75abde
**********************************************************************/
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/** Horizontal bar chart for UDP / protection counter summaries. */
public class SessionBarChartView extends View {
    private static final class Bar {
        final String label;
        final long value;
        final int color;

        Bar(String label, long value, int color) {
            this.label = label;
            this.value = value;
            this.color = color;
        }
    }

    private final Paint barPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint labelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final List<Bar> bars = new ArrayList<>();

    public SessionBarChartView(Context context) {
        super(context);
        init();
    }

    public SessionBarChartView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        textPaint.setColor(0xFFEEEEEE);
        textPaint.setTextSize(24f);
        labelPaint.setColor(0xFFAAAAAA);
        labelPaint.setTextSize(22f);
    }

    public void setSession(@Nullable SessionRecord session) {
        bars.clear();
        if (session != null) {
            SessionRecord.UdpStats u = session.udp;
            addBar("UDP tx", u.datagramsTx, 0xFF64B5F6);
            addBar("UDP rx", u.datagramsRx, 0xFF42A5F5);
            addBar("Reasm OK", u.reassemblyOk, 0xFF81C784);
            addBar("Reasm lost", u.reassemblyLost, 0xFFFF8A65);
            addBar("Wire drop", u.wireDropped, 0xFFE57373);
            addBar("Vid dec err", u.recvVideoDecodeErrors, 0xFFFF5252);
            SessionRecord.ProtectionStats p = session.protection;
            addBar("FEC enc", p.fecEncoded, 0xFFBA68C8);
            addBar("FEC dec", p.fecDecoded, 0xFFAB47BC);
            addBar("NACK rtx", p.nackRetransmits, 0xFFFFD54F);
        }
        requestLayout();
        invalidate();
    }

    private void addBar(String label, long value, int color) {
        if (value > 0) {
            bars.add(new Bar(label, value, color));
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int h = bars.isEmpty() ? 80 : Math.max(120, bars.size() * 36 + 24);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), h);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (bars.isEmpty()) {
            labelPaint.setTextSize(26f);
            canvas.drawText("No counter data", 24f, 48f, labelPaint);
            return;
        }
        long max = 1L;
        for (Bar b : bars) {
            max = Math.max(max, b.value);
        }
        float rowH = 32f;
        float y = 12f;
        float labelW = 140f;
        float barLeft = labelW + 16f;
        float barMaxW = getWidth() - barLeft - 80f;
        for (Bar b : bars) {
            labelPaint.setColor(0xFFAAAAAA);
            canvas.drawText(b.label, 8f, y + 22f, labelPaint);
            float bw = barMaxW * (b.value / (float) max);
            barPaint.setColor(b.color);
            canvas.drawRect(barLeft, y + 4f, barLeft + bw, y + 28f, barPaint);
            textPaint.setColor(0xFFEEEEEE);
            canvas.drawText(String.format(Locale.US, "%d", b.value), barLeft + bw + 8f, y + 22f, textPaint);
            y += rowH;
        }
    }
}
