package com.bonait.bnframework.modules.home.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
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 androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.common.constant.ConfigName;
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.bonait.bnframework.modules.home.fragment.mode.SectionItem;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class good_adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //图标
    private MyBitmapUtils myBitmapUtils = new MyBitmapUtils();
    private final LayoutInflater mLayoutInflater;

    private Context context;

    List<Object> liaos = new ArrayList<>();

    /**
     * 点击事件
     */
    public MyClickListener mListener = null;

    public good_adapter(Context context, List<Object> data, MyClickListener _mListener) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        liaos = data;
        mListener = _mListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.good_show, parent, false);
        return new MyLCViewHolder1(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        try {
            if (holder instanceof MyLCViewHolder1) {
                MyLCViewHolder1 myViewHolder = (MyLCViewHolder1) holder;

                SectionItem mode = (SectionItem) liaos.get(position);
                String name = mode.getTag();
                myViewHolder.IsSC = mode.getIsSC();
                String id = mode.getAccount();
                String url = mode.getAddress();
                boolean isCloud = mode.getIsCloud();//是否云端商品

//                myViewHolder.cardView.setRadius(20);//设置图片圆角的半径大小
//                myViewHolder.cardView.setCardElevation(10);//设置阴影部分大小
//                myViewHolder.cardView.setContentPadding(2, 2, 2, 2);//设置图片距离阴影大小

                if (isCloud) {
                    myViewHolder.Sc_text.setText("下载");
                    myViewHolder.sc_layout.setImageResource(R.mipmap.yxz);
                    myViewHolder.delete_text.setVisibility(View.GONE);
                    myViewHolder.sc_image.setVisibility(View.GONE);
                    if (mode.getIsdownload()) {
                        myViewHolder.Sc_text.setVisibility(View.GONE);
                        myViewHolder.sc_layout.setVisibility(View.GONE);
                    }
                } else {
                    myViewHolder.Sc_text.setText("上传");
                    myViewHolder.sc_layout.setImageResource(R.mipmap.ysc);

                    if (ConfigName.getInstance().user.userID.equals("研发用户")) {
                        myViewHolder.sc_image.setVisibility(View.GONE);
                        myViewHolder.delete_text.setVisibility(View.VISIBLE);
                        myViewHolder.Sc_text.setVisibility(View.VISIBLE);
                        myViewHolder.sc_layout.setVisibility(View.VISIBLE);

                    } else {
                        myViewHolder.sc_image.setVisibility(View.GONE);
                        myViewHolder.delete_text.setVisibility(View.GONE);
                        myViewHolder.Sc_text.setVisibility(View.GONE);
                        myViewHolder.sc_layout.setVisibility(View.GONE);
                    }
                }
                myViewHolder.tvTag.setText(name);
                myViewHolder.tvNote.setText("时间：" + mode.getNote() + "秒");
                //设置图片
                if (url != null && !url.isEmpty() && !url.equals("未知")) {
                    myBitmapUtils.disPlay(myViewHolder.ImageUrl, url);
                }
                //上传按钮点击
                myViewHolder.shangchuang.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (isCloud) {
                            ConfigData.getInstance().GetGoodProcess(holder.itemView.getContext(), id, true);
                        } else {
                            ConfigData.getInstance().UploadGoods(holder.itemView.getContext(), id);
                        }
                    }
                });
                //删除按钮点击
                myViewHolder.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) {
                                if (mListener != null) {
                                    mListener.clickListenerNew(view, 1, mode);
                                }
                                dialog.dismiss();
                            }
                        });
                    }
                });
                SetImage(myViewHolder.sc_image, myViewHolder.IsSC);
                //收藏按钮点击
                myViewHolder.sc_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        myViewHolder.IsSC = mode.getIsSC();
                        //按钮点击
                        String title = "收藏菜品操作提示！";
                        String message = "请问客官确定要[" + (myViewHolder.IsSC ? "取消收藏" : "收藏") + "]菜品吗?";
                        AlertDialogUtils.showDialog(holder.itemView.getContext(), title, message, new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                myViewHolder.IsSC = !myViewHolder.IsSC;
                                mode.SetIsSC(myViewHolder.IsSC);
                                SetImage(myViewHolder.sc_image, myViewHolder.IsSC);
                                notifyDataSetChanged();

                                SectionItem item = mode;
                                QueryDB.UpdateGoodsSC(item.getAccount(), item.getIsSC() ? 1 : 0);
                                mListener.clickListenerNew(view, 2, mode);
                                dialog.dismiss();
                            }
                        });
                    }
                });
                //点击查看当前商品信息
                myViewHolder.ImageUrl.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (isCloud) {
                            ToastUtils.warning("请先下载该商品！");
                        } else {
                            if (mListener != null) {
                                mListener.clickListenerNew(view, 4, mode);
                            }
                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

    private Activity findActivity(@NonNull Context context) {
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return findActivity(((ContextWrapper) context).getBaseContext());
        } else {
            return null;
        }
    }

    /**
     * 刷新
     *
     * @param
     */
    public void refresh() {
        Activity activity = findActivity(context);
        if (activity != null) {
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    try {
                        notifyDataSetChanged();
                    } catch (Exception e) {

                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return liaos.size();
    }

    public static class MyLCViewHolder1 extends RecyclerView.ViewHolder {
        private TextView tvTag, tvNote, tvAccount, Sc_text;

        public RelativeLayout delete_text, shangchuang;
        private ImageView ImageUrl, sc_layout;//图片

        private ImageView sc_image;//是否收藏

        private CardView cardView;
        public boolean IsSC = false;


        public MyLCViewHolder1(View view) {
            super(view);
            tvTag = view.findViewById(R.id.Tag_text);
            tvNote = view.findViewById(R.id.Note_text);
            Sc_text = view.findViewById(R.id.Sc_text);
            sc_layout = view.findViewById(R.id.sc_layout);
            delete_text = view.findViewById(R.id.delete_text);
            sc_image = view.findViewById(R.id.sc_image);//收藏
            ImageUrl = view.findViewById(R.id.ImageUrl);//图片
            shangchuang = view.findViewById(R.id.shangchuang);
            cardView = view.findViewById(R.id.cardView);
        }
    }
}


