package ai.pai.client.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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 android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;

import ai.pai.client.R;
import ai.pai.client.beans.GroupMemberInfo;
import ai.pai.client.db.Preferences;
import ai.pai.client.services.GroupService;
import ai.pai.client.utils.PhotoLoader;
import ai.pai.client.views.DeleteConfirmPopup;

public class GroupMemberAdapter extends RecyclerView.Adapter<GroupMemberAdapter.MemberViewHolder> implements View.OnClickListener{

    private Context context;
    private LayoutInflater mInflater;
    private DisplayImageOptions options;
    private boolean isAdmin;
    private String groupId;
    private ArrayList<GroupMemberInfo> memberList;
    private String adminId = "";
    private GroupMemberInfo selectedMemberInfo;

    public GroupMemberAdapter(Context context,boolean isAdmin, String groupId,ArrayList<GroupMemberInfo> memberList){
        this.context = context;
        this.isAdmin = isAdmin;
        this.groupId = groupId;
        this.memberList = memberList;

        mInflater = LayoutInflater.from(context);
        options = PhotoLoader.getUserAvatarOptions();
        if(isAdmin){
            adminId = Preferences.getInstance(context).getUserId();
        }
    }

    @Override
    public MemberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.group_member_item,parent,false);
        MemberViewHolder viewHolder = new MemberViewHolder(view);
        viewHolder.avatar = (ImageView)view.findViewById(R.id.iv_member_avatar);
        viewHolder.name = (TextView)view.findViewById(R.id.tv_member_name);
        viewHolder.deleteBtn = view.findViewById(R.id.iv_member_delete);
        viewHolder.adminFlag = (TextView) view.findViewById(R.id.tv_member_admin_flag);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MemberViewHolder holder, final int position) {
        final GroupMemberInfo info = memberList.get(position);
        holder.deleteBtn.setVisibility(isAdmin ? View.VISIBLE : View.GONE);
        holder.adminFlag.setVisibility(info.isAdmin ? View.VISIBLE : View.GONE);
        holder.name.setText(info.nickName);
        PhotoLoader.getInstance(context).displayImage(info.avatarUrl,holder.avatar,options);

        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(adminId.equals(info.userId)){
                    Toast.makeText(context,R.string.can_not_delete_self,Toast.LENGTH_SHORT).show();
                    return;
                }
                selectedMemberInfo = info;
                new DeleteConfirmPopup((Activity)context,GroupMemberAdapter.this).showPopupWindow();
            }
        });
    }

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

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btn_delete_confirm){
            Intent intent = new Intent(context, GroupService.class);
            Bundle bundle = new Bundle();
            bundle.putInt("command", GroupService.GroupCommand.COMMAND_DELETE_GROUP_MEMBER);
            bundle.putString("group_id",groupId);
            bundle.putString("user_id",selectedMemberInfo.userId);
            bundle.putString("admin_id",adminId);
            intent.putExtras(bundle);
            context.startService(intent);
            Toast.makeText(context,R.string.delete_member_processing,Toast.LENGTH_SHORT).show();
            int position = memberList.indexOf(selectedMemberInfo);
            memberList.remove(position);
            notifyItemRemoved(position);
        }
    }

    static class MemberViewHolder extends RecyclerView.ViewHolder{

        private ImageView avatar;
        private TextView adminFlag;
        private TextView name;
        private View  deleteBtn;

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