package com.casic.dcms.adapter;

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

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

import com.bumptech.glide.Glide;
import com.casic.dcms.R;
import com.casic.dcms.bean.PackageOrDeptBean;
import com.casic.dcms.bean.PunishTimesBean;
import com.casic.dcms.bean.PunishTypeBean;
import com.casic.dcms.utils.Constant;
import com.casic.dcms.utils.HttpRequestHelper;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.callback.IHttpRequestListener;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;

@SuppressLint("SetTextI18n")
public class PackageCaseAdapter extends RecyclerView.Adapter<PackageCaseAdapter.ItemViewHolder> {

    private static final String TAG = "PackageCaseAdapter";
    private Context context;
    private List<PackageOrDeptBean.DataBean.RowsBean> dataBeans;
    private LayoutInflater layoutInflater;
    private PunishTypeAdapter adapter;
    private Gson gson;

    public PackageCaseAdapter(Context context, List<PackageOrDeptBean.DataBean.RowsBean> dataRows) {
        this.context = context;
        this.dataBeans = dataRows;
        layoutInflater = LayoutInflater.from(context);
        gson = new Gson();
    }

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

    @Override
    public void onBindViewHolder(@NonNull PackageCaseAdapter.ItemViewHolder holder, int position) {
        holder.bindView(dataBeans.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 dataBeans.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private ImageView leftImageView;
        private TextView caseTitleView;
        private TextView caseDateView;
        private RecyclerView punishRecyclerView;

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


        void bindView(PackageOrDeptBean.DataBean.RowsBean rowsBean) {
            caseTitleView.setText(StringHelper.filterString(rowsBean.getDescription()));
            caseDateView.setText("上报时间：" + rowsBean.getReportTime());
            HttpRequestHelper.doRequest(Constant.SHOP_DETAIL, rowsBean.getShopId(), new IHttpRequestListener() {
                @Override
                public void onSuccess(ResponseBody resultBean) {
                    try {
                        String string = resultBean.string();
                        JSONObject jsonObject = new JSONObject(string);
                        int code = jsonObject.getInt("code");
                        if (code == 500) {
                            leftImageView.setBackgroundResource(R.drawable.ic_load_error);
                        } else {
                            JSONObject data = jsonObject.getJSONObject("data");
                            if (data.length() == 0) {
                                leftImageView.setBackgroundResource(R.drawable.ic_load_error);
                            } else {
                                JSONObject merchantInfo = data.getJSONObject("merchantInfo");
                                String images = merchantInfo.getString("images");
                                if (images.equals("")) {
                                    leftImageView.setBackgroundResource(R.drawable.ic_load_error);
                                } else {
                                    String img = images.split(",")[0];
                                    Glide.with(context).load(StringHelper.appendCompleteURL(img)).into(leftImageView);
                                }
                            }
                        }
                    } catch (JSONException | IOException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFailure(Throwable throwable) {

                }
            });
            HttpRequestHelper.doRequest(Constant.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(Constant.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;
    }
}
