package de.beyondsoft.ownchat.page.chatmembers;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
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 com.bumptech.glide.load.engine.DiskCacheStrategy;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.model.User;
import de.beyondsoft.ownchat.utils.ui.GlideApp;
import de.hdodenhof.circleimageview.CircleImageView;

class ChatMembersAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<User> mUsers;

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

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull 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(@NonNull 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;
        }

        GlideApp.with(viewHolder.avatarCircleImageView.getContext())
                .asBitmap()
                .load(user.avatar)
                .placeholder(R.drawable.avatar)
                .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);
        }
    }
}
