package com.unis.sxzcl.ui.aeration;

import android.view.View;
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;

/**
 * 包名: com.unis.sxzcl.ui.aeration
 * 类名: AerationStepAdapter
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/15 17:09
 * 更新人:
 * 更新时间: 2020/12/15 17:09
 * 更新说明:
 * 版本号: 1.0
 */
/**
 * 机械通风详情顶部流程适配器
 * 创建人: mQ
 * 时间: 2020/12/15 17:09
 */
public class AerationStepAdapter extends BaseQuickAdapter<ProcessStepBean, BaseViewHolder> implements LoadMoreModule {

    private int currentIndex = 0;

    private int maxClickIndex = 0;

    private String pageType = "add";

    public void setPageType(String pageType) {
        if (pageType == null) return;
        this.pageType = pageType;
    }

    private SwitchCallback mSwitchCallback;


    public AerationStepAdapter() {
        super(R.layout.item_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);

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

        holder.itemView.setTag(item);
        holder.itemView.setOnClickListener(v -> {
            // 详情时不切换流程
            if (pageType.equals("detail")) 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 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);
    }
}
