package de.accso.flexinale.backoffice.domain.model;

import de.accso.flexinale.common.shared_kernel.*;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Kino implements Identifiable, Versionable, EqualsByContent {
    public record Name(String raw) implements RawWrapper<String> {}
    public record Adresse(String raw) implements RawWrapper<String> {}
    public record EmailAdresse(String raw) implements RawWrapper<String> {}

    public final Id id;
    public final Version version;

    public Name name;
    public Adresse adresse;
    public EmailAdresse emailAdresse;
    @DoNotCheckInArchitectureTests
    public Set<KinoSaal> kinoSaele = new HashSet<>();

    public Kino(final Id id) {
        this(id, Versionable.unknownVersion());
    }

    public Kino(final Id id, final Version version) {
        this.id = id;
        this.version = version;
    }

    public Kino(final Id id,
                final Name name, final Adresse adresse,
                final EmailAdresse emailAdresse, final Set<KinoSaal> kinoSaele) {
        this(id, Versionable.unknownVersion(), name, adresse, emailAdresse, kinoSaele);
    }

    public Kino(final Id id, final Version version,
                final Name name, final Adresse adresse,
                final EmailAdresse emailAdresse, final Set<KinoSaal> kinoSaele) {
        this.id = id;
        this.version = version;
        this.name = name;
        this.adresse = adresse;
        this.emailAdresse = emailAdresse;
        this.kinoSaele = kinoSaele;
    }

    @Override
    public Id id() {
        return id;
    }

    @Override
    public Version version() {
        return version;
    }

    public void addSaele(Collection<KinoSaal> saele) {
        kinoSaele.addAll(saele);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
    }

    @Override
    public boolean equals(final Object o) {
        if (!equalsByContent(o)) return false;

        if (this == o) {return true;}
        if (o == null || getClass() != o.getClass()) {return false;}
        Kino that = (Kino) o;
        return new EqualsBuilder().append(version, that.version).isEquals();
    }

    @SuppressWarnings("ConstantValue")
    @Override
    public boolean equalsByContent(final Object o) {
        if (this == o) {return true;}

        if (o == null || getClass() != o.getClass()) {return false;}

        Kino that = (Kino) o;

        boolean result = new EqualsBuilder()
                .append(id, that.id)
                .append(name, that.name)
                .append(adresse, that.adresse).append(emailAdresse, that.emailAdresse)
                .isEquals();
        if (!result) return false;

        if (kinoSaele == null && that.kinoSaele == null) return true;
        if (kinoSaele == null && that.kinoSaele != null) return false;
        if (kinoSaele != null && that.kinoSaele == null) return false;

        List<KinoSaal> thisKSList = kinoSaele.stream().toList();
        List<KinoSaal> thatKSList = that.kinoSaele.stream().toList();
        if (thisKSList.size() != thatKSList.size()) return false;
        for (int counter = 0; counter < thisKSList.size(); counter++) {
            if (!thisKSList.get(counter).equalsByContent(thatKSList.get(counter))) return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(name).append(adresse).append(emailAdresse)
                .append(kinoSaele.stream().toList()) // need to use list, as Set does not support deep equals
                .toHashCode();
    }
}
