package com.stu.assistant.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.stu.assistant.R;
import com.stu.assistant.base.BaseActivity;
import com.stu.assistant.base.BaseApplion;
import com.stu.assistant.bean.UserInfoBean;
import com.stu.assistant.presenter.LoginPresenter;
import com.stu.assistant.uitls.AppTools;
import com.stu.assistant.uitls.ChenJingEditUil;
import com.stu.assistant.uitls.MyToast;
import com.stu.assistant.uitls.SharedConfig;
import com.stu.assistant.uitls.SharedUtil;

import androidx.annotation.Nullable;
import cn.bmob.v3.BmobUser;
//登录页面
public class LoginActivity extends BaseActivity {

    private EditText usertNameEdit;
    private EditText usertPasswordEdit;
    private LoginPresenter loginPresenter;
    private TextView loginBtn, versionNameTv;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        ChenJingEditUil.assistActivity(this);
        usertNameEdit = findViewById(R.id.tv_username);
        usertPasswordEdit = findViewById(R.id.tv_userpassword);
        loginBtn = findViewById(R.id.button_login);
        versionNameTv = findViewById(R.id.version_name);
        loginPresenter = new LoginPresenter(this);
    }

    @Override
    protected void initData() {
        versionNameTv.setText(AppTools.getVersionName(this));
    }

    @Override
    protected void initListener() {
        boolean aBoolean = SharedUtil.create(BaseApplion.application).getBoolean(SharedConfig.isLogin, false);
        if (aBoolean) {
            if (BmobUser.isLogin()) {
                UserInfoBean user = BmobUser.getCurrentUser(UserInfoBean.class);
                loginPresenter.saveUser(user);
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            } else {
                login();
            }
        } else {
            login();
        }
    }

    private void login() {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginPresenter.loginIn(usertNameEdit.getText().toString(), usertPasswordEdit.getText().toString(), new LoginPresenter.OnLoginResultListener() {
                    @Override
                    public void isSucLogin(UserInfoBean bean) {
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void errorMsg(String msg) {
                        MyToast.show(msg);
                    }
                });
            }
        });
    }

    public void OnRegister(View view) {
        Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
        intent.putExtra("type", "1");
        startActivityForResult(intent, 99);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 99) {
            if (resultCode == 2) {
                UserInfoBean userBean = (UserInfoBean) data.getSerializableExtra("userBean");
                usertNameEdit.setText(userBean.getUsername());
                usertPasswordEdit.setText("");
            }
        }
    }
}