package com.bonait.bnframework.modules.home.fragment.mode;


import android.content.Context;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.constant.MessageName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.image.MyBitmapUtils;
import com.bonait.bnframework.common.message.MessageManager;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.section.QMUIDefaultStickySectionAdapter;
import com.qmuiteam.qmui.widget.section.QMUISection;

public class QDListSectionAdapter extends QMUIDefaultStickySectionAdapter {
    private TextView tvTag ,tvNote,tvAccount,Sc_text,delete_text;

    private ImageView ImageUrl;//图片

    private ImageView sc_image;//是否收藏

    public boolean IsSC=false;

    //图标
    private MyBitmapUtils myBitmapUtils=new MyBitmapUtils();
    /**
     * 点击事件
     */
    public MyClickListener mListener=null;

    @NonNull
    @Override
    protected ViewHolder onCreateSectionHeaderViewHolder(@NonNull ViewGroup viewGroup) {
        return new ViewHolder(new QDSectionHeaderView(viewGroup.getContext()));
    }
    public QDListSectionAdapter() {
    }

    @Override
    protected void onBindSectionHeader(final ViewHolder holder, final int position, QMUISection section) {

        QDSectionHeaderView itemView = (QDSectionHeaderView) holder.itemView;
        itemView.render((SectionHeader) section.getHeader(), section.isFold());
        //点击事件
        itemView.getArrowView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.isForStickyHeader ? position : holder.getAdapterPosition();
                toggleFold(pos, false);
            }
        });
    }

    @Override
    protected void onBindSectionItem(ViewHolder holder, int position, QMUISection section, int itemIndex) {
        //获取id
        tvTag = holder.itemView.findViewById(R.id.Tag_text);
        tvNote = holder.itemView.findViewById(R.id.Note_text);
        Sc_text = holder.itemView.findViewById(R.id.Sc_text);
        delete_text = holder.itemView.findViewById(R.id.delete_text);
        sc_image=holder.itemView.findViewById(R.id.sc_image);//收藏
        ImageUrl=holder.itemView.findViewById(R.id.ImageUrl);//图片
        //设置文本

        String name=((SectionItem)section.getItemAt(itemIndex)).getTag();
        IsSC=((SectionItem)section.getItemAt(itemIndex)).getIsSC();
        String id= ((SectionItem)section.getItemAt(itemIndex)).getAccount();
        String url=((SectionItem)section.getItemAt(itemIndex)).getAddress();
        tvTag.setText(name);
        tvNote.setText("时间："+ ((SectionItem)section.getItemAt(itemIndex)).getNote()+"秒");
        //设置图片
        if(!url.equals("未知"))
        {
            myBitmapUtils.disPlay(ImageUrl,url);
        }
        //ImageUrl.setBackground(holder.itemView.getContext().getResources().getDrawable(R.mipmap.hgr));
        //上传按钮点击
        Sc_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.info("点击上传按钮,菜品:"+name);
            }
        });
        //删除按钮点击
        delete_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //按钮点击
                String title = "删除菜品操作提示！";
                String message = "请问客官确定要删除["+name+"]菜品吗?";
                AlertDialogUtils.showDialog(holder.itemView.getContext(), title, message, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        MessageManager.getInstance().sendMessage(MessageName.DeleteGood, ((SectionItem)section.getItemAt(itemIndex)));
                        dialog.dismiss();
                    }
                });
            }
        });
        SetImage(sc_image,IsSC);
        //收藏按钮点击
        sc_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IsSC=((SectionItem)section.getItemAt(itemIndex)).getIsSC();
                //按钮点击
                String title = "收藏菜品操作提示！";
                String message = "请问客官确定要["+(IsSC?"取消收藏":"收藏")+"]菜品吗?";
                AlertDialogUtils.showDialog(holder.itemView.getContext(), title, message, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        IsSC=!IsSC;
                        ((SectionItem)section.getItemAt(itemIndex)).SetIsSC(IsSC);
                        SetImage(sc_image,IsSC);
                        notifyDataSetChanged();

                        SectionItem item=((SectionItem)section.getItemAt(itemIndex));
                        QueryDB.UpdateGoodsSC(item.getAccount(),item.getIsSC()?1:0);
                        MessageManager.getInstance().sendMessage(MessageName.ScGood,"Good");
                        dialog.dismiss();
                    }
                });
            }
        });
        //点击查看当前商品信息
        ImageUrl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MessageManager.getInstance().sendMessage(MessageName.ClickGood, ((SectionItem)section.getItemAt(itemIndex)));
            }
        });
    }

    public void SetImage(ImageView image,boolean isxz)
    {
        if(isxz)
        {
            image.setImageResource(R.mipmap.sc3);
        }else
        {
            image.setImageResource(R.mipmap.wsc3);
        }
    }

    @NonNull
    @Override
    protected ViewHolder onCreateSectionItemViewHolder(@NonNull ViewGroup viewGroup) {
        Context context = viewGroup.getContext();
        //这里是通过xml绑定布局，当然你也可以用代码new出来，方法请看头部视图
        View itemView = LayoutInflater.from(context).inflate(R.layout.note_rv_tree_second, viewGroup, false);
        return new ViewHolder(itemView);
    }
}