package ch.mod_p.sre24.e5.ui.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import ch.mod_p.sre24.e5.R;
import ch.mod_p.sre24.e5.databinding.ActivityLoginBinding;

/* loaded from: classes.dex */
public class LoginActivity extends AppCompatActivity {
    private LoginViewModel loginViewModel;

    @Override // androidx.fragment.app.FragmentActivity, androidx.activity.ComponentActivity, androidx.core.app.ComponentActivity, android.app.Activity
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        ActivityLoginBinding inflate = ActivityLoginBinding.inflate(getLayoutInflater());
        setContentView(inflate.getRoot());
        this.loginViewModel = (LoginViewModel) new ViewModelProvider(this, new LoginViewModelFactory()).get(LoginViewModel.class);
        final EditText editText = inflate.username;
        final EditText editText2 = inflate.password;
        final Button button = inflate.login;
        final ProgressBar progressBar = inflate.loading;
        this.loginViewModel.getLoginFormState().observe(this, new Observer() { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity$$ExternalSyntheticLambda0
            @Override // androidx.lifecycle.Observer
            public final void onChanged(Object obj) {
                LoginActivity.this.m186lambda$onCreate$0$chmod_psre24e5uiloginLoginActivity(button, editText, editText2, (LoginFormState) obj);
            }
        });
        this.loginViewModel.getLoginResult().observe(this, new Observer() { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity$$ExternalSyntheticLambda1
            @Override // androidx.lifecycle.Observer
            public final void onChanged(Object obj) {
                LoginActivity.this.m187lambda$onCreate$1$chmod_psre24e5uiloginLoginActivity(progressBar, (LoginResult) obj);
            }
        });
        TextWatcher textWatcher = new TextWatcher(this) { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity.1
            final /* synthetic */ LoginActivity this$0;

            @Override // android.text.TextWatcher
            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            @Override // android.text.TextWatcher
            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            {
                this.this$0 = this;
            }

            @Override // android.text.TextWatcher
            public void afterTextChanged(Editable editable) {
                this.this$0.loginViewModel.loginDataChanged(editText.getText().toString(), editText2.getText().toString());
            }
        };
        editText.addTextChangedListener(textWatcher);
        editText2.addTextChangedListener(textWatcher);
        editText2.setOnEditorActionListener(new TextView.OnEditorActionListener() { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity$$ExternalSyntheticLambda2
            @Override // android.widget.TextView.OnEditorActionListener
            public final boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                return LoginActivity.this.m188lambda$onCreate$2$chmod_psre24e5uiloginLoginActivity(editText, editText2, textView, i, keyEvent);
            }
        });
        button.setOnClickListener(new View.OnClickListener() { // from class: ch.mod_p.sre24.e5.ui.login.LoginActivity$$ExternalSyntheticLambda3
            @Override // android.view.View.OnClickListener
            public final void onClick(View view) {
                LoginActivity.this.m189lambda$onCreate$3$chmod_psre24e5uiloginLoginActivity(progressBar, editText, editText2, view);
            }
        });
    }

    /* renamed from: lambda$onCreate$0$ch-mod_p-sre24-e5-ui-login-LoginActivity, reason: not valid java name */
    /* synthetic */ void m186lambda$onCreate$0$chmod_psre24e5uiloginLoginActivity(Button button, EditText editText, EditText editText2, LoginFormState loginFormState) {
        if (loginFormState == null) {
            return;
        }
        button.setEnabled(loginFormState.isDataValid());
        if (loginFormState.getUsernameError() != null) {
            editText.setError(getString(loginFormState.getUsernameError().intValue()));
        }
        if (loginFormState.getPasswordError() != null) {
            editText2.setError(getString(loginFormState.getPasswordError().intValue()));
        }
    }

    /* renamed from: lambda$onCreate$1$ch-mod_p-sre24-e5-ui-login-LoginActivity, reason: not valid java name */
    /* synthetic */ void m187lambda$onCreate$1$chmod_psre24e5uiloginLoginActivity(ProgressBar progressBar, LoginResult loginResult) {
        if (loginResult == null) {
            return;
        }
        progressBar.setVisibility(8);
        if (loginResult.getError() != null) {
            showLoginFailed(loginResult.getError());
        }
        if (loginResult.getSuccess() != null) {
            updateUiWithUser(loginResult.getSuccess());
        }
        setResult(-1);
        finish();
    }

    /* renamed from: lambda$onCreate$2$ch-mod_p-sre24-e5-ui-login-LoginActivity, reason: not valid java name */
    /* synthetic */ boolean m188lambda$onCreate$2$chmod_psre24e5uiloginLoginActivity(EditText editText, EditText editText2, TextView textView, int i, KeyEvent keyEvent) {
        if (i != 6) {
            return false;
        }
        this.loginViewModel.login(getBaseContext(), editText.getText().toString(), editText2.getText().toString());
        return false;
    }

    /* renamed from: lambda$onCreate$3$ch-mod_p-sre24-e5-ui-login-LoginActivity, reason: not valid java name */
    /* synthetic */ void m189lambda$onCreate$3$chmod_psre24e5uiloginLoginActivity(ProgressBar progressBar, EditText editText, EditText editText2, View view) {
        progressBar.setVisibility(0);
        this.loginViewModel.login(getBaseContext(), editText.getText().toString(), editText2.getText().toString());
    }

    private void updateUiWithUser(LoggedInUserView loggedInUserView) {
        Toast.makeText(getApplicationContext(), getString(R.string.correct_password) + loggedInUserView.getDisplayName(), 1).show();
    }

    private void showLoginFailed(Integer num) {
        Toast.makeText(getApplicationContext(), num.intValue(), 0).show();
    }
}
