package com.example.sleep.activity;

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.R;
import com.example.sleep.database.UserBean;
import com.example.sleep.database.dao.DaoSession;
import com.example.sleep.database.dao.UserBeanDao;
//处理用户的注册操作
public class RegisterActivity extends AppCompatActivity {

    private EditText registerUsernameEditText;//用户名
    private EditText registerPasswordEditText;//密码
    private Button registerButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
//设置一些基本的视图元素，并为其中视图元素设置一个简单的事件处理程序。
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        registerUsernameEditText = findViewById(R.id.usernameEditText);
        registerPasswordEditText = findViewById(R.id.passwordEditText);
        registerButton = findViewById(R.id.registerButton);
        findViewById(R.id.btn_AfterSleep).setOnClickListener(v -> finish());

        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 处理注册逻辑
                String newUsername = registerUsernameEditText.getText().toString();
                String newPassword = registerPasswordEditText.getText().toString();
                // 将新用户的用户名和密码保存到GreenDAO数据库中
                // 执行注册操作
                // 创建一个新的 User 对象并设置用户名和密码
                UserBean newUser = new UserBean();
                newUser.setUsername(newUsername);
                newUser.setPassword(newPassword);
                // 获取 GreenDAO 的 DAOSession
                DaoSession mDaoSession = App.mDaoSession;
                UserBeanDao userBeanDao = mDaoSession.getUserBeanDao();
                long id = userBeanDao.insert(newUser);
                System.out.println("userDao: " + id);
                if (id != -1) Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
}