package com.casic.dcms.adapter;

import android.content.Context;
import android.text.TextUtils;
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.RecyclerView;

import com.amap.api.services.core.LatLonPoint;
import com.casic.dcms.R;
import com.casic.dcms.bean.CleanRecordBean;
import com.casic.dcms.utils.LocationHelper;
import com.casic.dcms.utils.OtherUtils;
import com.casic.dcms.utils.StringHelper;
import com.casic.dcms.utils.callback.IAddressListener;

import java.util.ArrayList;
import java.util.List;

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

    private Context context;
    private List<CleanRecordBean.DataBean.RowsBean> dataRows;
    private LayoutInflater layoutInflater;

    public RecordListAdapter(Context context, List<CleanRecordBean.DataBean.RowsBean> dataRows) {
        this.context = context;
        this.dataRows = dataRows;
        layoutInflater = LayoutInflater.from(context);
    }

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

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, 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();
    }

    private OnItemClickListener clickListener;

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

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

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView workTimeView;
        private TextView toiletNameView;
        private TextView workerPositionView;
        private ImageView descriptionImageView;

        ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            workTimeView = itemView.findViewById(R.id.workTimeView);
            toiletNameView = itemView.findViewById(R.id.toiletNameView);
            workerPositionView = itemView.findViewById(R.id.workerPositionView);
            descriptionImageView = itemView.findViewById(R.id.descriptionImageView);
        }

        void bindView(CleanRecordBean.DataBean.RowsBean rowsBean) {
            toiletNameView.setText(rowsBean.getToiletName().equals("") ? "未知" : rowsBean.getToiletName());
            if (rowsBean.getPhoto().equals("")) {
                descriptionImageView.setVisibility(View.GONE);
            } else {
                descriptionImageView.setVisibility(View.VISIBLE);
                ArrayList<String> urls = new ArrayList<>();
                urls.add(StringHelper.appendCompleteURL(rowsBean.getPhoto()));
                descriptionImageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        OtherUtils.showBigImage(context, 0, urls);
                    }
                });
            }

            String lng = rowsBean.getLng();
            if (TextUtils.isEmpty(lng)) {
                return;
            }
            String lat = rowsBean.getLat();
            if (TextUtils.isEmpty(lat)) {
                return;
            }
            LatLonPoint latLonPoint = new LatLonPoint(Double.parseDouble(lat), Double.parseDouble(lng));
            LocationHelper.antiCodingLocation(context, latLonPoint, new IAddressListener() {
                @Override
                public void onGetAddress(String address) {
                    workerPositionView.setText(address);
                }
            });
            workTimeView.setText(rowsBean.getCreateTime());
        }
    }
}
