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

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.mgq.base.utils.customview.CircleImageView;
import com.mgq.base.utils.image.BitmapUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
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.module.incident.domain.DeviceUploadBean;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lihualong on 2017/11/6.
 */

public class MonitorAdapter extends ARecyclerBaseAdapter<MonitorAdapter.MyViewHolder, DeviceUploadBean> {

    private List<DeviceUploadBean> mData;

    private Context context;

    private Drawable onlineDrawable;

    private Drawable offlineDrawable;


    public MonitorAdapter(Context context, List<DeviceUploadBean> data) {
        super(context, data);
        this.context = context;
        this.mData = data;

        onlineDrawable = mContext.getResources().getDrawable(R.mipmap.ic_peopel_head);
        offlineDrawable = mContext.getResources().getDrawable(R.mipmap.ic_people_head_offline);
    }

    public void setData(List<DeviceUploadBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void addAll(List<DeviceUploadBean> data) {
        int startIndex = mData.size();
        mData.addAll(startIndex, data);
        notifyItemRangeInserted(startIndex, data.size());
    }

    public void clearAll() {
        mData.clear();
        notifyDataSetChanged();
    }

    public DeviceUploadBean getItem(int position) {
        if (position < mData.size())
            return mData.get(position);
        else return null;
    }

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

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_monitor, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final DeviceUploadBean deviceUploadBean = mData.get(position);
        int sign_state = deviceUploadBean.get_sign_state();
        final boolean isSignUp = sign_state >= 1;
        String userAvatar = deviceUploadBean.getAvatar();
        if (userAvatar != null && !userAvatar.isEmpty()) {
            //加载头像
            String faceurl = NetRequestURL.newApi_dyrq.getFormattedPicture(userAvatar);
            ImageLoader imageLoader = ImageLoader.getInstance();
            DisplayImageOptions displayOptions = BaseApp.getDefaultDisplayOptions();
            imageLoader.displayImage(faceurl, holder.itemIncidentHead,  displayOptions, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String s, View view) {

                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {
                    holder.itemIncidentHead.setImageDrawable(isSignUp ? onlineDrawable : offlineDrawable);
                }

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                    if (isSignUp){
                        holder.itemIncidentHead.setImageBitmap(bitmap);
                    }else {
                        Bitmap grayBitmap = BitmapUtils.saturation(bitmap, 0);
                        if (grayBitmap != null)
                            holder.itemIncidentHead.setImageBitmap(grayBitmap);
                    }
                }

                @Override
                public void onLoadingCancelled(String s, View view) {

                }
            });
        } else {
            holder.itemIncidentHead.setImageDrawable(isSignUp ? onlineDrawable : offlineDrawable);
        }

        holder.itemIncidentName.setText(deviceUploadBean.getRealname());
        int colorRes = isSignUp ? R.color.blue4 : sign_state == -1 || sign_state == 0 ? R.color.callout_text_color : R.color.red;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            holder.itemIncidentName.setTextColor(context.getResources().getColor(colorRes, null));
        } else {
            holder.itemIncidentName.setTextColor(context.getResources().getColor(colorRes));
        }
        if (sign_state == 3 || sign_state == 4) {
            holder.itemIncidentName.setTextColor(context.getResources().getColor(R.color.top_bar_light));
        }

        holder.itemView.setOnClickListener(v -> {
            if (onItemClickListener != null){
                onItemClickListener.onItemClick(holder.itemView, holder.getAdapterPosition());
            }
        });
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_incident_name)
        TextView itemIncidentName;
        @BindView(R.id.item_incident_head)
        CircleImageView itemIncidentHead;

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