package de.beyondsoft.ownchat.page.sharing;

import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
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.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.model.Group;
import de.beyondsoft.ownchat.utils.ui.GlideApp;

public class SharingContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int GROUP_TYPE = 0;
    private static final int CONTACT_TYPE = 1;

    private ArrayList<Group> mGroups;
    private SharingContactsAdapter.OnOpenClickListener mItemClickListener;

    interface OnOpenClickListener {
        void onOpenGroupMessage(Group group);

        void onOpenPrivateMessage(Group contact);
    }

    SharingContactsAdapter(OnOpenClickListener openClickListener) {
        mItemClickListener = openClickListener;
        mGroups = new ArrayList<>();
    }

    void setGroups(ArrayList<Group> groups) {
        mGroups.clear();

        if (groups != null) {
            mGroups.addAll(groups);
        }

        notifyDataSetChanged();
    }

    @Override
    public final int getItemViewType(int position) {
        if (mGroups.get(position).users.size() > 2) {
            return GROUP_TYPE;
        }

        return CONTACT_TYPE;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Group group = mGroups.get(position);
        SharingContactsAdapter.SharingContactViewHolder viewHolder = (SharingContactsAdapter.SharingContactViewHolder) holder;

        int type = getItemViewType(position);

        if (type == GROUP_TYPE) {
            viewHolder.name.setText(group.name);
            viewHolder.name.setTextColor(ContextCompat.getColor(viewHolder.name.getContext(), group.isHelpGroup() ? R.color.colorHelpGroup : R.color.colorTeamGroup));
            viewHolder.avatar.setImageResource(group.isHelpGroup() ? R.drawable.avatar_help_group : R.drawable.avatar_team_group);

            return;
        } else if(!group.onlyEmployees) {
            viewHolder.avatar.setImageResource(R.drawable.avatar_help);
        }

        if(type == CONTACT_TYPE) {
            viewHolder.name.setTextColor(ContextCompat.getColor(viewHolder.name.getContext(), group.isEmployee(mGroups.get(position).users) ? R.color.colorBlack : R.color.colorHelpGroup));
        }

        viewHolder.name.setText(group.name);

        GlideApp.with(viewHolder.avatar.getContext())
                .asBitmap()
                .load(group.avatar)
                .placeholder(group.onlyEmployees ? R.drawable.avatar_list : R.drawable.avatar_help)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(viewHolder.avatar);
    }

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

    class SharingContactViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.sharing_contact_avatar)
        ImageView avatar;

        @BindView(R.id.sharing_contact_name)
        TextView name;

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

        @OnClick(R.id.sharing_contact_container)
        void onClickContact() {
            int type = getItemViewType();

            if (type == GROUP_TYPE) {
                mItemClickListener.onOpenGroupMessage(mGroups.get(getAdapterPosition()));
                return;
            }

            mItemClickListener.onOpenPrivateMessage(mGroups.get(getAdapterPosition()));
        }
    }
}
