/*
 * @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.backup.json.coders;

import androidx.annotation.NonNull;

import java.util.List;

import com.hardbacknutter.nevertoomanybooks.database.DBKey;
import com.hardbacknutter.nevertoomanybooks.entities.Identifier;
import com.hardbacknutter.nevertoomanybooks.entities.Series;
import com.hardbacknutter.org.json.JSONArray;
import com.hardbacknutter.org.json.JSONException;
import com.hardbacknutter.org.json.JSONObject;

public class SeriesCoder
        implements JsonCoder<Series> {

    private static final String ALT_COMPLETE_KEY = "complete";

    private final JsonCoder<Identifier.Value> identifierValueCoder = new IdentifierValueCoder();

    SeriesCoder() {
    }

    @Override
    @NonNull
    public JSONObject encode(@NonNull final Series series)
            throws JSONException {
        final JSONObject out = new JSONObject();

        out.put(DBKey.PK_ID, series.getId());
        out.put(DBKey.SERIES.TITLE, series.getTitle());

        if (!series.getNumber().isEmpty()) {
            out.put(DBKey.SERIES.BOOK_SERIES_NUMBER, series.getNumber());
        }
        if (series.isComplete()) {
            out.put(DBKey.SERIES.COMPLETE, true);
        }

        final List<Identifier.Value> identifiers = series.getIdentifiers();
        if (!identifiers.isEmpty()) {
            out.put(Identifier.Value.BKEY_LIST, identifierValueCoder.encode(identifiers));
        }
        return out;
    }

    @Override
    @NonNull
    public Series decode(@NonNull final JSONObject data)
            throws JSONException {

        final Series series = new Series(data.getString(DBKey.SERIES.TITLE));
        series.setId(data.getLong(DBKey.PK_ID));

        if (data.has(DBKey.SERIES.BOOK_SERIES_NUMBER)) {
            series.setNumber(data.getString(DBKey.SERIES.BOOK_SERIES_NUMBER));
        }
        if (data.has(DBKey.SERIES.COMPLETE)) {
            series.setComplete(data.getBoolean(DBKey.SERIES.COMPLETE));
        } else if (data.has(ALT_COMPLETE_KEY)) {
            series.setComplete(data.getBoolean(ALT_COMPLETE_KEY));
        }

        if (data.has(Identifier.Value.BKEY_LIST)) {
            final JSONArray array = data.getJSONArray(Identifier.Value.BKEY_LIST);
            series.setIdentifiers(identifierValueCoder.decode(array));
        }
        return series;
    }
}
