package com.example.myapplication.ui;
import com.example.myapplication.R;
import com.example.myapplication.Utils;
import com.example.myapplication.service.AuthService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class LoginUI extends AppCompatActivity {

    private SharedPreferences prefs;

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

        prefs = getSharedPreferences("app_prefs", MODE_PRIVATE);

        EditText usernameEt = findViewById(R.id.usernameInput);
        EditText passwordEt = findViewById(R.id.passwordInput);
        Button loginBtn = findViewById(R.id.loginBtn);
        TextView registerLink = findViewById(R.id.registerLink);

        loginBtn.setOnClickListener(v -> {
            String username = usernameEt.getText().toString().trim();
            String password = passwordEt.getText().toString().trim();
            if (!AuthService.login(this, username, password)) {
                Utils.toast(this, "请输入用户名和密码");
                return;
            }
            Utils.toast(this, "登录成功！即将跳转到主界面");
            startActivity(new Intent(this, MainUI.class));
            finish();
        });

        registerLink.setOnClickListener(v ->
                startActivity(new Intent(this, RegisterUI.class)));
    }
}