/*
 * @Copyright 2018-2026 HardBackNutter
 * @License GNU General Public License
 *
 * This file is part of NeverTooManyBooks.
 *
 * NeverTooManyBooks is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * NeverTooManyBooks is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with NeverTooManyBooks. If not, see <http://www.gnu.org/licenses/>.
 */
package com.hardbacknutter.nevertoomanybooks.entities;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;
import java.util.Objects;

import com.hardbacknutter.nevertoomanybooks.booklist.style.Style;
import com.hardbacknutter.nevertoomanybooks.core.utils.LocaleListUtils;
import com.hardbacknutter.nevertoomanybooks.database.DBKey;
import com.hardbacknutter.nevertoomanybooks.utils.ReorderHelper;

/**
 * Represents a Publisher.
 * <p>
 * ENHANCE: The Publisher Locale should be based on the country where they are.
 */
public class Publisher
        implements Parcelable, Entity, Mergeable {

    /** {@link Parcelable}. */
    public static final Creator<Publisher> CREATOR = new Creator<>() {
        @Override
        @NonNull
        public Publisher createFromParcel(@NonNull final Parcel source) {
            return new Publisher(source);
        }

        @Override
        @NonNull
        public Publisher[] newArray(final int size) {
            return new Publisher[size];
        }
    };

    /** Row ID. */
    private long id;
    /** Publisher name. */
    @NonNull
    private String name;

    /**
     * Copy constructor.
     *
     * @param publisher to copy
     */
    public Publisher(@NonNull final Publisher publisher) {
        copyFrom(publisher);
    }

    /**
     * Constructor.
     *
     * @param name of publisher.
     */
    public Publisher(@NonNull final String name) {
        this.name = name.strip();
    }

    /**
     * Full constructor.
     *
     * @param id      ID of the Publisher in the database.
     * @param rowData with data
     */
    public Publisher(final long id,
                     @NonNull final DataHolder rowData) {
        this.id = id;
        name = rowData.getString(DBKey.PUBLISHER.NAME);
    }

    /**
     * {@link Parcelable} Constructor.
     *
     * @param in Parcel to construct the object from
     */
    private Publisher(@NonNull final Parcel in) {
        id = in.readLong();
        //noinspection DataFlowIssue
        name = in.readString();
    }

    /**
     * Constructor.
     *
     * @param name of publisher.
     *
     * @return Publisher
     */
    @NonNull
    public static Publisher from(@NonNull final String name) {
        return new Publisher(name);
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(@NonNull final Parcel dest,
                              final int flags) {
        dest.writeLong(id);
        dest.writeString(name);
    }

    @Override
    public long getId() {
        return id;
    }

    public void setId(final long id) {
        this.id = id;
    }

    @Override
    @NonNull
    public String getLabel(@NonNull final Context context,
                           @Nullable final Details details,
                           @NonNull final Style style) {

        // yes, we're using the 'title'. Adding specific publisher logic was
        // too much overhead for a presumably little used feature
        if (style.isShowReorderedTitle()) {
            final ReorderHelper reorderHelper = new ReorderHelper(
                    LocaleListUtils.asList(context.getResources().getConfiguration().getLocales()));
            // Using the locale here is overkill;  see #getLocale(..)
            return reorderHelper.reorder(context, name);
        } else {
            return name;
        }
    }

    /**
     * Get the <strong>unformatted</strong> name.
     *
     * @return the name
     */
    @NonNull
    public String getName() {
        return name;
    }

    /**
     * Set the unformatted name; as entered manually by the user.
     *
     * @param name to use
     */
    public void setName(@NonNull final String name) {
        this.name = name;
    }

    /**
     * <strong>Replace</strong> local details with those from the given Publisher.
     *
     * @param source to copy from
     */
    public void copyFrom(@NonNull final Publisher source) {
        name = source.name;
    }

    /**
     * <strong>Merge</strong> local details with those from the given Publisher.
     * The <em>name</em> is never merged.
     *
     * @param source to copy from
     *
     * @return {@code true} if this Publisher was modified in any way
     */
    public boolean merge(@NonNull final Publisher source) {
        // placeholder method, there is nothing to merge for now.
        return false;
    }

    @NonNull
    @Override
    public List<String> getNameFields() {
        return List.of(name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    /**
     * Enhanced {@link #equals(Object)}.
     *
     * @param that to compare to
     *
     * @return {@code true} if equals
     */
    public boolean isIdentical(@Nullable final Publisher that) {
        // No other fields for now
        return equals(that);
    }

    /**
     * Equality: <strong>id, name</strong>.
     * <p>
     * <strong>Comparing is DIACRITIC and CASE SENSITIVE</strong>:
     * This allows correcting case mistakes even with identical ID.
     *
     * @see #isIdentical(Publisher)
     */
    @Override
    public boolean equals(@Nullable final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        final Publisher that = (Publisher) o;
        // if both 'exist' but have different ID's -> different.
        if (id != 0 && that.id != 0 && id != that.id) {
            return false;
        }

        // The ids MAY be different, but at least one is != 0
        return Objects.equals(name, that.name);
    }

    @Override
    @NonNull
    public String toString() {
        return "Publisher{"
               + "id=" + id
               + ", name=`" + name + '`'
               + '}';
    }
}
