package p.studio.projectstate;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

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

@JsonIgnoreProperties(ignoreUnknown = true)
public record ProjectLocalStudioState(
        int version,
        ShellLayout shellLayout,
        OpenShellState openShellState) {
    public static final int CURRENT_VERSION = 1;

    public ProjectLocalStudioState {
        version = version <= 0 ? CURRENT_VERSION : version;
        shellLayout = shellLayout == null ? ShellLayout.defaults() : shellLayout.normalize();
        openShellState = openShellState == null ? OpenShellState.defaults() : openShellState.normalize();
    }

    public static ProjectLocalStudioState defaults() {
        return new ProjectLocalStudioState(
                CURRENT_VERSION,
                ShellLayout.defaults(),
                OpenShellState.defaults());
    }

    public ProjectLocalStudioState withShellLayout(final ShellLayout nextShellLayout) {
        return new ProjectLocalStudioState(version, nextShellLayout, openShellState);
    }

    public ProjectLocalStudioState withOpenShellState(final OpenShellState nextOpenShellState) {
        return new ProjectLocalStudioState(version, shellLayout, nextOpenShellState);
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public record ShellLayout(AssetsLayoutState assetsLayout) {
        public ShellLayout {
            assetsLayout = assetsLayout == null ? AssetsLayoutState.defaults() : assetsLayout.normalize();
        }

        public static ShellLayout defaults() {
            return new ShellLayout(AssetsLayoutState.defaults());
        }

        private ShellLayout normalize() {
            return new ShellLayout(assetsLayout);
        }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public record AssetsLayoutState(List<Double> contentSplitDividers) {
        public AssetsLayoutState {
            contentSplitDividers = normalizeDividerPositions(contentSplitDividers);
        }

        public static AssetsLayoutState defaults() {
            return new AssetsLayoutState(List.of());
        }

        private AssetsLayoutState normalize() {
            return new AssetsLayoutState(contentSplitDividers);
        }
    }

    @JsonIgnoreProperties(ignoreUnknown = true)
    public record OpenShellState(String selectedWorkspaceId) {
        public OpenShellState {
            selectedWorkspaceId = normalizeText(selectedWorkspaceId);
            if ("EDITOR".equals(selectedWorkspaceId)) {
                selectedWorkspaceId = "ASSETS";
            }
        }

        public static OpenShellState defaults() {
            return new OpenShellState(null);
        }

        private OpenShellState normalize() {
            return new OpenShellState(selectedWorkspaceId);
        }
    }

    private static String normalizeText(final String value) {
        if (value == null) {
            return null;
        }
        final String trimmed = value.trim();
        return trimmed.isEmpty() ? null : trimmed;
    }

    private static List<Double> normalizeDividerPositions(final List<Double> values) {
        if (values == null || values.isEmpty()) {
            return List.of();
        }
        final List<Double> normalized = new ArrayList<>();
        for (final Double value : values) {
            if (value == null || value.isNaN() || value.isInfinite()) {
                continue;
            }
            normalized.add(Math.max(0.0d, Math.min(1.0d, value)));
        }
        return List.copyOf(normalized);
    }
}
