package com.example.sleep.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.sleep.App;
import com.example.sleep.MainScreen;
import com.example.sleep.R;
import com.example.sleep.database.UserBean;
import com.example.sleep.database.dao.DaoSession;
import com.example.sleep.database.dao.UserBeanDao;

import java.util.List;

// LoginActivity.java
//定义了一个名为LoginActivity的类。这个类在Android应用中处理用户的登录操作。
public class LoginActivity extends AppCompatActivity {

    private EditText usernameEditText;
    private EditText passwordEditText;
    private Button loginButton;
    private Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

//初始化LoginActivity的各种资源，以及为一些控件设置事件监听器。

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        usernameEditText = findViewById(R.id.usernameEditText);
        passwordEditText = findViewById(R.id.passwordEditText);
        loginButton = findViewById(R.id.loginButton);
        registerButton = findViewById(R.id.registerButton);
        findViewById(R.id.btn_AfterSleep).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 处理登录逻辑
                String username = usernameEditText.getText().toString();
                String password = passwordEditText.getText().toString();
                // 1. 获取SharedPreferences实例
                SharedPreferences sharedPreferences = LoginActivity.this.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);

                SharedPreferences.Editor editor = sharedPreferences.edit();

                editor.putString("UserNameKey", username);

                editor.apply();

                // 验证用户名和密码，从GreenDAO数据库中检查
                // 如果匹配成功，执行登录操作
                // 否则，显示错误消息
                // 获取 GreenDAO 的 DAOSession
                DaoSession mDaoSession = App.mDaoSession;
                UserBeanDao userBeanDao = mDaoSession.getUserBeanDao();
                // 查询数据库以验证用户名和密码
                List<UserBean> matchingUsers = userBeanDao.queryBuilder()
                        .where(UserBeanDao.Properties.Username.eq(username), UserBeanDao.Properties.Password.eq(password))
                        .list();

                if (matchingUsers != null && !matchingUsers.isEmpty()) {
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this, MainScreen.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "用户名或密码不正确", Toast.LENGTH_SHORT).show();
                }
            }
        });

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 跳转到注册页面
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }
}