/*
 * @Copyright 2018-2025 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.dialogs.entities.inlinestring;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.ViewModelProvider;

import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

import com.hardbacknutter.nevertoomanybooks.R;
import com.hardbacknutter.nevertoomanybooks.core.widgets.adapters.ExtArrayAdapter;
import com.hardbacknutter.nevertoomanybooks.database.dao.InlineStringDao;
import com.hardbacknutter.nevertoomanybooks.databinding.DialogEditBookInlineStringContentBinding;
import com.hardbacknutter.nevertoomanybooks.dialogs.DialogType;
import com.hardbacknutter.nevertoomanybooks.dialogs.FlexDialogDelegate;
import com.hardbacknutter.nevertoomanybooks.widgets.TilUtil;

/**
 * Dialog to edit an <strong>in-line {@code String} in the Books table</strong>
 * supported by an {@link InlineStringDao}.
 */
class EditInLineStringDelegate
        implements FlexDialogDelegate {

    @NonNull
    private final String dialogTitle;
    @NonNull
    private final String label;

    private final EditInLineStringViewModel vm;
    @NonNull
    private final DialogFragment owner;
    @NonNull
    private final String requestKey;
    private DialogEditBookInlineStringContentBinding vb;
    @Nullable
    private Toolbar toolbar;

    /**
     * Constructor.
     * <p>
     * Class output: {@link EditInLineStringOutput}.
     *
     * @param owner         hosting DialogFragment
     * @param args          all arguments
     * @param dialogTitleId for the dialog (i.e. the toolbar)
     * @param labelResId    to use for the 'hint' of the input field
     * @param daoSupplier   the {@link InlineStringDao} supplier
     */
    EditInLineStringDelegate(@NonNull final DialogFragment owner,
                             @NonNull final EditInLineStringInput args,
                             @StringRes final int dialogTitleId,
                             @StringRes final int labelResId,
                             @NonNull final Supplier<InlineStringDao> daoSupplier) {
        this.owner = owner;
        requestKey = args.getRequestKey();

        vm = new ViewModelProvider(owner).get(EditInLineStringViewModel.class);
        vm.init(args, daoSupplier);

        final Context context = owner.getContext();
        //noinspection DataFlowIssue
        this.dialogTitle = context.getString(dialogTitleId);
        this.label = context.getString(labelResId);
    }

    @NonNull
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater,
                             @Nullable final ViewGroup container) {
        vb = DialogEditBookInlineStringContentBinding.inflate(inflater, container, false);
        return vb.getRoot();
    }

    @Override
    @NonNull
    public View onCreateFullscreen(@NonNull final LayoutInflater inflater,
                                   @Nullable final ViewGroup container) {
        final View view = inflater.inflate(R.layout.dialog_edit_book_inline_string,
                                           container, false);
        vb = DialogEditBookInlineStringContentBinding.bind(view.findViewById(R.id.dialog_content));
        return view;
    }

    @Override
    public void setToolbar(@Nullable final Toolbar toolbar) {
        this.toolbar = toolbar;
    }

    @NonNull
    public Toolbar getToolbar() {
        return Objects.requireNonNull(toolbar, "No toolbar set");
    }

    @Override
    public void onViewCreated(@NonNull final DialogType dialogType) {
        if (toolbar != null) {
            if (dialogType == DialogType.BottomSheet) {
                toolbar.inflateMenu(R.menu.toolbar_action_save);
            }
            initToolbar(owner, dialogType, toolbar);
            toolbar.setTitle(dialogTitle);
        }

        final Context context = vb.getRoot().getContext();

        vb.lblEditString.setHint(label);
        vb.editString.setText(vm.getCurrentEdit());
        TilUtil.autoRemoveError(vb.editString, vb.lblEditString);

        // soft-keyboards 'done' button act as a shortcut to confirming/saving the changes
        vb.editString.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                if (saveChanges()) {
                    owner.dismiss();
                }
                return true;
            }
            return false;
        });

        final ExtArrayAdapter<String> adapter = new ExtArrayAdapter<>(
                context, R.layout.popup_dropdown_menu_item,
                ExtArrayAdapter.FilterType.Diacritic, getList(context));
        vb.editString.setAdapter(adapter);

        vb.editString.requestFocus();
    }

    @Override
    public void onToolbarNavigationClick(@NonNull final View v) {
        owner.dismiss();
    }

    @Override
    public boolean onToolbarButtonClick(@Nullable final View button) {
        if (button != null) {
            final int id = button.getId();
            if (id == R.id.toolbar_btn_save || id == R.id.btn_positive) {
                if (saveChanges()) {
                    owner.dismiss();
                }
                return true;
            }
        }
        return false;
    }

    private boolean saveChanges() {
        viewToModel();

        final Context context = vb.getRoot().getContext();

        if (vm.getCurrentEdit().isEmpty()) {
            vb.lblEditString.setError(context.getString(R.string.vldt_non_blank_required));
            return false;
        }

        // anything actually changed ? If not, we're done.
        if (!vm.isModified()) {
            return true;
        }

        final String storedText = onSave(context, vm.getOriginal(), vm.getCurrentEdit());

        new EditInLineStringOutput(vm.getOriginal(), storedText)
                .send(owner, requestKey);
        return true;
    }

    /**
     * Get the (optional) list of strings for the auto-complete.
     * <p>
     * Override if needed.
     *
     * @param context Current context
     *
     * @return list
     */
    @NonNull
    List<String> getList(@NonNull final Context context) {
        return vm.getList();
    }

    /**
     * Save the modifications to the database.
     * <p>
     * Override if needed.
     *
     * @param context      Current context
     * @param originalText the original text which was passed in to be edited
     * @param currentText  the modified text
     *
     * @return the text as <strong>stored</strong> which can be different from
     *         the modified text passed in when this method is overridden.
     */
    @NonNull
    String onSave(@NonNull final Context context,
                  @NonNull final String originalText,
                  @NonNull final String currentText) {
        vm.rename(originalText, currentText);
        return currentText;
    }

    @Override
    public void onPause(@NonNull final LifecycleOwner lifecycleOwner) {
        viewToModel();
    }

    private void viewToModel() {
        vm.setCurrentEdit(vb.editString.getText().toString().strip());
    }
}
