package com.unis.sxzcl.ui.allround;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.model.bean.process.ProcessStepBean;
import com.unis.sxzcl.R;

import org.jetbrains.annotations.NotNull;

/**
 * 流程节点 适配器
 * 创建人: mQ
 * 时间: 2021/1/26 17:22
 * <p>
 * setMaxClickIndex() 设置最大可点击位置
 * setCurrentIndex() 设置选中位置
 * setSwitchEnable() 设置是否可点击
 * setSwitchCallback() 监听选中事件
 */
public class ProcessAdapter extends BaseQuickAdapter<ProcessStepBean, BaseViewHolder> implements LoadMoreModule {

    private int currentIndex = 0;

    private int maxClickIndex = 0;

    private boolean switchEnable = true;

    private SwitchCallback mSwitchCallback;


    public ProcessAdapter() {
        super(R.layout.item_process_step_layout);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, ProcessStepBean item) {
        int position = getItemPosition(item);
        // 标题
        TextView tvTitle = holder.getView(R.id.tv_title);
        tvTitle.setText(item.title);

        TextView leftLine = holder.getView(R.id.leftLine);
        // 第一项隐藏左侧线
        if (position == 0) leftLine.setVisibility(View.INVISIBLE);
        else leftLine.setVisibility(View.VISIBLE);

        TextView rightLine = holder.getView(R.id.rightLine);
        // 最后一项隐藏右侧线
        if (position == getData().size() - 1) rightLine.setVisibility(View.INVISIBLE);
        else rightLine.setVisibility(View.VISIBLE);

        ImageView tvCircle = holder.getView(R.id.tv_circle);
        // 列表项小于当前流程 高亮
        if (position <= currentIndex) {
            tvTitle.setEnabled(true);
            tvCircle.setSelected(true);
            leftLine.setEnabled(true);
            rightLine.setEnabled(true);
        } else {
            tvTitle.setEnabled(false);
            tvCircle.setSelected(false);
            leftLine.setEnabled(false);
            rightLine.setEnabled(false);
        }

        holder.itemView.setTag(item);
        holder.itemView.setOnClickListener(v -> {

            if (!switchEnable) return;

            // 刷新选中项
            ProcessStepBean bean = (ProcessStepBean) v.getTag();
            int index = getItemPosition(bean);
            if (currentIndex == index)
                return;
            if (maxClickIndex != -1 && index > maxClickIndex) {
                return;
            }
            currentIndex = index;
            notifyDataSetChanged();
            if (mSwitchCallback != null)
                mSwitchCallback.onSwitchListener(bean);
        });
    }

    /**
     * 设置是否可切换流程
     */
    public void setSwitchEnable(boolean switchEnable) {
        this.switchEnable = switchEnable;
    }

    /**
     * 设置当前选中流程位置
     */
    public void setCurrentIndex(int currentIndex) {
        this.currentIndex = currentIndex;
        notifyDataSetChanged();
    }

    /**
     * 获取当前选中流程位置
     */
    public int getCurrentIndex() {
        return currentIndex;
    }

    /**
     * 设置最大可选位置
     */
    public void setMaxClickIndex(int maxClickIndex) {
        this.maxClickIndex = maxClickIndex;
    }

    /**
     * 获取最大可选位置
     */
    public int getMaxClickIndex() {
        return maxClickIndex;
    }

    /**
     * 设置切换流程监听事件
     */
    public void setSwitchCallback(SwitchCallback switchCallback) {
        mSwitchCallback = switchCallback;
    }

    public interface SwitchCallback {
        void onSwitchListener(ProcessStepBean item);
    }
}
