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.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.mgq.base.utils.customview.CircleImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.common.ui.view.MySwipeHorizontalMenuLayout;
import com.tofly.ghrq_route.module.message.domain.OutMessageBean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class MessageSendAdapter extends RecyclerView.Adapter<MessageSendAdapter.MyViewHolder> {
    private Context mContext;
    private List<OutMessageBean> mData;
    private OnItemClickListener mItemClickListener;
    private OnItemOpenListener mItemOpenListener;
    private OnItemRemoveListener mItemRemoveListener;
    private Set<Integer> selectedPostions = new HashSet<>();
    private boolean isChoiceMode = false;
    private boolean mbSwipeEnabled = true;
    private ImageLoader imageLoader;

    public MessageSendAdapter(Context context, List<OutMessageBean> data) {
        mContext = context;
        mData = data;
        imageLoader = ImageLoader.getInstance();
    }

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

    public List<OutMessageBean> getData() {
        return mData;
    }

    public Set<Integer> getSelectedPositionSet() {
        return selectedPostions;
    }

    public OutMessageBean getItem(int postion) {
        OutMessageBean message = null;
        if (postion < mData.size()) {
            message = mData.get(postion);
        }
        return message;
    }

    public void allSelected() {
        selectedPostions.clear();
        for (int i = 0; i < mData.size(); i++)
            selectedPostions.add(i);
        this.notifyDataSetChanged();
    }

    public void cancelSelected() {
        isChoiceMode = false;
        selectedPostions.clear();
        mbSwipeEnabled = true;
        this.notifyDataSetChanged();
    }

    public void enableAllSwipable(boolean enable) {
        mbSwipeEnabled = enable;
        this.notifyDataSetChanged();
    }

    public void remove(int position) {
        if (position < mData.size()) {
            mData.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void removeSelectedItems(List<Integer> selectedPos) {
        for (int postion : selectedPos) {
            remove(postion);
        }
    }

    public void setReaded(int position) {
        if (position < mData.size()) {
            OutMessageBean messageBean = mData.get(position);
            messageBean.setIsread(1);
            notifyItemChanged(position);
        }
    }

    public void setSelectedReaded(List<Integer> selectedPos) {
        for (int postion : selectedPos) {
            setReaded(postion);
        }
    }

    public int getUnreadCount() {
        int count = 0;
        if (mData.size() > 0) {
            for (OutMessageBean message : mData) {
                if (message.getIsread() == 0)
                    count++;
            }
        }
        return count;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        boolean onItemLongClick(View view, int position);
    }

    public void setItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    public interface OnItemOpenListener {
        void onItemOpened(OutMessageBean message, int position);
    }

    public void setItemOpenListener(OnItemOpenListener listener) {
        mItemOpenListener = listener;
    }

    public interface OnItemRemoveListener {
        void onItemRemoved(OutMessageBean message, int position);
    }

    public void setItemRemoveListener(OnItemRemoveListener listener) {
        mItemRemoveListener = listener;
    }

    public void toggleSelection(int position) {
        if (selectedPostions.contains(position)) {
            selectedPostions.remove(position);
        } else {
            selectedPostions.add(position);
        }
        notifyItemChanged(position);
    }

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

    @Override
    public void onBindViewHolder(final MyViewHolder viewHolder, int position) {
        OutMessageBean message = mData.get(position);
        if (message != null) {
            viewHolder.textviewMessageUser.setText(message.getCreateUserName());
            viewHolder.textviewMessageDate.setText(message.getCreateDate());
            viewHolder.textviewMessageDesc.setText(message.getMessage());

            if (selectedPostions.contains(position)) {
                viewHolder.imageviewMessageIcon.setImageResource(R.drawable.ic_check_circle_pink_400_36dp);
            } else {
//                String faceurl = NetRequestURL.getHttpHead() + message.getAvatar();
//                DisplayImageOptions imageOptions = new DisplayImageOptions.Builder()
//                        .showImageOnFail(R.mipmap.ico_user_name).build();
//                imageLoader.displayImage(faceurl, viewHolder.imageviewMessageIcon, imageOptions);
            }

            if (message.getIsread() == 0) {
                viewHolder.imageViewNewMessage.setVisibility(View.GONE);
                viewHolder.imageViewOpenMail.setEnabled(false);
                viewHolder.imageViewOpenMail.setVisibility(View.GONE);
            } else {
                viewHolder.imageViewNewMessage.setVisibility(View.GONE);
                viewHolder.imageViewOpenMail.setEnabled(false);
                viewHolder.imageViewOpenMail.setVisibility(View.GONE);
            }

            viewHolder.textviewMessageUser.setTextColor(mContext.getResources().getColor(R.color.gray));

            viewHolder.linearLayoutRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPostion = viewHolder.getAdapterPosition();
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(v, adapterPostion);
                    }
                }
            });

            viewHolder.linearLayoutRoot.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int adapterPostion = viewHolder.getAdapterPosition();
                    if (mItemClickListener != null) {
                        return mItemClickListener.onItemLongClick(v, adapterPostion);
                    } else {
                        return false;
                    }
                }
            });

            viewHolder.imageViewDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPostion = viewHolder.getAdapterPosition();
                    OutMessageBean seletedMessage = mData.get(adapterPostion);
                    viewHolder.swipeMenu.smoothCloseMenu();
                    if (mItemRemoveListener != null) {
                        mItemRemoveListener.onItemRemoved(seletedMessage, adapterPostion);
                    }
                }
            });

            viewHolder.imageViewOpenMail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPostion = viewHolder.getAdapterPosition();
                    OutMessageBean seletedMessage = mData.get(adapterPostion);
                    viewHolder.swipeMenu.smoothCloseMenu();
                    if (mItemOpenListener != null) {
                        mItemOpenListener.onItemOpened(seletedMessage, adapterPostion);
                    }
                }
            });
        }
        if (mbSwipeEnabled) {
            viewHolder.swipeMenu.setSwipeEnable(true);
        } else {
            viewHolder.swipeMenu.setSwipeEnable(false);
        }
    }

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

    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.imageview_message_icon)
        CircleImageView imageviewMessageIcon;
        @BindView(R.id.textview_message_user)
        TextView textviewMessageUser;
        @BindView(R.id.textview_message_date)
        TextView textviewMessageDate;
        @BindView(R.id.imageview_new_message)
        ImageView imageViewNewMessage;
        @BindView(R.id.textview_message_desc)
        TextView textviewMessageDesc;
        @BindView(R.id.imageview_delete)
        ImageView imageViewDelete;
        @BindView(R.id.imageview_open_mail)
        ImageView imageViewOpenMail;
        @BindView(R.id.smContentView)
        LinearLayout linearLayoutRoot;
        @BindView(R.id.swipe_menu_layout)
        MySwipeHorizontalMenuLayout swipeMenu;

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