package com.joshuaavalon.wsdeckeditor.sdk.deck;

import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Multiset;
import com.joshuaavalon.wsdeckeditor.sdk.card.Card;

import java.util.HashSet;
import java.util.Set;

public class Deck {
    public static final long NO_ID = -1;
    @NonNull
    private final Multiset<Card> cardList;
    @NonNull
    private String name;
    private long id;
    @Nullable
    private String cover;

    public Deck() {
        name = "";
        id = NO_ID;
        cardList = HashMultiset.create();
        cover = null;
    }

    @NonNull
    public String getName() {
        return name;
    }

    public void setName(@NonNull final String name) {
        this.name = name;
    }

    public long getId() {
        return id;
    }

    public void setId(final long id) {
        this.id = id;
    }

    @Nullable
    public String getCover() {
        return cover;
    }

    public void setCover(@Nullable final String cover) {
        this.cover = cover;
    }

    @NonNull
    public Multiset<Card> getCardList() {
        return ImmutableMultiset.copyOf(cardList);
    }

    public void setCardList(@NonNull final Multiset<Card> cardList) {
        this.cardList.clear();
        this.cardList.addAll(cardList);
    }

    @NonNull
    public Set<String> getExpansion() {
        final Set<String> expansions = new HashSet<>();
        for (Card card : cardList.elementSet())
            expansions.add(card.getExpansion());
        return expansions;
    }

    @NonNull
    public Multiset<Card.Color> getColor() {
        final Multiset<Card.Color> colors = HashMultiset.create();
        for (Card card : cardList.elementSet())
            colors.add(card.getColor());
        return colors;
    }

    @NonNull
    public Multiset<Card.Trigger> getTrigger() {
        final Multiset<Card.Trigger> triggers = HashMultiset.create();
        for (Card card : cardList.elementSet())
            triggers.add(card.getTrigger());
        return triggers;
    }

    @NonNull
    public Multiset<Card.Type> getType() {
        final Multiset<Card.Type> types = HashMultiset.create();
        for (Card card : cardList.elementSet())
            types.add(card.getType());
        return types;
    }

    @NonNull
    public Multiset<Integer> getLevel() {
        final Multiset<Integer> levels = HashMultiset.create();
        for (Card card : cardList.elementSet())
            levels.add(card.getLevel());
        return levels;
    }

    @NonNull
    public Multiset<Integer> getCost() {
        final Multiset<Integer> costs = HashMultiset.create();
        for (Card card : cardList.elementSet())
            costs.add(card.getCost());
        return costs;
    }

    public void setCardCount(@NonNull final Card card, @IntRange(from = 0) final int count) {
        cardList.setCount(card, count);
    }

    public void removeCard(@NonNull final Card card) {
        cardList.remove(card);
    }

    public void addCard(@NonNull final Card card) {
        cardList.add(card);
    }

    public void addIfNotExist(@NonNull final Card card) {
        if (!cardList.contains(card))
            addCard(card);
    }

    @NonNull
    public DeckMeta meta() {
        return new DeckMeta(id, name, cover);
    }
}

@@ -1844,33 +1844,50 @@
        for (
-Card card
+Multiset.Entry%3CCard%3E entry
  : cardList.
@@ -1879,38 +1879,36 @@
 try : cardList.e
-leme
 nt
+ry
 Set())%0A         
@@ -1925,21 +1925,53 @@
 add(
-card.getColor
+entry.getElement().getColor(), entry.getCount
 ());
@@ -2143,33 +2143,50 @@
        for (
-Card card
+Multiset.Entry%3CCard%3E entry
  : cardList.
@@ -2178,38 +2178,36 @@
 try : cardList.e
-leme
 nt
+ry
 Set())%0A         
@@ -2226,23 +2226,55 @@
 add(
-card.getTrigger
+entry.getElement().getTrigger(), entry.getCount
 ());
@@ -2436,33 +2436,50 @@
        for (
-Card card
+Multiset.Entry%3CCard%3E entry
  : cardList.
@@ -2471,38 +2471,36 @@
 try : cardList.e
-leme
 nt
+ry
 Set())%0A         
@@ -2516,20 +2516,52 @@
 add(
-card.getType
+entry.getElement().getType(), entry.getCount
 ());
@@ -2718,33 +2718,50 @@
        for (
-Card card
+Multiset.Entry%3CCard%3E entry
  : cardList.
@@ -2753,38 +2753,36 @@
 try : cardList.e
-leme
 nt
+ry
 Set())%0A         
@@ -2799,21 +2799,53 @@
 add(
-card.getLevel
+entry.getElement().getLevel(), entry.getCount
 ());
@@ -3001,33 +3001,50 @@
        for (
-Card card
+Multiset.Entry%3CCard%3E entry
  : cardList.
@@ -3036,38 +3036,36 @@
 try : cardList.e
-leme
 nt
+ry
 Set())%0A         
@@ -3081,19 +3081,51 @@
 add(
-card.getCos
+entry.getElement().getCost(), entry.getCoun
 t())

Fix incorrect card count