package com.max.soundboard;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.wnafee.vector.MorphButton;
import com.wnafee.vector.compat.AnimatedVectorDrawable;
import com.wnafee.vector.compat.ResourcesCompat;


class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private final Context mContext;
    private final Group mSoundGroup;
    private final RecyclerView mRecyclerView;
    private final boolean mIsFavoritesAdapter;

    public RecyclerViewAdapter(Group soundGroup, RecyclerView recyclerView) {
        mContext = recyclerView.getContext();
        mSoundGroup = soundGroup;
        mRecyclerView = recyclerView;
        mIsFavoritesAdapter = mSoundGroup.getName().equals(Favorites.NAME);
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View view = layoutInflater.inflate(R.layout.card_view_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        setupPlayPauseView(viewHolder);
        return viewHolder;
    }

    private void setupPlayPauseView(ViewHolder viewHolder) {
        viewHolder.mPlayPauseView.setStartDrawable(AnimatedVectorDrawable.getDrawable(mContext,
                R.drawable.ic_play_to_pause));
        viewHolder.mPlayPauseView.setEndDrawable(AnimatedVectorDrawable.getDrawable(mContext,
                R.drawable.ic_pause_to_play));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        final Sound sound = mSoundGroup.getSound(position);
        final String soundName = sound.getName();

        // PlayPauseView must match the current state of the sound
        updatePlayPauseView(holder, position);

        // Sound name and group name text view
        updateSoundNameTextView(holder, sound, soundName);

        // Favoring button
        updateFavoriteButton(holder, sound, soundName);

        // The card view recognizes clicks to play the sound
        holder.mCardView.setOnClickListener(new PlayButtonListener(sound));
    }

    private void updatePlayPauseView(ViewHolder holder, int position) {
        if ((holder.mPlayPauseView.getState() == MorphButton.MorphState.END)
                && !mSoundGroup.getSound(position).isPlaying()) {
            holder.mPlayPauseView.setState(MorphButton.MorphState.START, false);
        }
        if ((holder.mPlayPauseView.getState() == MorphButton.MorphState.START)
                && mSoundGroup.getSound(position).isPlaying()) {
            holder.mPlayPauseView.setState(MorphButton.MorphState.END, false);
        }
    }

    private void updateSoundNameTextView(ViewHolder holder, Sound sound, String soundName) {
        holder.mSoundNameTextView.setText(soundName.substring(0, soundName.length() - 4));
        if (mIsFavoritesAdapter) {
            // Get the group name of the sounds in the favorites
            holder.mGroupNameTextView.setText(sound.getGroup().getName());

            // Add bottom padding so the group name has its own space
            final float scale = mContext.getResources().getDisplayMetrics().density;
            holder.mSoundNameTextView.setPadding(0, 0, 0, (int) (20 * scale + 0.5f));
        } else {
            // Remove bottom padding if group name is not present
            holder.mGroupNameTextView.setVisibility(View.GONE);
            holder.mSoundNameTextView.setPadding(0, 0, 0, 0);
        }
    }

    private void updateFavoriteButton(ViewHolder holder, Sound sound, String soundName) {
        holder.mFavoringButton.setOnClickListener(new FavoriteButtonListener(sound, mRecyclerView,
                mIsFavoritesAdapter));
        holder.mFavoringButton.setContentDescription(soundName);
        if (SoundManager.getFavorites().contains(sound)) {
            holder.mFavoringButton.setImageDrawable(ResourcesCompat.getDrawable(mContext,
                    R.drawable.ic_star));
        } else {
            holder.mFavoringButton.setImageDrawable(ResourcesCompat.getDrawable(mContext,
                    R.drawable.ic_star_outline));
        }
    }

    @Override
    public int getItemCount() {
        return mSoundGroup.getSize();
    }

    public void updateItem(Sound sound) {
        notifyItemChanged(mSoundGroup.indexOf(sound));
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final MorphButton mPlayPauseView;
        public final TextView mSoundNameTextView;
        public final TextView mGroupNameTextView;
        public final ImageButton mFavoringButton;
        public final CardView mCardView;

        public ViewHolder(View v) {
            super(v);
            mPlayPauseView = (MorphButton) v.findViewById(R.id.play_pause_view);
            mSoundNameTextView = (TextView) v.findViewById(R.id.sound_name_text_view);
            mGroupNameTextView = (TextView) v.findViewById(R.id.group_name_text_view);
            mFavoringButton = (ImageButton) v.findViewById(R.id.favoring_button);
            mCardView = (CardView) v.findViewById(R.id.card_view);
        }
    }
}

@@ -1874,58 +1874,8 @@
 on);
-%0A        final String soundName = sound.getName();
 %0A%0A  
@@ -1975,24 +1975,21 @@
 holder, 
-position
+sound
 );%0A%0A    
@@ -2072,35 +2072,24 @@
 older, sound
-, soundName
 );%0A%0A        
@@ -2149,27 +2149,16 @@
 r, sound
-, soundName
 );%0A%0A    
@@ -2349,36 +2349,35 @@
 der holder, 
-int position
+Sound sound
 ) %7B%0A        
@@ -2371,33 +2371,32 @@
 d) %7B%0A        if 
-(
 (holder.mPlayPau
@@ -2443,63 +2443,22 @@
 ate.
-END)%0A                && !mSoundGroup.getSound(position)
+START && sound
 .isP
@@ -2458,34 +2458,32 @@
 und.isPlaying())
- %7B
 %0A            hol
@@ -2533,21 +2533,19 @@
 phState.
-START
+END
 , false)
@@ -2558,153 +2558,12 @@
     
-%7D%0A        if ((holder.mPlayPauseView.getState() == MorphButton.MorphState.START)%0A                && mSoundGroup.getSound(position).isPlaying()) %7B
+else
 %0A   
@@ -2617,35 +2617,37 @@
 tton.MorphState.
-END
+START
 , false);%0A      
@@ -2640,26 +2640,16 @@
 false);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -2718,37 +2718,112 @@
 nd sound
-, String soundName) %7B
+) %7B%0A        final String soundName = sound.getName().substring(0, sound.getName().length() - 4);
 %0A       
@@ -2870,45 +2870,8 @@
 Name
-.substring(0, soundName.length() - 4)
 );%0A 
@@ -3584,26 +3584,8 @@
 ound
-, String soundName
 ) %7B%0A
@@ -3776,28 +3776,34 @@
 iption(sound
+.get
 Name
+()
 );%0A        i

Refactor RecyclerViewAdapter