package com.example.myapplication6;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

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

public class SettingsActivity extends BaseActivity {

    private RadioGroup rgLanguage;
    private RadioButton rbEnglish;
    private RadioButton rbTurkish;
    private MaterialButton btnApplyLanguage;

    private TextView textLoginStatus;
    private MaterialButton btnLoginOrLogout;
    private Button btnBack;

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

        rgLanguage = findViewById(R.id.rgLanguage);
        rbEnglish = findViewById(R.id.rbEnglish);
        rbTurkish = findViewById(R.id.rbTurkish);
        btnApplyLanguage = findViewById(R.id.btnApplyLanguage);

        textLoginStatus = findViewById(R.id.textLoginStatus);
        btnLoginOrLogout = findViewById(R.id.btnLoginOrLogout);
        btnBack = findViewById(R.id.btnBack);

        String lang = LocaleHelper.getPreferredLanguage(this);
        if ("tr".equalsIgnoreCase(lang)) {
            rbTurkish.setChecked(true);
        } else {
            rbEnglish.setChecked(true);
        }

        refreshLoginUI();

        btnApplyLanguage.setOnClickListener(v -> {
            String selected = getSelectedLanguage();
            LocaleHelper.setPreferredLanguage(this, selected);
            // Apply immediately by restarting the app's entry screen.
            Intent i = new Intent(this, DashboardActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(i);
            finish();
        });

        btnLoginOrLogout.setOnClickListener(v -> {
            if (AuthManager.isLoggedIn(this)) {
                AuthManager.signOut(this);
                Toast.makeText(this, R.string.toast_logged_out, Toast.LENGTH_SHORT).show();
                refreshLoginUI();
            } else {
                Intent i = new Intent(this, LoginActivity.class);
                i.putExtra(LoginActivity.EXTRA_REDIRECT, LoginActivity.REDIRECT_SETTINGS);
                startActivity(i);
            }
        });

        btnBack.setOnClickListener(v -> {
            Intent i = new Intent(this, DashboardActivity.class);
            startActivity(i);
            finish();
        });
    }

    private void refreshLoginUI() {
        if (AuthManager.isLoggedIn(this)) {
            String username = AuthManager.getUsername(this);
            textLoginStatus.setText(getString(R.string.login_status_logged_in, username));
            btnLoginOrLogout.setText(R.string.action_logout);
        } else {
            textLoginStatus.setText(R.string.login_status_logged_out);
            btnLoginOrLogout.setText(R.string.action_login);
        }
    }

    private String getSelectedLanguage() {
        int checkedId = rgLanguage.getCheckedRadioButtonId();
        if (checkedId == rbTurkish.getId()) return "tr";
        return "en";
    }

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

