package com.example.map;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.map.database.UserDBHelper;
import com.example.map.enity.User;
import com.example.map.util.ToastUtil;

import java.util.ArrayList;

/**
 * 此类 implements View.OnClickListener 之后，
 * 就可以把onClick事件写到onCreate()方法之外
 * 这样，onCreate()方法中的代码就不会显得很冗余
 */

public class LoginScreen extends AppCompatActivity implements View.OnClickListener{

//    private UserDBHelper userDBHelper;
    private EditText username,userpassword;
    private String userName;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_screen);//屏幕显示的布局
        
        getSupportActionBar().hide();

        initView();//初始化界面
        UserDBHelper userDBHelper = new UserDBHelper(this);
        
        SharedPreferences sp = getSharedPreferences("user_mes", MODE_PRIVATE);
        editor = sp.edit();
        if(sp.getBoolean("flag",false)){
            String user_read = sp.getString("user","");
            String psw_read = sp.getString("psw","");
            username.setText(user_read);
            userpassword.setText(psw_read);
        }
    }

    private void initView() {
        //初始化控件
        username = findViewById(R.id.user_name);
        userpassword = findViewById(R.id.passwd);
        Button btn_Login = findViewById(R.id.Login_button);
        Button tv_register = findViewById(R.id.zhuce_button);
        //设置点击事件监听器
        btn_Login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.zhuce_button: //注册
                Intent intent = new Intent(LoginScreen.this, Register.class);//跳转到注册界面
                startActivity(intent);
                finish();
                break;

            case R.id.Login_button:
                String name = username.getText().toString().trim();
                String password = userpassword.getText().toString().trim();
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
                    UserDBHelper userDBHelper = new UserDBHelper(this);
                    ArrayList<User> data = userDBHelper.getAllData();
                    boolean match = false;
                    for (int i = 0; i < data.size(); i++) {
                        User user = data.get(i);
                        if ((name.equals(user.getName()) && password.equals(user.getPassword()))) {
                            userName = user.getName();
                            match = true;
                            break;
                        } else {
                            match = false;
//                            Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
                        }
                    }
                    if (match) {
                        ToastUtil.show(this,"登录成功");
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        Runnable target;
                        //用线程启动
                        Thread thread = new Thread(){
                            @Override
                            public void run(){
                                try {
                                    sleep(2000);//2秒 模拟登录时间
                                    String user_name = userName;
                                    Intent intent1 = new Intent(LoginScreen.this, MainActivity.class);//设置自己跳转到成功的界面

                                    //intent1.putExtra("user_name",user_name);
                                    startActivity(intent1);
                                    finish();
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }
                        };
                        thread.start();//打开线程
                    } else {
                        Toast.makeText(this, "用户名或密码不正确，请重新输入", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}