package com.example.register.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.register.R;
import com.example.register.dateoperation.Database;
import com.example.register.entity.User;
import com.example.register.netrequest.OkHttpUser;

import java.io.IOException;


public class loginActivity extends AppCompatActivity implements View.OnClickListener {
    Button login, register;
    EditText usernameT, passwordT;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        //对应xml文件中组件的连接
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        usernameT = findViewById(R.id.username);
        passwordT = findViewById(R.id.password);

        login.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setClass(this, userActivity.class);
            startActivity(intent);
        });
        register.setOnClickListener(view -> {
            Intent intent = new Intent();
            intent.setClass(this, registerActivity.class);
            startActivity(intent);
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register:
                Intent intent = new Intent(loginActivity.this, registerActivity.class);
                startActivity(intent);
                break;
            case R.id.login:
                //注册时，我们引入了数据库，登录这里可以通过数据库进行验证，验证跳转到首页，不通过进行提示
                if (validateLogin()) {
                    Intent intent1 = new Intent(loginActivity.this, IndexActivity.class);
                    Bundle bundle = new Bundle();
                    Database database = new Database(loginActivity.this);
                    bundle.putString("username", usernameT.getText().toString());
                    bundle = database.queryUserInfo(database.getReadableDatabase(), bundle);
                    intent1.putExtras(bundle);
                    startActivity(intent1);
                } else {
                    Toast.makeText(loginActivity.this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    /**
     * 登录验证
     *
     * @return
     */
    private boolean validateLogin() {
        String username = usernameT.getText().toString();
        String password = passwordT.getText().toString();
        Database database = new Database(loginActivity.this);
        SQLiteDatabase sqLiteDatabase = database.getReadableDatabase();
        Cursor cursor = sqLiteDatabase.rawQuery("select * from orange_user where username=? and password=?", new String[]{username, password});
        if (cursor.getCount() > 0) {
            return true;
        }
        return false;
    }
    /**
     * 登录验证
     */
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            String username = usernameT.getText().toString();
            String password = passwordT.getText().toString();
            User orangeUser = new User();
            orangeUser.setUsername(username);
            orangeUser.setPassword(password);
            OkHttpUser okHttpUser = new OkHttpUser();
            User orangeUser1 = null;
            try {
                orangeUser1 = okHttpUser.userLogin(orangeUser);
                if (orangeUser1 != null) {
                    Intent intent1 = new Intent(loginActivity.this, IndexActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username", usernameT.getText().toString());
                    bundle.putString("password", orangeUser1.getPassword());
                    bundle.putString("city", orangeUser1.getCity());
                    bundle.putString("sex", orangeUser1.getSex());
                    intent1.putExtras(bundle);
                    startActivity(intent1);
                } else {
                    //解决在子线程中调用Toast的异常情况处理
                    Looper.prepare();
                    Toast.makeText(loginActivity.this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };


}