package com.casic.dcms.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.casic.dcms.R;
import com.casic.dcms.model.PackageCaseBean;
import com.casic.dcms.utils.StringHelper;
import com.google.gson.Gson;
import com.pengxh.androidx.lite.hub.StringHub;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

public class PackageCaseListAdapter extends RecyclerView.Adapter<PackageCaseListAdapter.ItemViewHolder> {

    private static final String TAG = "PackageCaseAdapter";
    private final Context context;
    private final List<PackageCaseBean.DataDTO.RowsDTO> dataRows;
    private final LayoutInflater layoutInflater;
    private PunishTypeAdapter adapter;
    private final Gson gson = new Gson();

    public PackageCaseListAdapter(Context context, List<PackageCaseBean.DataDTO.RowsDTO> dataRows) {
        this.context = context;
        this.dataRows = dataRows;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(layoutInflater.inflate(R.layout.item_package_rv, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.bindView(dataRows.get(position));
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickListener.onClick(position);
                }
            });
        }
    }

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

    @SuppressLint("NotifyDataSetChanged")
    public void setRefreshData(List<PackageCaseBean.DataDTO.RowsDTO> dataRows) {
        this.dataRows.clear();
        this.dataRows.addAll(dataRows);
        notifyDataSetChanged();
    }

    public void setLoadMoreData(List<PackageCaseBean.DataDTO.RowsDTO> dataRows) {
        this.dataRows.addAll(dataRows);
        notifyItemRangeInserted(this.dataRows.size(), dataRows.size());
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private final QMUIRadiusImageView caseImageView;
        private final TextView caseTitleView;
        private final TextView caseDateView;
        private final RecyclerView casePunishView;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            caseImageView = itemView.findViewById(R.id.caseImageView);
            caseTitleView = itemView.findViewById(R.id.caseTitleView);
            caseDateView = itemView.findViewById(R.id.caseDateView);
            casePunishView = itemView.findViewById(R.id.casePunishView);
        }


        void bindView(PackageCaseBean.DataDTO.RowsDTO rowsBean) {
            //三包核实图片
            String fileIdVerify = rowsBean.getFileIdVerify();
            if (!TextUtils.isEmpty(fileIdVerify)) {
                String[] split = fileIdVerify.split(",");
                String firstImage = split[0];
                if (firstImage.endsWith(".jpg") || firstImage.contains(".jpeg") || firstImage.contains(".png")) {
                    Glide.with(context).load(StringHelper.appendCompleteURL(firstImage)).into(caseImageView);
                }
            }
            caseTitleView.setText(StringHub.filterSpaceOrEnter(rowsBean.getDescription()));
            caseDateView.setText("上报时间：" + rowsBean.getReportTime());
//            HttpRequestHelper.doRequest(LocaleConstant.SHOP_PUNISH_LIST, rowsBean.getShopId(), new IHttpRequestListener() {
//                @Override
//                public void onSuccess(ResponseBody resultBean) {
//                    try {
//                        PunishTypeBean punishTypeBean = gson.fromJson(resultBean.string(), PunishTypeBean.class);
//                        if (punishTypeBean.getData().size() != 0) {
//                            //获取商铺处罚次数
//                            HttpRequestHelper.doRequest(LocaleConstant.SHOP_PUNISH_TYPE, rowsBean.getShopId(), new IHttpRequestListener() {
//                                @Override
//                                public void onSuccess(ResponseBody resultBean) {
//                                    try {
//                                        PunishTimesBean punishTimesBean = gson.fromJson(resultBean.string(), PunishTimesBean.class);
//
//                                        adapter = new PunishTypeAdapter(context, punishTypeBean.getData(), punishTimesBean.getData());
//                                        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
//                                        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
//                                        punishRecyclerView.setLayoutManager(layoutManager);
//                                        punishRecyclerView.setAdapter(adapter);
//                                    } catch (IOException e) {
//                                        e.printStackTrace();
//                                    }
//                                }
//
//                                @Override
//                                public void onFailure(Throwable throwable) {
//
//                                }
//                            });
//                        }
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//
//                @Override
//                public void onFailure(Throwable throwable) {
//
//                }
//            });
        }
    }

    private OnItemClickListener clickListener;

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.clickListener = onItemClickListener;
    }
}
