package com.p3.bewoplaner.page.chatmembers;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.p3.bewoplaner.R;
import com.p3.bewoplaner.model.User;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by imarneanu on 2/24/17.
 */
class ChatMembersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<User> mUsers;

    ChatMembersAdapter(ArrayList<User> users) {
        mUsers = users;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chat_member, parent,
                false);
        return new ContactViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ContactViewHolder viewHolder = (ContactViewHolder) holder;
        User user = mUsers.get(position);
        viewHolder.contactTextView.setText(String.format(Locale.getDefault(), "%s %s", user.firstName, user.lastName));
        viewHolder.contactTextView.setTextColor(ContextCompat.getColor(viewHolder.contactTextView.getContext(),
                user.isEmployee ? R.color.colorBlack : R.color.colorHelpGroup));
        viewHolder.phoneImageView.setVisibility(TextUtils.isEmpty(user.mobile) ? View.GONE : View.VISIBLE);
        if (TextUtils.isEmpty(user.avatar)) {
            viewHolder.avatarCircleImageView.setImageResource(R.drawable.avatar_list);
            return;
        }
        Glide.with(viewHolder.avatarCircleImageView.getContext())
                .load(user.avatar)
                .placeholder(R.drawable.avatar)
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(viewHolder.avatarCircleImageView);
    }

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

    class ContactViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.chat_member_avatar)
        CircleImageView avatarCircleImageView;
        @BindView(R.id.chat_member_name)
        TextView contactTextView;
        @BindView(R.id.chat_member_phone)
        ImageView phoneImageView;

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

        @OnClick(R.id.chat_member_phone)
        void onPhoneClicked() {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel",
                    mUsers.get(getAdapterPosition()).mobile,
                    null));
            phoneImageView.getContext().startActivity(intent);
        }
    }
}
