package ai.pai.client.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.common.utils.TimeUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.activity.PhotoDetailsActivity;
import ai.pai.client.beans.MessageBean;
import ai.pai.client.utils.PhotoLoader;

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {

    private Context context;
    private LayoutInflater mInflater;
    private DisplayImageOptions photoOptions;
    private DisplayImageOptions userAvatarOptions;
    private ArrayList<MessageBean> messageList;
    private MessageActionListener listener;

    public MessageAdapter(Context context, ArrayList<MessageBean> messageList){

        this.context = context;
        this.messageList = messageList;
        mInflater = LayoutInflater.from(context);
        photoOptions = PhotoLoader.getPhotoOptions();
        userAvatarOptions = PhotoLoader.getUserAvatarOptions();

    }

    public void addMessageDeleteListener(MessageActionListener listener){
        this.listener = listener;
    }


    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.message_item,parent,false);
        MessageViewHolder viewHolder = new MessageViewHolder(view);
        viewHolder.photo = (ImageView)view.findViewById(R.id.iv_message_photo);
        viewHolder.avatar = (ImageView)view.findViewById(R.id.iv_message_avatar);
        viewHolder.name = (TextView)view.findViewById(R.id.tv_message_from);
        viewHolder.content = (TextView)view.findViewById(R.id.tv_message_content);
        viewHolder.time = (TextView)view.findViewById(R.id.tv_message_time);
        viewHolder.delete = (TextView)view.findViewById(R.id.tv_trash);
        viewHolder.itemView = view.findViewById(R.id.layout_message_item);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MessageViewHolder holder, final int position) {
        final MessageBean messageBean = messageList.get(position);
        holder.content.setText(messageBean.content);
        holder.time.setText(TimeUtils.getFormattedTime(messageBean.createTime));
        holder.name.setText(messageBean.senderName);
        PhotoLoader.getInstance(context).displayImage(messageBean.photoItem.thumbnailUrl,holder.photo,photoOptions);
        PhotoLoader.getInstance(context).displayImage(messageBean.senderAvatarUrl,holder.avatar,userAvatarOptions);
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                messageList.remove(holder.getAdapterPosition());
                notifyItemRemoved(holder.getAdapterPosition());
                if(listener!=null){
                    listener.onMessageDeleted(messageBean.msgId);
                }
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PhotoDetailsActivity.class);
                intent.putExtra("photo_item",messageBean.photoItem);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return messageList==null ? 0 : messageList.size();
    }

    public synchronized void clearMessageList(){
        messageList.clear();
        notifyDataSetChanged();
    }

    public synchronized void addMessageList(ArrayList<MessageBean> messageList){
        this.messageList.addAll(messageList);
        notifyDataSetChanged();
    }

    public ArrayList<MessageBean> getMessageList(){
        return messageList;
    }

    static class MessageViewHolder extends RecyclerView.ViewHolder{

        private ImageView photo;
        private ImageView avatar;
        private TextView name;
        private TextView time;
        private TextView content;
        private TextView delete;
        private View itemView;

        public MessageViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface MessageActionListener {

        void onMessageDeleted(String messageId);

    }
}
