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;

public class Film implements Identifiable, Versionable, Mergeable<Film>, EqualsByContent {
    public record Titel(String raw) implements RawWrapper<String> {}
    public record ImdbUrl(String raw) implements RawWrapper<String> {}
    public record DauerInMinuten(Integer raw) implements RawWrapper<Integer> {} 

    public final Id id;
    public final Version version;

    public Titel titel;
    public ImdbUrl imdbUrl;
    public DauerInMinuten dauerInMinuten;

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

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

    public Film(final Id id,
                final Titel titel, final ImdbUrl imdbUrl, final DauerInMinuten dauerInMinuten) {
        this(id, Versionable.unknownVersion(), titel, imdbUrl, dauerInMinuten);
    }

    public Film(final Id id, final Version version,
                final Titel titel, final ImdbUrl imdbUrl, final DauerInMinuten dauerInMinuten) {
        this.id = id;
        this.version = version;
        this.titel = titel;
        this.imdbUrl = imdbUrl;
        this.dauerInMinuten = dauerInMinuten;
    }

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

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

    @Override
    public Film merge(final Film newData) {
        return new Film(this.id, this.version,
                newData.titel, newData.imdbUrl, newData.dauerInMinuten);
    }

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

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

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

        final Film that = (Film) o;

        return new EqualsBuilder()
                .append(id, that.id)
                .append(titel, that.titel).append(imdbUrl, that.imdbUrl)
                .append(dauerInMinuten, that.dauerInMinuten).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(id).append(version)
                .append(titel).append(imdbUrl).append(dauerInMinuten).toHashCode();
    }
}
