package com.nestedworld.nestedworld.ui.fight.battle;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.Glide;
import com.nestedworld.nestedworld.R;
import com.nestedworld.nestedworld.database.models.Monster;
import com.nestedworld.nestedworld.helpers.log.LogHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.orm.util.ContextUtil.getContext;


public class BattleMonsterAdapter extends RecyclerView.Adapter<BattleMonsterAdapter.BattleMonsterViewHolder> {
    private final List<BattleMonster> mMonsters = new ArrayList<>();

    /*
    ** Constructor
     */
    public BattleMonsterAdapter() {

    }

    /*
    ** Life cycle
     */
    @Override
    public BattleMonsterViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fight_battlemonster, parent, false);
        BattleMonsterViewHolder battleMonsterViewHolder = new BattleMonsterViewHolder(v);
        ButterKnife.bind(battleMonsterViewHolder, v);

        return battleMonsterViewHolder;
    }

    @Override
    public void onBindViewHolder(BattleMonsterViewHolder holder, int position) {
        //Get selectedMonster
        BattleMonster battleMonster = mMonsters.get(position);

        if (battleMonster != null) {
            populateMonsterInfo(holder, battleMonster);
        }
    }

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

    /*
    ** Public method
     */
    public void add(@Nullable final Monster monster, @NonNull final Status status) {
        mMonsters.add(new BattleMonster(monster, status));
    }

    public void replace(@Nullable final Monster monster, @NonNull final Status status) {
        for (BattleMonster battleMonster : mMonsters) {
            if (battleMonster.monster == monster) {
                battleMonster.monster = monster;
                battleMonster.status = status;

                notifyDataSetChanged();
            }
        }
    }

    /*
    ** Internal method
     */
    private void populateMonsterInfo(@NonNull final BattleMonsterViewHolder holder, @NonNull final BattleMonster battleMonster) {
        //Populate name & lvl
        LogHelper.d("populateMonsterInfo > ", "name=" + battleMonster.monster.name);
        holder.textViewMonsterName.setText(battleMonster.monster.name);

        //Display monster picture
        Glide.with(getContext())
                .load(battleMonster.monster.sprite)
                .placeholder(R.drawable.default_monster)
                .centerCrop()
                .into(holder.imageViewMonster);

        //Check if monster is alive
        switch (battleMonster.status) {
            case DEAD:
                holder.imageViewMonsterStatus.setImageResource(R.drawable.red_cross);
                break;
            case SELECTED:
                holder.imageViewMonsterStatus.setImageResource(R.drawable.green_selected);
                break;
            default:
                break;
        }
    }


    public enum Status {
        DEAD,
        SELECTED,
        DEFAULT
    }

    private static class BattleMonster {
        @Nullable
        public Monster monster;
        @NonNull
        public Status status;

        public BattleMonster(@Nullable final Monster monster, @NonNull final Status status) {
            this.monster = monster;
            this.status = status;
        }
    }

    public class BattleMonsterViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.textview_monster_name)
        TextView textViewMonsterName;
        @BindView(R.id.imageView_monster)
        ImageView imageViewMonster;
        @BindView(R.id.imageView_monster_status)
        ImageView imageViewMonsterStatus;

        public BattleMonsterViewHolder(View itemView) {
            super(itemView);
        }
    }
}

@@ -831,22 +831,16 @@
 al List%3C
-Battle
 Monster%3E
@@ -1556,30 +1556,24 @@
         
-Battle
 Monster 
 battleMo
@@ -1560,31 +1560,25 @@
     Monster 
-battleM
+m
 onster = mMo
@@ -1608,31 +1608,25 @@
         if (
-battleM
+m
 onster != nu
@@ -1671,23 +1671,17 @@
 holder, 
-battleM
+m
 onster);
@@ -1875,98 +1875,91 @@
 ster
-, @NonNull final Status status) %7B%0A        mMonsters.add(new BattleMonster
+) %7B%0A        mMonsters.add(monster);%0A        notifyItemInserted
 (m
+M
 onster
-, status)
+s.size() - 1
 );%0A 
@@ -1984,47 +1984,51 @@
 oid 
-replace(@Nullable
+addAll(@NonNull
  final 
+List%3C
 Monster
+%3E
  monster
 , @N
@@ -2027,95 +2027,47 @@
 ster
-, @NonNull final Status status) %7B%0A        for (BattleMonster battleMonster : mM
+s) %7B%0A        mMonsters.addAll(m
 onsters)
  %7B%0A 
@@ -2054,34 +2054,33 @@
 addAll(monsters)
- %7B
+;
 %0A            if 
@@ -2076,99 +2076,157 @@
     
-    if (battleMonster.monster ==
+notifyItemRangeChanged(mMonsters.size() - monsters.size(),
  m
+M
 onster
-) %7B
+s.size());
 %0A    
+%7D%0A%0A
     
-        battleMonster.monster
+public void clear() %7B%0A        int oldSize
  = m
+M
 onster
+s.size()
 ;%0A  
@@ -2235,111 +2235,70 @@
     
-        battleMonster.status = status;%0A%0A                notifyDataSetChanged();%0A            %7D%0A        %7D
+mMonsters.clear();%0A        notifyItemRangeRemoved(0, oldSize);
 %0A   
@@ -2438,30 +2438,24 @@
 l final 
-Battle
 Monster 
 battleMo
@@ -2446,23 +2446,17 @@
 Monster 
-battleM
+m
 onster) 
@@ -2461,123 +2461,8 @@
 ) %7B%0A
-        //Populate name & lvl%0A        LogHelper.d(%22populateMonsterInfo %3E %22, %22name=%22 + battleMonster.monster.name);%0A
     
@@ -2500,30 +2500,16 @@
 setText(
-battleMonster.
 monster.
@@ -2605,30 +2605,16 @@
   .load(
-battleMonster.
 monster.
@@ -2759,813 +2759,8 @@
 er);
-%0A%0A        //Check if monster is alive%0A        switch (battleMonster.status) %7B%0A            case DEAD:%0A                holder.imageViewMonsterStatus.setImageResource(R.drawable.red_cross);%0A                break;%0A            case SELECTED:%0A                holder.imageViewMonsterStatus.setImageResource(R.drawable.green_selected);%0A                break;%0A            default:%0A                break;%0A        %7D%0A    %7D%0A%0A%0A    public enum Status %7B%0A        DEAD,%0A        SELECTED,%0A        DEFAULT%0A    %7D%0A%0A    private static class BattleMonster %7B%0A        @Nullable%0A        public Monster monster;%0A        @NonNull%0A        public Status status;%0A%0A        public BattleMonster(@Nullable final Monster monster, @NonNull final Status status) %7B%0A            this.monster = monster;%0A            this.status = status;%0A        %7D
 %0A   

fix adapter design