package com.example.orangesale_end;

import android.content.Intent;
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.orangesale_end.activity.IndexActivity;
import com.example.orangesale_end.activity.RegisterActivity;
import com.example.orangesale_end.entity.OrangeUser;
import com.example.orangesale_end.netrequest.OkHttpUser;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button registerButton, loginButton;
    private EditText usernameText, paswdEdit;

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

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register:
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.login:
                //注册时，我们引入了数据库，登录这里可以通过数据库进行验证，验证跳转到首页，不通过进行提示
                Thread thread = new Thread(runnable);
                thread.start();
                break;
        }

    }

    //界面组件初始化
    private void init() {
        usernameText = findViewById(R.id.username);
        paswdEdit = findViewById(R.id.password);
        loginButton = findViewById(R.id.login);
        loginButton.setOnClickListener(this);
        registerButton = findViewById(R.id.register);
        registerButton.setOnClickListener(this);
    }

    /**
     * 登录验证
     */
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            String username = usernameText.getText().toString();
            String password = paswdEdit.getText().toString();
            OrangeUser orangeUser = new OrangeUser();
            orangeUser.setUsername(username);
            orangeUser.setPassword(password);
            OkHttpUser okHttpUser = new OkHttpUser();
            OrangeUser orangeUser1 = null;
            try {
                orangeUser1 = okHttpUser.userLogin(orangeUser);
                if (orangeUser1 != null) {
                    Intent intent1 = new Intent(MainActivity.this, IndexActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username", usernameText.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(MainActivity.this, "账号或者密码错误", Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
}
