package me.koba1.led.managers;

import lombok.Getter;
import me.koba1.led.Main;
import me.koba1.led.animations.BaseAnimation;
import me.koba1.led.device.packets.UpdateColorPacket;
import me.koba1.led.device.packets.UpdateLedStripsPacket;
import me.koba1.led.model.LedStrip;
import me.koba1.led.model.colors.KobaOutputColor;
import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Getter
public class LedStripManager {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    private final HashMap<Integer, LedStrip> ledStrips;

    public LedStripManager() {
        this.ledStrips = new HashMap<>();
    }

    public void load() {
        if (Main.getSavedDataFile().exists() && Main.getSavedDataFile().length() == 0) {
            return;
        }

        List<LedStrip> strips = MAPPER.readValue(Main.getSavedDataFile(), new TypeReference<List<LedStrip>>() {});
        Map<Integer, LedStrip> map = strips.stream().collect(Collectors.toMap(LedStrip::getId, s -> s));
        this.ledStrips.putAll(map);
    }

    public List<LedStrip> update(List<LedStrip> strips) {
        Map<Integer, LedStrip> map = strips.stream().collect(Collectors.toMap(LedStrip::getId, s -> s));

        this.ledStrips.clear();
        this.ledStrips.putAll(map);

        System.out.println("Új stripek: " + new ArrayList<>(ledStrips.keySet()));
        save();


        UpdateLedStripsPacket packet = new UpdateLedStripsPacket();
        Main.getInstance().getDeviceManager().sendPacket(packet);

        // Kikapcsoljuk a ledStripeket, hogy ne main loopban küldjük folyamatosan ujra.
        Map<Integer, KobaOutputColor> offColors = new HashMap<>();
        for (LedStrip value : map.values()) {
            if (value.isPower()) {
                offColors.put(value.getId(), KobaOutputColor.createBlack(value.getId()));
            }
        }

        Main.getInstance().getDeviceManager().sendPacket(new UpdateColorPacket(offColors));
        return new ArrayList<>(map.values());
    }

    public void save() {
        MAPPER.writeValue(Main.getSavedDataFile(), ledStrips.values());
    }

    public LedStrip createNew(String name) {
        return new LedStrip(0, name, 0, 50, false, 0, BaseAnimation.getDefaultAnimations());
    }

    public void setupDefaults() { // ledcount adriannál: 61
        this.ledStrips.put(0, new LedStrip(0, "J Bal ajtó", 0, 61, true, 103, BaseAnimation.getDefaultAnimations()));
        //this.ledStrips.put(1, new LedStrip(1, "J Jobb ajtó", 1, 50, false, 0, BaseAnimation.getDefaultAnimations()));
        //this.ledStrips.put(2, new LedStrip(2, "J Hátsó üléssor", 2, 75, false, 0, BaseAnimation.getDefaultAnimations()));

        // this.ledStrips.get(0).setAnimations(BaseAnimation.getDefaultAnimations());

        // System.out.println(new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(this.ledStrips.get(0)));
        save();


        UpdateLedStripsPacket packet = new UpdateLedStripsPacket();
        Main.getInstance().getDeviceManager().sendPacket(packet);
    }
}
