package com.carmaintenance;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class RoleHomeActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_role_home);

        TextView userInfo = findViewById(R.id.userInfo);
        Button primaryAction = findViewById(R.id.primaryAction);
        Button secondaryAction = findViewById(R.id.secondaryAction);
        Button logoutButton = findViewById(R.id.logoutButton);

        String role = getIntent().getStringExtra("role");
        String username = getIntent().getStringExtra("username");

        if (role == null) {
            role = "unknown";
        }
        if (username == null) {
            username = "user";
        }

        userInfo.setText("Welcome " + username + " (" + role + ")");

        if ("admin".equalsIgnoreCase(role)) {
            primaryAction.setText("Manage Users");
            secondaryAction.setText("Manage Orders");
        } else if ("staff".equalsIgnoreCase(role)) {
            primaryAction.setText("Search Vehicles");
            secondaryAction.setText("Parts Lookup");
        } else {
            primaryAction.setText("My Vehicles");
            secondaryAction.setText("My Appointments");
        }

        logoutButton.setOnClickListener(v -> {
            new TokenStore(RoleHomeActivity.this).clear();
            Intent intent = new Intent(RoleHomeActivity.this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        });
    }
}
