package de.beyondsoft.ownchat.page.chatmembers;

import de.beyondsoft.ownchat.R;
import de.beyondsoft.ownchat.model.Group;
import de.beyondsoft.ownchat.utils.ui.HorizontalDividerItemDecoration;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChatMembersActivity extends AppCompatActivity {
    protected final static String GROUP_ARG = "group_arg";

    @BindView(R.id.chat_toolbar_image_view)
    ImageView mChatImageView;

    @BindView(R.id.chat_toolbar_group_title)
    TextView mGroupTitleTextView;

    @BindView(R.id.chat_toolbar_group_members)
    TextView mGroupMembersTextView;

    @BindView(R.id.chat_toolbar_extra_image)
    ImageView mExtraImageView;

    @BindView(R.id.chat_members_recycler_view)
    RecyclerView mChatMembersRecyclerView;

    @BindView(R.id.chatSearchView)
    SearchView mChatSearchView;

    private RecyclerView.ItemDecoration mHorizontalDividerItemDecoration;

    public static Intent createIntent(Context context, Group group) {
        Intent intent = new Intent(context, ChatMembersActivity.class);
        intent.putExtra(GROUP_ARG, group);
        return intent;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_members);
        ButterKnife.bind(this);
        Group group = getIntent().getParcelableExtra(GROUP_ARG);
        setToolbar(group);

        mHorizontalDividerItemDecoration = new HorizontalDividerItemDecoration.Builder(this).colorResId(R.color.colorFill).build();
        mChatMembersRecyclerView.addItemDecoration(mHorizontalDividerItemDecoration);
        mChatMembersRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        ChatMembersAdapter contactsAdapter = new ChatMembersAdapter(group.users);
        mChatMembersRecyclerView.setAdapter(contactsAdapter);

        mChatSearchView.setVisibility(View.GONE);
    }

    private void setToolbar(Group group) {
        mGroupTitleTextView.setVisibility(View.VISIBLE);
        mGroupMembersTextView.setVisibility(View.GONE);
        mExtraImageView.setVisibility(View.GONE);
        mChatImageView.setImageResource(group.isHelpGroup() ? R.drawable.avatar_help_group : R.drawable.avatar_team_group);
        mGroupTitleTextView.setText(group.name);
        mGroupTitleTextView.setTextColor(ContextCompat.getColor(this, group.isHelpGroup() ? R.color.colorHelpGroup : R.color.colorTeamGroup));
    }

    @OnClick(R.id.chat_toolbar_back_arrow)
    void backPressed() {
        onBackPressed();
    }

    @Override
    public void onDestroy() {
        mChatMembersRecyclerView.setAdapter(null);
        mChatMembersRecyclerView.removeItemDecoration(mHorizontalDividerItemDecoration);
        super.onDestroy();
    }
}
