package com.itant.shiwushu.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.itant.shiwushu.MainActivity;
import com.itant.shiwushu.R;
import com.itant.shiwushu.base.LAFBaseActivity;
import com.itant.shiwushu.bean.User;
import com.itant.shiwushu.manager.ActivityManager;
import com.itant.shiwushu.manager.ToastManager;
import com.itant.shiwushu.manager.UserManager;
import com.itant.shiwushu.tool.ActivityTool;

/**
 * Created by Jason on 2018/10/2.
 */

public class RegisterActivity extends LAFBaseActivity implements View.OnClickListener, RegisterContract.View {
    private EditText et_nickname;
    private EditText et_email;
    private EditText et_password;
    private EditText et_password_repeat;

    private RegisterPresenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        findViewById(R.id.aciv_back).setOnClickListener(this);
        et_nickname = findViewById(R.id.et_nickname);
        et_email = findViewById(R.id.et_email);
        et_password = findViewById(R.id.et_password);
        et_password_repeat = findViewById(R.id.et_password_repeat);
        findViewById(R.id.btn_register).setOnClickListener(this);

        presenter = new RegisterPresenter(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.aciv_back:
                // 返回
                finish();
                break;

            case R.id.btn_register:
                // 注册
                goRegister();
                break;
        }
    }

    /**
     *  注册
     */
    private void goRegister() {
        String nickName = et_nickname.getText().toString();
        String registerEmail = et_email.getText().toString();
        String password = et_password.getText().toString();
        String repeatPassword = et_password_repeat.getText().toString();

        if (TextUtils.isEmpty(nickName)) {
            ToastManager.getInstance().toastShort("请输入昵称");
            return;
        }

        if (TextUtils.isEmpty(registerEmail)) {
            ToastManager.getInstance().toastShort("请输入邮箱");
            return;
        }

        if (!registerEmail.contains("@")) {
            ToastManager.getInstance().toastShort("邮箱格式有误");
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastManager.getInstance().toastShort("请输入密码");
            return;
        }

        if (!TextUtils.equals(password, repeatPassword)) {
            ToastManager.getInstance().toastShort("两次输入的密码不一致");
            return;
        }

        setLoading(true);
        presenter.register(nickName, registerEmail, password);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.setView(null);
    }

    @Override
    public void onRegisterSuccess(User user) {
        ToastManager.getInstance().toastShort("注册成功");
        // 注册成功，跳转到主界面
        UserManager.getInstance().setUser(user);
        Intent intent = new Intent(this, MainActivity.class);
        ActivityTool.startActivity(this, intent);
        ActivityManager.getInstance().clearActivityExcept(MainActivity.class);
    }

    @Override
    public void onRegisterFailed(String message) {
        ToastManager.getInstance().toastShort(message);
    }

    @Override
    public void onRequestComplete() {
        setLoading(false);
    }
}
