package com.secureline.ui;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Reusable premium LemenzoOS timeline used across protection and license pages.
 * Each step owns its marker and connecting rail so the rail automatically follows
 * the real height of multi-line content without visual gaps.
 */
public final class SecureLineTimelineView extends LinearLayout {
    public SecureLineTimelineView(Context context) { this(context, null); }
    public SecureLineTimelineView(Context context, AttributeSet attrs) { this(context, attrs, 0); }
    public SecureLineTimelineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(VERTICAL);
        setClipChildren(false);
        setClipToPadding(false);
    }

    public void setSteps(CharSequence[] titles, CharSequence[] descriptions) {
        removeAllViews();
        if (titles == null || descriptions == null || titles.length != descriptions.length) {
            throw new IllegalArgumentException(
                    "Timeline titles and descriptions must have matching lengths");
        }
        for (int i = 0; i < titles.length; i++) {
            addView(createStep(i + 1, titles[i], descriptions[i], i == titles.length - 1));
        }
    }

    private View createStep(
            int number,
            CharSequence title,
            CharSequence description,
            boolean lastStep
    ) {
        LinearLayout row = new LinearLayout(getContext());
        row.setOrientation(HORIZONTAL);
        row.setGravity(Gravity.TOP);
        row.setClipChildren(false);
        row.setClipToPadding(false);

        // The marker and rail share one vertical column. The weighted rail stretches
        // to the actual text height, keeping long descriptions visually connected.
        LinearLayout indicatorColumn = new LinearLayout(getContext());
        indicatorColumn.setOrientation(VERTICAL);
        indicatorColumn.setGravity(Gravity.CENTER_HORIZONTAL);
        indicatorColumn.setClipChildren(false);
        indicatorColumn.setClipToPadding(false);
        row.addView(indicatorColumn, new LayoutParams(dp(34), LayoutParams.MATCH_PARENT));

        TextView marker = new TextView(getContext());
        marker.setGravity(Gravity.CENTER);
        marker.setText(String.valueOf(number));
        marker.setTextColor(getResources().getColor(
                R.color.sl_color_text_primary, getContext().getTheme()));
        marker.setTextSize(13);
        marker.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
        marker.setBackgroundResource(R.drawable.sl_detail_step);
        marker.setElevation(dp(3));
        indicatorColumn.addView(marker, new LayoutParams(dp(34), dp(34)));

        if (!lastStep) {
            View rail = new View(getContext());
            rail.setBackgroundResource(R.drawable.sl_detail_rail);
            LayoutParams railParams = new LayoutParams(dp(3), 0, 1f);
            railParams.topMargin = dp(5);
            indicatorColumn.addView(rail, railParams);
        }

        LinearLayout copy = new LinearLayout(getContext());
        copy.setOrientation(VERTICAL);
        copy.setPadding(0, 0, 0, lastStep ? 0 : dp(22));
        LayoutParams copyParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
        copyParams.leftMargin = dp(16);
        row.addView(copy, copyParams);

        TextView heading = new TextView(getContext());
        heading.setTextAppearance(R.style.SecureLineUI_Text_Heading);
        heading.setText(title);
        copy.addView(heading, new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        TextView body = new TextView(getContext());
        body.setTextAppearance(R.style.SecureLineUI_Text_BodySmall);
        body.setText(description);
        LayoutParams bodyParams = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        bodyParams.topMargin = dp(6);
        copy.addView(body, bodyParams);

        return row;
    }

    private int dp(int value) {
        return Math.round(value * getResources().getDisplayMetrics().density);
    }
}
