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.TextView;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.activity.WebViewActivity;
import ai.pai.client.beans.SystemMessageBean;

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

    private Context context;
    private LayoutInflater mInflater;
    private ArrayList<SystemMessageBean> messageList;
    private MessageActionListener listener;

    public SystemMessageAdapter(Context context, ArrayList<SystemMessageBean> messageList){

        this.context = context;
        this.messageList = messageList;
        mInflater = LayoutInflater.from(context);
    }

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


    @Override
    public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.system_message_item,parent,false);
        MessageViewHolder viewHolder = new MessageViewHolder(view);
        viewHolder.title = (TextView)view.findViewById(R.id.tv_message_title);
        viewHolder.content = (TextView)view.findViewById(R.id.tv_message_content);
        viewHolder.delete = (TextView)view.findViewById(R.id.tv_trash);
        viewHolder.jump = view.findViewById(R.id.layout_message_jump);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MessageViewHolder holder, final int position) {
        final SystemMessageBean messageBean = messageList.get(position);
        holder.content.setText(messageBean.content);
        holder.title.setText(messageBean.title);
        holder.jump.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context,WebViewActivity.class);
                intent.putExtra("url",messageBean.url);
                intent.putExtra("title",messageBean.title);
                context.startActivity(intent);
            }
        });

        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);
                }
            }
        });
    }

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

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

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

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

    static class MessageViewHolder extends RecyclerView.ViewHolder{

        private TextView title;
        private TextView content;
        private TextView delete;
        private View   jump;

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

    public interface MessageActionListener {

        void onMessageDeleted(String messageId);

    }
}
