package com.ghost.technic.server.counter;

import lombok.Data;

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

@Data
public class Counters {

    private Set<Counter> counters = new HashSet<>();

    public int getDmg() {
        return counters.stream()
                .filter(counter -> counter.getCounterType().equals(CounterType.DAMAGE))
                .findFirst()
                .map(Counter::getNumberOfCounter)
                .orElse(0);
    }

    public void addDmg(int dmg) {
        var dmgToken = getToken(CounterType.DAMAGE);
        if (dmgToken != null) {
            var newDmg = dmgToken.getNumberOfCounter() + dmg;
            dmgToken.setNumberOfCounter(newDmg);
            counters.add(dmgToken);
        } else {
            var dmgTokens = Counter.builder()
                    .counterType(CounterType.DAMAGE)
                    .numberOfCounter(dmg)
                    .build();
            counters.add(dmgTokens);
        }
    }

    public int getAmber() {
        return counters.stream()
                .filter(counter -> counter.getCounterType().equals(CounterType.ÆMBER))
                .findFirst()
                .map(Counter::getNumberOfCounter)
                .orElse(0);
    }

    public void addAmber(int amber) {
        var ambers = getToken(CounterType.ÆMBER);
        if (ambers != null) {
            var newAmbers = ambers.getNumberOfCounter() + amber;
            ambers.setNumberOfCounter(newAmbers);
            counters.add(ambers);
        } else {
            var amberTokens = Counter.builder()
                    .counterType(CounterType.ÆMBER)
                    .numberOfCounter(amber)
                    .build();
            counters.add(amberTokens);
        }
    }

    public void addStun() {
        var stun = getToken(CounterType.STUN);
        if (stun == null) {
            counters.add(Counter.builder().counterType(CounterType.STUN).build());
        }
    }

    private Counter getToken(CounterType counterType) {
        return counters.stream()
                .filter(counter -> counter.getCounterType().equals(counterType))
                .findFirst()
                .orElse(null);
    }
}
