package me.koba1.led.model.colors;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import me.koba1.led.utils.serializer.KobaGradientSerializer;
import tools.jackson.databind.annotation.JsonSerialize;

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

@Getter
@AllArgsConstructor
@ToString
public class AnimationColor {
    public static enum ColorType {
        SOLID,
        GRADIENT;

        @JsonValue
        public String getValue() {
            return this.name().toLowerCase();
        }
    }

    private ColorType type;
    private KobaColor color;

    @JsonProperty("gradient")
    @JsonSerialize(using = KobaGradientSerializer.class)
    private KobaGradient gradient;

    public AnimationColor() {
        this.type = ColorType.SOLID;
        this.color = new KobaColor();
        this.gradient = new KobaGradient();
    }

    public AnimationColor type(ColorType type) {
        this.type = type;
        return this;
    }

    public KobaColor getColor(int ledIndex, int ledCount) {
        return switch (this.type) {
            case SOLID -> this.color;
            case GRADIENT -> this.gradient.getColor(ledIndex, ledCount);
        };
    }

    public List<KobaColor> getColor(int ledCount) {
        return switch (this.type) {
            case SOLID -> {
                List<KobaColor> colors = new ArrayList<>();
                for (int i = 0; i < ledCount; i++) {
                    colors.add(this.color);
                }
                yield colors;
            }
            case GRADIENT -> {
                List<KobaColor> colors = new ArrayList<>();
                for (int i = 0; i < ledCount; i++) {
                    colors.add(this.gradient.getColor(i, ledCount));
                }
                yield colors;
            }
        };
    }
}
