package com.handset.serialportsensor.adapter;

import android.text.TextUtils;

import com.blankj.utilcode.util.SPUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.handset.serialportsensor.R;
import com.handset.serialportsensor.bean.LeftDetailBean;
import com.handset.serialportsensor.bean.LeftHeadBean;

import java.util.List;

public class LeftAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    public static final int TYPE_HEAD = 1;
    public static final int TYPE_DETAIL = 2;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public LeftAdapter(List<MultiItemEntity> data) {
        super(data);

        addItemType(TYPE_HEAD, R.layout.left_rv_item_head);
        addItemType(TYPE_DETAIL, R.layout.left_rv_item_detail);
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_HEAD:
                if (item instanceof LeftHeadBean) {
                    helper.setImageResource(R.id.left_rv_item_image, ((LeftHeadBean) item).getImageRes())
                            .setText(R.id.left_rv_item_title, ((LeftHeadBean) item).getTitle())
                            .setText(R.id.left_rv_item_value,
                                    TextUtils.isEmpty(((LeftHeadBean) item).getValue()) ? ""
                                            : ((LeftHeadBean) item).getValue());

                    helper.itemView.setOnClickListener(v -> {
                        collapseOrExpand(item);
                    });
                }
                break;
            case TYPE_DETAIL:
                if (item instanceof LeftDetailBean) {
                    helper.setText(R.id.left_rv_item_item, ((LeftDetailBean) item).getItem())
                            .setChecked(R.id.left_rv_item_check, ((LeftDetailBean) item).isCheck());

                    helper.itemView.setOnClickListener(v -> {
                        int parentPosition = getParentPosition(item);
                        LeftHeadBean headBean = (LeftHeadBean) getData().get(parentPosition);

                        List<LeftDetailBean> subItems = headBean.getSubItems();
                        for (int i = 0; i < subItems.size(); i++) {
                            if (subItems.get(i).isCheck()) {
                                subItems.get(i).setCheck(false);
                                break;
                            }
                        }

                        int subPosition = helper.getAdapterPosition() - parentPosition - 1;
                        LeftDetailBean bean = subItems.get(subPosition);
                        bean.setCheck(true);
                        headBean.setValue(String.valueOf(bean.getItem()));
                        SPUtils.getInstance().put(headBean.getSpKey(), headBean.getValue());
                        notifyDataSetChanged();
                    });
                }
                break;
        }
    }

    public void collapseOrExpand(MultiItemEntity item) {
        int position = getParentPosition(item);
        if (((LeftHeadBean) item).isExpanded()) {
            collapse(position);
        } else {
            expand(position);
        }
    }
}
