package me.koba1.led.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.*;
import me.koba1.led.animations.BaseAnimation;
import me.koba1.led.utils.AnimationResolver;

import java.util.List;
import java.util.Optional;

@Setter
@Getter
// @AllArgsConstructor
@ToString
public class LedStrip {

    private int id;
    private String name;
    private int pin;
    private int ledCount;
    private boolean power;
    private int animation;
    private List<BaseAnimation> animations;

    @JsonIgnore
    // @Setter(AccessLevel.NONE) // Lombok nem fogja beletenni a constructorba sem
    private BaseAnimation selectedAnimation;

    public LedStrip() {
    }

    public LedStrip(int id, String name, int pin, int ledCount, boolean power, int animation, List<BaseAnimation> animations) {
        this.id = id;
        this.name = name;
        this.pin = pin;
        this.ledCount = ledCount;
        this.power = power;
        this.animation = animation;
        this.animations = animations;

        this.selectedAnimation = this.animations.stream().filter(a -> a.getId() == this.animation).findFirst().orElse(null);
    }

//    public <T extends BaseAnimation> Optional<T> getAnimation(Class<T> clazz) {
//        return animations.stream()
//                .filter(clazz::isInstance)
//                .map(clazz::cast) // cast-oljuk a T típusra
//                .findFirst();
//    }

//    public Optional<BaseAnimation> getAnimation(int id) {
//        return animations.stream().filter(a -> a.getId() == id).findFirst();
//    }
//
//    @JsonIgnore
//    public BaseAnimation getSelectedAnimation() {
//        return getAnimation(animation).orElse(null);
//    }

    @JsonSetter("animations")
    public void setAnimations(List<BaseAnimation> animations) {
        this.animations = animations;
//        updateSelectedAnimation();
    }

    @JsonSetter("animation")
    public void setAnimation(int animation) {
        this.animation = animation;
//        updateSelectedAnimation();
    }

    private BaseAnimation updateSelectedAnimation() {
        if (this.animations != null && !this.animations.isEmpty() && this.animation >= 0 && this.animations.stream().anyMatch(a -> a.getId() == this.animation)) {
           // this.selectedAnimation = this.animations.get(this.animation);
            return this.animations.get(this.animation);
        }

        return null;
    }
}
