package ch.threema.app.dialogs;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;

import org.slf4j.Logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatDialog;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import ch.threema.app.R;
import ch.threema.app.emojis.EmojiMarkupUtil;
import static ch.threema.base.utils.LoggingKt.getThreemaLogger;

import static ch.threema.app.utils.ActiveScreenLoggerKt.logScreenVisibility;
import static ch.threema.common.JavaCompat.isNullOrBlank;

public class SimpleStringAlertDialog extends ThreemaDialogFragment {
    private static final Logger logger = getThreemaLogger("SimpleStringAlertDialog");
    protected Activity activity;
    @Nullable
    private Runnable onDismissRunnable;

    public static SimpleStringAlertDialog newInstance(int title, CharSequence message) {
        SimpleStringAlertDialog dialog = new SimpleStringAlertDialog();
        Bundle args = new Bundle();
        args.putInt("title", title);
        args.putCharSequence("message", message);

        dialog.setArguments(args);
        return dialog;
    }

    public static SimpleStringAlertDialog newInstance(int title, int message) {
        SimpleStringAlertDialog dialog = new SimpleStringAlertDialog();
        Bundle args = new Bundle();
        args.putInt("title", title);
        args.putInt("messageInt", message);

        dialog.setArguments(args);
        return dialog;
    }

    public static SimpleStringAlertDialog newInstance(int title, int message, boolean noButton) {
        SimpleStringAlertDialog dialog = new SimpleStringAlertDialog();
        Bundle args = new Bundle();
        args.putInt("title", title);
        args.putInt("messageInt", message);
        args.putBoolean("noButton", noButton);

        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        logScreenVisibility(this, logger);
    }

    @Override
    public void onAttach(@NonNull Activity activity) {
        super.onAttach(activity);

        this.activity = activity;
    }

    @Override
    // generally allow state loss for simple string alerts
    public void show(@Nullable FragmentManager manager, String tag) {
        if (manager != null) {
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }
    }

    // generally allow state loss for simple string alerts
    public void show(@NonNull FragmentManager manager) {
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, null);
        ft.commitAllowingStateLoss();
    }

    @NonNull
    @Override
    public AppCompatDialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("title");
        int messageInt = getArguments().getInt("messageInt");
        CharSequence message = getArguments().getCharSequence("message");
        boolean noButton = getArguments().getBoolean("noButton", false);

        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity(), getTheme())
            .setCancelable(false);

        if (title != -1) {
            builder.setTitle(title);
        }

        if (!noButton) {
            builder.setPositiveButton(getString(R.string.ok), null);
        } else {
            setCancelable(false);
        }

        if (isNullOrBlank(message)) {
            builder.setMessage(messageInt);
        } else {
            builder.setMessage(message);
        }
        AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener((dialog) -> applyEmojis(alertDialog));
        return alertDialog;
    }

    private void applyEmojis(AlertDialog alertDialog) {
        View messageView = alertDialog.findViewById(android.R.id.message);
        if (messageView instanceof TextView) {
            TextView textView = (TextView) messageView;
            textView.setText(
                EmojiMarkupUtil.getInstance().addTextSpans(getContext(), textView.getText(), textView, true, true, false, false)
            );
        }
    }

    public SimpleStringAlertDialog setOnDismissRunnable(Runnable onDismissRunnable) {
        this.onDismissRunnable = onDismissRunnable;
        return this;
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);

        if (onDismissRunnable != null) {
            onDismissRunnable.run();
        }
    }
}
