/*
 * @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.csv.bc;

import androidx.annotation.NonNull;

import java.util.List;

import com.hardbacknutter.nevertoomanybooks.backup.csv.util.StringList;
import com.hardbacknutter.nevertoomanybooks.database.DBKey;
import com.hardbacknutter.nevertoomanybooks.entities.Author;
import com.hardbacknutter.org.json.JSONException;
import com.hardbacknutter.org.json.JSONObject;

/**
 * StringList factory for an Author.
 * <p>
 * Format: {@code name[ * {json}][|name[ * {json}]|...]}
 * <br>
 * <strong>Note:</strong> the " * {json}" suffix is optional and can be missing.
 * <p>
 * With the name in a format supported by {@link Author#from(String)}
 */
class AuthorCoder
        implements StringList.Coder<Author> {

    @Override
    public char getElementSeparator() {
        return DefaultBookCoder.ELEMENT_SEPARATOR;
    }

    @Override
    @NonNull
    public Author decode(@NonNull final String element) {
        final List<String> parts = StringList.newInstance(DefaultBookCoder.ELEMENT_SEPARATOR)
                                             .decodeElement(element);
        final Author author = Author.from(parts.get(0));
        if (parts.size() > 1) {
            try {
                final JSONObject details = new JSONObject(parts.get(1));
                if (details.has(DBKey.AUTHOR.BIRTH_DATE)) {
                    author.setBirthDate(details.optString(DBKey.AUTHOR.BIRTH_DATE));
                }
                if (details.has(DBKey.AUTHOR.DEATH_DATE)) {
                    author.setDeathDate(details.optString(DBKey.AUTHOR.DEATH_DATE));
                }
                if (details.has(DBKey.AUTHOR.PICTURE_UUID)) {
                    author.setImageUuid(details.optString(DBKey.AUTHOR.PICTURE_UUID));
                }
                if (details.has(DBKey.AUTHOR.COMPLETE)) {
                    author.setComplete(details.optBoolean(DBKey.AUTHOR.COMPLETE));
                } else if (details.has("complete")) {
                    author.setComplete(details.optBoolean("complete"));
                }

                if (details.has(DBKey.AUTHOR.BOOK_AUTHOR_ROLE)) {
                    author.setRole(details.optInt(DBKey.AUTHOR.BOOK_AUTHOR_ROLE));
                } else if (details.has("type")) {
                    author.setRole(details.optInt("type"));
                }
            } catch (@NonNull final JSONException ignore) {
                // ignore
            }
        }
        return author;
    }
}
