package com.unis.sxzcl.widget.multi_list;

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

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseNodeAdapter;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.module.LoadMoreModule;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.unis.sxzcl.R;
import com.xyxsbj.mylibrary.tool.DPTool;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包名: com.unis.sxzcl.widget.multi_list
 * 类名: MultiListAdapter
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/1 11:17
 * 更新人:
 * 更新时间: 2020/12/1 11:17
 * 更新说明:
 * 版本号: 1.0
 */
public class MultiListAdapter extends BaseNodeAdapter implements LoadMoreModule {

    public Callback mCallback;
    private Map<Integer, Integer> mLeftPaddingMap = new HashMap<>();

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    public MultiListAdapter(Context context) {
        super();
        // 初始化Item页面
        addNodeProvider(new BaseMultiProvider(1));
        addNodeProvider(new BaseMultiProvider(2));
        addNodeProvider(new BaseMultiProvider(3));
        addNodeProvider(new BaseMultiProvider(4));
        addNodeProvider(new BaseMultiProvider(5));
        // 初始化边距
        mLeftPaddingMap.put(1, DPTool.dp2px(context, 16));
        mLeftPaddingMap.put(2, DPTool.dp2px(context, 32));
        mLeftPaddingMap.put(3, DPTool.dp2px(context, 48));
        mLeftPaddingMap.put(4, DPTool.dp2px(context, 64));
        mLeftPaddingMap.put(5, DPTool.dp2px(context, 80));
    }


    @Override
    protected int getItemType(@NotNull List<? extends BaseNode> list, int i) {
        // 根据数据类型设定层级
        BaseNode node = list.get(i);
        if (node instanceof BaseMultiBean)
            return ((BaseMultiBean) node).level;
        return -1;
    }

    public class BaseMultiProvider extends BaseNodeProvider {

        private int type;

        public BaseMultiProvider(int type) {
            this.type = type;
        }

        @Override
        public int getItemViewType() {
            return type;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_multi_list;
        }

        @Override
        public void convert(@NotNull BaseViewHolder holder, BaseNode node) {
            BaseMultiBean item = (BaseMultiBean) node;
            // 是否显示图标
            View cardIv = holder.getView(R.id.card_iv);
            if (item.icon == null) {
                cardIv.setVisibility(View.GONE);
            } else {
                cardIv.setVisibility(View.VISIBLE);
                Glide.with(getContext())
                        .load(item.icon)
                        .placeholder(R.drawable.ic_img_loading)
                        .error(R.drawable.no_banner)
                        .into((ImageView) holder.getView(R.id.iv_show));
            }
            // 标题
            holder.setText(R.id.tv_name, item.name);

            // 是否显示描述
            TextView tvContent = holder.getView(R.id.tv_content);
            tvContent.setVisibility(item.content != null ? View.VISIBLE : View.GONE);
            tvContent.setText(item.content);


            // 设置边距
            int paddingTop = holder.itemView.getPaddingTop();
            int paddingRight = holder.itemView.getPaddingRight();
            int paddingBottom = holder.itemView.getPaddingBottom();
            int paddingLeft = mLeftPaddingMap.get(type);
            holder.itemView.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);


            // 展开 收缩
            ImageView ivFlag = holder.getView(R.id.iv_flag);
            // 箭头标记
            ivFlag.setSelected(item.isExpanded());
            if (item.getChildNode() == null || item.getChildNode().size() == 0) {
                ivFlag.setVisibility(View.GONE);
            } else {
                ivFlag.setVisibility(View.VISIBLE);
            }
            //点击事件
            ivFlag.setTag(node);
            ivFlag.setOnClickListener(v -> {
                BaseMultiBean bean = (BaseMultiBean) v.getTag();
                v.setSelected(!bean.isExpanded());
                getAdapter().expandOrCollapse(getItemPosition(bean));
            });

            // 项点击事件
            holder.itemView.setTag(node);
            holder.itemView.setOnClickListener(v -> {
//                if (mCallback != null && (item.getChildNode() == null || item.getChildNode().size() == 0))
                if (mCallback != null)
                    mCallback.onItemClickListener((BaseNode) v.getTag());
            });
        }

        @Override
        public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
            super.onClick(helper, view, data, position);
            BaseMultiBean item = (BaseMultiBean) data;
            helper.getView(R.id.iv_flag).setSelected(!item.isExpanded());
            getAdapter().expandOrCollapse(position);

        }

    }

    public interface Callback {
        public void onItemClickListener(BaseNode data);
    }

}
