package com.getbubblenow.android.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;

import com.getbubblenow.android.R;
import com.getbubblenow.android.viewmodel.SetLauncherViewModel;
import com.jakewharton.rxbinding4.widget.RxTextView;

import androidx.appcompat.widget.AppCompatButton;
import androidx.lifecycle.ViewModelProvider;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;


public class SetLauncherActivity extends BaseActivityBubble {

    private static final String PREFIX = "https://";

    private SetLauncherViewModel setLauncherViewModel;
    private EditText launcherUrl;
    private AppCompatButton setLauncherButton;

    private final CompositeDisposable compositeSubscription = new CompositeDisposable();


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_sage);
        initUI();
        setLauncherViewModel = new ViewModelProvider(this).get(SetLauncherViewModel.class);
    }

    @Override protected void onDestroy() {
        super.onDestroy();
        compositeSubscription.dispose();
    }

    private void initUI() {
        initViews();
        initListeners();
    }

    private void initListeners() {
        setLauncherButton.setOnClickListener(v -> setLauncher());
        sendListener();
        launcherUrlStateListener();
    }

    private void initViews() {
        launcherUrl = findViewById(R.id.launcher_url);
        setLauncherButton = findViewById(R.id.setLauncherButton);
    }

    private void sendListener() {
        launcherUrl.setOnEditorActionListener((v, actionId, event) -> {
            if ((event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP)
                    || (actionId == EditorInfo.IME_ACTION_SEND)) {
                setLauncher();
                return true;
            }
            return false;
        });
    }

    private void setLauncher() {
        String launcherInput = launcherUrl.getText().toString().trim();

        if (!launcherInput.isEmpty()) {
            if (!launcherInput.startsWith(PREFIX)) {
                launcherInput = PREFIX + launcherInput;
            }
        }
        setLauncherViewModel.setSageHost(this, launcherInput);
        finish();
    }

    private void launcherUrlStateListener() {
        final Disposable subscribe = RxTextView
                .afterTextChangeEvents(launcherUrl)
                .subscribe(view -> setButtonState(!launcherUrl.getText().toString().trim().isEmpty()));
        compositeSubscription.add(subscribe);
    }

    private void setButtonState(final boolean launcherUrlStateFlag) {
        if (launcherUrlStateFlag) {
            setLauncherButton.setBackgroundDrawable(getDrawable(R.drawable.sign_in_enable));
            setLauncherButton.setEnabled(true);
        } else {
            setLauncherButton.setBackgroundDrawable(getDrawable(R.drawable.sign_in_disable));
            setLauncherButton.setEnabled(false);
        }
    }

}
