package ch.threema.app.fragments.wizard;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.InputFilter;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import org.slf4j.Logger;

import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import ch.threema.app.R;
import ch.threema.app.activities.wizard.WizardBaseActivity;
import ch.threema.android.textwatchers.SimpleTextWatcher;
import ch.threema.app.utils.EditTextUtil;
import ch.threema.app.utils.RuntimeUtil;

import static ch.threema.base.utils.LoggingKt.getThreemaLogger;

import ch.threema.domain.models.Nickname;

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

public class WizardFragment2 extends WizardFragment {
    private static final Logger logger = getThreemaLogger("WizardFragment2");
    private EditText nicknameText;
    public static final int PAGE_ID = 2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = Objects.requireNonNull(super.onCreateView(inflater, container, savedInstanceState));

        WizardFragment4.SettingsInterface callback = (WizardFragment4.SettingsInterface) requireActivity();

        TextView title = rootView.findViewById(R.id.wizard_title);
        title.setText(R.string.new_wizard_choose_nickname);

        // inflate content layout
        contentViewStub.setLayoutResource(R.layout.fragment_wizard2);
        contentViewStub.inflate();

        nicknameText = rootView.findViewById(R.id.wizard_edit1);
        if (callback.isReadOnlyProfile()) {
            nicknameText.setEnabled(false);
            rootView.findViewById(R.id.disabled_by_policy).setVisibility(View.VISIBLE);
        } else {
            nicknameText.addTextChangedListener(new SimpleTextWatcher() {
                @Override
                public void afterTextChanged(@NonNull Editable editable) {
                    Activity activity = requireActivity();
                    if (activity.getCurrentFocus() == nicknameText) {
                        ((OnSettingsChangedListener) activity).onNicknameSet(editable.toString());
                    }
                }
            });
            // TODO(ANDR-3180): Consolidate nickname length
            nicknameText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(Nickname.MAX_BYTE_LENGTH)});
        }
        this.nicknameText.setOnKeyListener((v, keyCode, event) -> {
            if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                if (getActivity() != null && isAdded()) {
                    ((WizardBaseActivity) getActivity()).nextPage();
                }
                return true;
            }
            return false;
        });

        return rootView;
    }

    @Override
    protected int getAdditionalInfoText() {
        return R.string.new_wizard_info_nickname;
    }

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

    public interface OnSettingsChangedListener {
        void onNicknameSet(String nickname);
    }

    @Override
    public void onResume() {
        super.onResume();
        new Handler(Looper.getMainLooper()).postDelayed(() -> RuntimeUtil.runOnUiThread(() -> {
            initValues();
            if (nicknameText != null) {
                nicknameText.requestFocus();
                EditTextUtil.showSoftKeyboard(nicknameText);
            }
        }), 50);
    }

    @Override
    public void onPause() {
        if (this.nicknameText != null) {
            this.nicknameText.clearFocus();
            EditTextUtil.hideSoftKeyboard(this.nicknameText);
        }
        super.onPause();
    }

    private void initValues() {
        if (!isResumed()) {
            return;
        }
        WizardFragment4.SettingsInterface callback = (WizardFragment4.SettingsInterface) requireActivity();
        final @Nullable String nickname = callback.getNickname();
        // If the nickname is longer than allowed, we increase the maximum length of the
        // nickname edit text because in this case the nickname comes from an external MDM as
        // there are no other possibilities to add such a long nickname.
        // Note: This is necessary to prevent a crash when calling 'setSelection'.
        // TODO(ANDR-3180): Consolidate nickname length
        if (nickname != null && nickname.length() > Nickname.MAX_BYTE_LENGTH) {
            nicknameText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(nickname.length())});
        }
        nicknameText.setText(nickname);
        if (!isNullOrEmpty(nickname)) {
            nicknameText.setSelection(nickname.length());
        }
    }
}
