package ezviz.ezopensdkcommon.common;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import ezviz.ezopensdkcommon.R;

/**
 * 步骤进度条控件
 * <p>
 * 支持通过 XML 属性或代码设置总步骤数和当前完成步骤数。
 * 已完成的步骤显示蓝色，未完成的显示灰色。
 * </p>
 * <p>使用示例：</p>
 * <pre>
 * &lt;ezviz.ezopensdkcommon.common.StepProgressBar
 *     android:id="@+id/stepProgressBar"
 *     android:layout_width="match_parent"
 *     android:layout_height="wrap_content"
 *     app:stepCount="4"
 *     app:currentStep="1" /&gt;
 * </pre>
 */
public class StepProgressBar extends LinearLayout {

    /** 默认总步骤数 */
    private static final int DEFAULT_STEP_COUNT = 4;
    /** 默认当前完成步骤数 */
    private static final int DEFAULT_CURRENT_STEP = 0;

    /** 步骤条高度(dp) */
    private static final int BAR_HEIGHT_DP = 3;
    /** 步骤条之间的间距(dp) */
    private static final int BAR_GAP_DP = 4;

    private int mStepCount;
    private int mCurrentStep;

    public StepProgressBar(Context context) {
        this(context, null);
    }

    public StepProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StepProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        setOrientation(HORIZONTAL);
        // 设置默认内边距
        int paddingHorizontal = dpToPx(74);
        int paddingTop = dpToPx(4);
        int paddingBottom = dpToPx(8);
        setPadding(paddingHorizontal, paddingTop, paddingHorizontal, paddingBottom);

        // 读取 XML 属性
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StepProgressBar);
            mStepCount = ta.getInt(R.styleable.StepProgressBar_stepCount, DEFAULT_STEP_COUNT);
            mCurrentStep = ta.getInt(R.styleable.StepProgressBar_currentStep, DEFAULT_CURRENT_STEP);
            ta.recycle();
        } else {
            mStepCount = DEFAULT_STEP_COUNT;
            mCurrentStep = DEFAULT_CURRENT_STEP;
        }

        buildStepViews();
    }

    /**
     * 设置当前完成的步骤数
     *
     * @param step 完成步骤数（从1开始，0表示全部未完成）
     */
    public void setCurrentStep(int step) {
        if (step < 0) step = 0;
        if (step > mStepCount) step = mStepCount;
        mCurrentStep = step;
        updateStepStates();
    }

    /**
     * 获取当前完成的步骤数
     */
    public int getCurrentStep() {
        return mCurrentStep;
    }

    /**
     * 设置总步骤数（会重建子View）
     *
     * @param count 总步骤数
     */
    public void setStepCount(int count) {
        if (count < 1) count = 1;
        mStepCount = count;
        buildStepViews();
    }

    /**
     * 构建步骤条子 View
     */
    private void buildStepViews() {
        removeAllViews();
        int barHeight = dpToPx(BAR_HEIGHT_DP);
        int barGap = dpToPx(BAR_GAP_DP);

        for (int i = 0; i < mStepCount; i++) {
            View stepView = new View(getContext());
            LayoutParams lp = new LayoutParams(0, barHeight, 1f);
            // 除最后一个外，右侧加间距
            if (i < mStepCount - 1) {
                lp.rightMargin = barGap;
            }
            stepView.setLayoutParams(lp);
            // 设置圆角背景
            if (i < mCurrentStep) {
                stepView.setBackgroundResource(R.drawable.bg_step_done);
            } else {
                stepView.setBackgroundResource(R.drawable.bg_step_pending);
            }
            addView(stepView);
        }
    }

    /**
     * 更新步骤条的完成状态
     */
    private void updateStepStates() {
        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (i < mCurrentStep) {
                child.setBackgroundResource(R.drawable.bg_step_done);
            } else {
                child.setBackgroundResource(R.drawable.bg_step_pending);
            }
        }
    }

    private int dpToPx(int dp) {
        return (int) (dp * getContext().getResources().getDisplayMetrics().density + 0.5f);
    }
}
