package de.accso.flexinale.domain.model;

import de.accso.flexinale.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.time.LocalDateTime;

import static de.accso.flexinale.shared_kernel.RawWrapper.getRawOrNull;

public final class Vorfuehrung implements Identifiable, Versionable, EqualsByContent {   // TODO is only final as otherwise Spotbugs complains, get rid of Exception in constructor!
    public record Zeit(LocalDateTime raw) implements RawWrapper<LocalDateTime> {
        public Zeit(LocalDateTime raw) {
            this.raw = raw.withNano(0); // precision is second
        }
    }

    public final Id id;
    public final Version version;

    public final Zeit zeit;

    @DoNotCheckInArchitectureTests
    public final Film film;
    @DoNotCheckInArchitectureTests
    public final KinoSaal kinoSaal;

    public Kontingent getKontingent() {
        return kontingent;
    }

    @DoNotCheckInArchitectureTests
    private Kontingent kontingent;  // kontingent is not final because when tickets are sold, the kontingent may change.

    public Vorfuehrung(final Id id,
                       final Zeit zeit,
                       final Film film, final KinoSaal kinoSaal, final Kontingent kontingent) {
        this(id, Versionable.unknownVersion(), zeit, film, kinoSaal, kontingent);
    }

    public Vorfuehrung(final Id id, final Version version,
                       final Zeit zeit,
                       final Film film, final KinoSaal kinoSaal, final Kontingent kontingent) {
        this.id = id;
        this.version = version;
        this.zeit = zeit;
        this.film = film;
        this.kinoSaal = kinoSaal;
        this.kontingent = kontingent;

        //TODO this validation should not be done here. If class is not final, Spotbugs complains. For DB entities use "nullable=false" and "optional=false" for DB checks. In domain classes check with jakarta.annotation NonNull? Also: Why is KinoSaal obligatory but not Film?
        if (kinoSaal == null) {
            throw new FlexinaleIllegalArgumentException("KinoSaal of Vorfuehrung " + this +
                    " must not be null");
        }
        //TODO this validation should not be done here but in KinoSaal itself
        if (kinoSaal.anzahlPlaetze == null) {
            throw new FlexinaleIllegalArgumentException("KinoSaal's Anzahl Plaetze of Vorfuehrung " + this +
                    " must not be null");
        }
        if (kontingent == null) {
            throw new FlexinaleIllegalArgumentException("Kontingent of Vorfuehrung " + this +
                    " must not be null");
        }
    }

    public Vorfuehrung(final Id id,
                       final Zeit zeit, final Film film, final KinoSaal kinoSaal, int quoteOnline) {
        this(id, Versionable.unknownVersion(), zeit, film, kinoSaal, quoteOnline);
    }

    public Vorfuehrung(final Id id, final Version version,
                       final Zeit zeit, final Film film, final KinoSaal kinoSaal, int quoteOnline) {
        this.id = id;
        this.version = version;
        this.zeit = zeit;
        this.film = film;
        this.kinoSaal = kinoSaal;

        if (kinoSaal == null) {
            throw new FlexinaleIllegalArgumentException("KinoSaal of Vorfuehrung " + this +
                    " must not be null");
        }
        if (kinoSaal.anzahlPlaetze == null) {
            throw new FlexinaleIllegalArgumentException("KinoSaal's Anzahl Plaetze of Vorfuehrung " + this +
                    " must not be null");
        }

        kontingent = new Kontingent(Identifiable.Id.of("K" + id), getRawOrNull(kinoSaal.anzahlPlaetze), quoteOnline);
    }

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

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

    public Integer getGesamtkapazitaetVerkauf() {
        return getRawOrNull(kinoSaal.anzahlPlaetze);
    }

    public void verkaufeTickets(final Ticket.VerkaufsKanal verkaufsKanal, final int anzahlGutscheine) {
        switch (verkaufsKanal) {
            case ONLINE    ->
                    this.kontingent = kontingent.reduziereKontingent(Kontingent.KontingentType.ONLINE,    anzahlGutscheine);
            case ZENTRAL   ->
                    this.kontingent = kontingent.reduziereKontingent(Kontingent.KontingentType.ZENTRAL,   anzahlGutscheine);
            case KINOKASSE ->
                    this.kontingent = kontingent.reduziereKontingent(Kontingent.KontingentType.KINOKASSE, anzahlGutscheine);
        }
    }

    public boolean ueberlapptMit(Vorfuehrung other, long puffer) {
        LocalDateTime begin = getRawOrNull(this.zeit);
        LocalDateTime beginOther = getRawOrNull(other.zeit);
        LocalDateTime end = begin.plusMinutes(this.film.dauerInMinuten.raw());
        LocalDateTime endOther = beginOther.plusMinutes(other.film.dauerInMinuten.raw());

        return ((begin.isBefore(endOther.plusMinutes(puffer)))
                && (end.isAfter(beginOther.minusMinutes(puffer))));
    }

    @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;}
        Vorfuehrung that = (Vorfuehrung) o;
        return new EqualsBuilder().append(version, that.version).isEquals();
    }

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

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

        Vorfuehrung that = (Vorfuehrung) o;

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

        if (film == null && that.film == null) result = true;
        if (film == null && that.film != null) return false;
        if (film != null) {
            result = film.equalsByContent(that.film);
            if (!result) return false;
        }

        if (kinoSaal == null && that.kinoSaal == null) result = true;
        if (kinoSaal == null && that.kinoSaal != null) return false;
        if (kinoSaal != null) {
            result = kinoSaal.equalsByContent(that.kinoSaal);
            if (!result) return false;
        }

        if (kontingent == null && that.kontingent == null) result = true;
        if (kontingent == null && that.kontingent != null) return false;
        if (kontingent != null) {
            result = kontingent.equalsByContent(that.kontingent);
        }

        return result;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(zeit).append(film).append(kinoSaal)
                .append(kontingent)
                .toHashCode();
    }
}
