package com.example.myapplication6;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.android.material.button.MaterialButton;

public class LoginActivity extends BaseActivity {

    public static final String EXTRA_REDIRECT = "redirect";
    public static final String REDIRECT_SETTINGS = "settings";

    private EditText editUsername;
    private EditText editPassword;
    private MaterialButton btnSignIn;
    private MaterialButton btnCreateAccount;
    private Button btnBackToSettings;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        editUsername = findViewById(R.id.editUsername);
        editPassword = findViewById(R.id.editPassword);
        btnSignIn = findViewById(R.id.btnSignIn);
        btnCreateAccount = findViewById(R.id.btnCreateAccount);
        btnBackToSettings = findViewById(R.id.btnBackToSettings);

        editPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

        btnSignIn.setOnClickListener(v -> doSignIn());
        btnCreateAccount.setOnClickListener(v -> doCreateAccount());
        btnBackToSettings.setOnClickListener(v -> {
            Intent i = new Intent(this, SettingsActivity.class);
            startActivity(i);
            finish();
        });
    }

    @Override
    protected boolean requiresLogin() {
        return false;
    }

    private void doSignIn() {
        String username = safeTrim(editUsername.getText() != null ? editUsername.getText().toString() : "");
        String password = safeTrim(editPassword.getText() != null ? editPassword.getText().toString() : "");

        if (username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, R.string.error_missing_credentials, Toast.LENGTH_SHORT).show();
            return;
        }

        AuthDb db = new AuthDb(this);
        boolean ok = db.verifyUser(username, password);
        if (!ok) {
            Toast.makeText(this, R.string.error_invalid_credentials, Toast.LENGTH_SHORT).show();
            return;
        }

        AuthManager.setLoggedIn(this, username);
        goAfterLogin();
    }

    private void doCreateAccount() {
        String username = safeTrim(editUsername.getText() != null ? editUsername.getText().toString() : "");
        String password = safeTrim(editPassword.getText() != null ? editPassword.getText().toString() : "");

        if (username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, R.string.error_missing_credentials, Toast.LENGTH_SHORT).show();
            return;
        }

        AuthDb db = new AuthDb(this);
        boolean created = db.createUser(username, password);
        if (!created) {
            Toast.makeText(this, R.string.error_user_already_exists, Toast.LENGTH_SHORT).show();
            return;
        }

        Toast.makeText(this, R.string.toast_account_created, Toast.LENGTH_SHORT).show();
        AuthManager.setLoggedIn(this, username);
        goAfterLogin();
    }

    private void goAfterLogin() {
        String redirect = getIntent().getStringExtra(EXTRA_REDIRECT);
        Intent i;
        if (REDIRECT_SETTINGS.equals(redirect)) {
            i = new Intent(this, SettingsActivity.class);
        } else {
            i = new Intent(this, DashboardActivity.class);
        }
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(i);
        finish();
    }

    private static String safeTrim(String s) {
        return s == null ? "" : s.trim();
    }
}

