package com.tofly.ghrq_route.common.ui.adapter;

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.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.BaseApp;
import com.tofly.ghrq_route.base.url.NetRequestURL;
import com.tofly.ghrq_route.common.ui.view.CircleImageView;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.module.user.domain.UserBean;
import com.tofly.ghrq_route.common.util.date.DateUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/2/16.
 */

public class DangerLocationAdapter extends ARecyclerBaseAdapter<DangerLocationAdapter.MyViewHolder, TroubleBean> {
    private Context mContext;
    private List<TroubleBean> mData;
    private ImageLoader imageLoader;

    public DangerLocationAdapter(Context context, List<TroubleBean> troubles) {
        super(context, troubles);
        mContext = context;
        mData = troubles;
        imageLoader = ImageLoader.getInstance();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.listview_reported_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(rootView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder viewHolder, int position) {
        TroubleBean trouble = mData.get(position);
        if (trouble != null) {
            int status = trouble.getSave_state();
            String originTime = trouble.getFindTime();

            viewHolder.reportedAddr.setText(trouble.getAddress());

            String troubleName = "";
            if (troubleName != null && !troubleName.isEmpty())
                viewHolder.reportedUser.setText(troubleName);

            if (status == -1) {
                UserBean user = BaseApp.getInstance().getLoginUser();
                String faceurl = NetRequestURL.getHttpHead() + user.getAvatar();
                DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
                        .showImageOnFail(R.mipmap.ico_user_name).build();
                imageLoader.displayImage(faceurl, viewHolder.imageUserIcon, imageOptions);
            }

            if (status == -1) {
                viewHolder.imageviewStatus.setVisibility(View.VISIBLE);
            } else {
                viewHolder.imageviewStatus.setVisibility(View.INVISIBLE);
            }

            String processName = "";
            if (status != -1 && !processName.isEmpty()){
                viewHolder.reportedClearState.setVisibility(View.VISIBLE);
                viewHolder.reportedClearState.setText(processName);
            }else {
                viewHolder.reportedClearState.setVisibility(View.GONE);
            }

            if (originTime != null && !originTime.isEmpty()) {
                String formatTime[] = new String[2];
                DateUtils.formatTime(originTime, formatTime);
                viewHolder.reportedDate.setText(formatTime[0]);
                viewHolder.reportedTime.setText(formatTime[1]);
            } else {
                viewHolder.reportedDate.setVisibility(View.GONE);
                viewHolder.reportedTime.setVisibility(View.GONE);
            }

            if (onItemClickListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onItemClickListener.onItemClick(viewHolder.itemView, viewHolder.getLayoutPosition());
                    }
                });
            }
        }
    }

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

    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imageview_status)
        ImageView imageviewStatus;
        @BindView(R.id.imageview_user_icon)
        CircleImageView imageUserIcon;
        @BindView(R.id.reported_user)
        TextView reportedUser;
        @BindView(R.id.text_clear_state)
        TextView reportedClearState;
        @BindView(R.id.reported_date)
        TextView reportedDate;
        @BindView(R.id.reported_time)
        TextView reportedTime;
        @BindView(R.id.reported_addr)
        TextView reportedAddr;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
