package com.bonait.bnframework.common.linktab.good;

import android.content.res.Resources;
import android.text.InputType;
import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODSTYPE;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.linktab.LinkMode;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.List;

public class GoodLeftAdapter extends BaseQuickAdapter<LinkMode, BaseViewHolder> {
    public List<LinkMode> Fdata;
    public Resources resources;
    public LinearLayoutManager Manager;

    public GoodLeftAdapter(@LayoutRes int layoutResId, @Nullable List<LinkMode> data, Resources _resources, LinearLayoutManager _manager) {
        super(layoutResId, data);
        resources = _resources;
        Fdata = data;
        Manager = _manager;
    }

    @Override
    protected void convert(BaseViewHolder holder, LinkMode item) {

        BPA_GOODSTYPE type = (BPA_GOODSTYPE) item.type;
        holder.setText(R.id.tv_type, type.name);
        int index_d = Fdata.indexOf(item);
        if (item.isChoose) {
            holder.setBackgroundColor(R.id.tv_linearlayout, resources.getColor(R.color.main_background));
            holder.setTextColor(R.id.tv_type, resources.getColor(R.color.app_color_blue));
            holder.setVisible(R.id.tv_image, true);
            holder.setVisible(R.id.tv_update, true);
            holder.setVisible(R.id.tv_delete, true);


        } else {
            holder.setBackgroundColor(R.id.tv_linearlayout, resources.getColor(R.color.white));
            holder.setTextColor(R.id.tv_type, resources.getColor(R.color.tab_text_normal));
            holder.setVisible(R.id.tv_image, false);
            holder.setVisible(R.id.tv_update, false);
            holder.setVisible(R.id.tv_delete, false);
        }

        //选中行
        holder.getView(R.id.tv_type).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setChoose(index_d);
                Manager.scrollToPositionWithOffset(index_d, 0);
            }
        });

        //修改
        holder.getView(R.id.tv_update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final QMUIDialog.EditTextDialogBuilder builder_add =
                        new QMUIDialog.EditTextDialogBuilder(getContext());
                builder_add.setTitle("修改类型")
                        .setPlaceholder("请输入")
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text = builder_add.getEditText().getText();
                                if (text != null && text.length() > 0) {
                                    if (QueryDB.GetGoodsTypeIs(text.toString())) {
                                        ToastUtils.info("数据重复,请重新输入后重试!");
                                        return;
                                    }
                                    type.name = text.toString();
                                    QueryDB.UpdateGoodsType(type);
                                    ToastUtils.info("修改成功");
                                    notifyDataSetChanged();
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.info("数据不能为空！");
                                }
                            }
                        })
                        .show();
            }
        });

        //删除
        holder.getView(R.id.tv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialogUtils.showDialog(getContext(),
                        "删除提示！",
                        "客官确定要删除类型,[" +type.name+ "]吗？",
                        new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                Detele(index_d);
                                dialog.dismiss();
                            }
                        });
            }
        });
    }

    /**
     * 删除数据
     *
     * @param index
     */
    public void Detele(int index) {
        QueryDB.DeleteGoodsType((BPA_GOODSTYPE) Fdata.get(index).type);
        Fdata.remove(index);
        notifyDataSetChanged();
    }

    /**
     * 设置是否选择
     *
     * @param position
     */
    public void setChoose(Integer position) {
        for (LinkMode it : Fdata) {
            it.isChoose = false;
        }
        Fdata.get(position).isChoose = true;
        notifyDataSetChanged();
    }
}
