package de.accso.flexinale.domain.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import de.accso.flexinale.shared_kernel.*;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

public class KinoSaal implements Identifiable, Versionable, EqualsByContent {
    public record Name(String raw) implements RawWrapper<String> {}
    public record AnzahlPlaetze(Integer raw) implements RawWrapper<Integer> {}

    public final Id id;
    public final Version version;

    public Name name;
    public AnzahlPlaetze anzahlPlaetze = new AnzahlPlaetze(0); //TODO why do we initialize this and not the rest?

    @JsonIgnore
    @DoNotCheckInArchitectureTests
    public Kino kino;

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

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

    public KinoSaal(final Id id,
                    final Name name, final AnzahlPlaetze anzahlPlaetze, final Kino kino) {
        this(id, Versionable.unknownVersion(), name, anzahlPlaetze, kino);
    }

    public KinoSaal(final Id id, final Version version,
                    final Name name, final AnzahlPlaetze anzahlPlaetze, final Kino kino) {
        this.id = id;
        this.version = version;
        this.name = name;
        this.anzahlPlaetze = anzahlPlaetze;
        this.kino = kino;
    }

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

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

    @Override
    public String toString() {
        return "KinoSaal{" +
                "id='" + id + '\'' +
                ", version=" + version +
                ", name='" + name + '\'' +
                ", anzahlPlaetze=" + anzahlPlaetze +
                (kino != null ? ", kino.id=" + kino.id : ", kino=null") +
                '}';
    }

    @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;}
        KinoSaal that = (KinoSaal) 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;}

        KinoSaal that = (KinoSaal) o;

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

        if (kino == null && that.kino == null) return true;
        if (kino == null && that.kino != null) return false;
        if (kino != null) {
            result = kino.id.equals(that.kino.id);  // do not check kino but only its id (otherwise Stackoverflow error)
        }

        return result;
    }

    @Override
    public int hashCode() {
        HashCodeBuilder builder = new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(name).append(anzahlPlaetze);
        if (kino != null) {
            builder.append(kino.id); // do not use kino but only its id (otherwise Stackoverflow error)
        }
        return builder.toHashCode();
    }
}
