package com.yutou.jianr_mg.views;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.yutou.jianr_mg.Interfaces.HttpInterface;
import com.yutou.jianr_mg.LoadingActivity;
import com.yutou.jianr_mg.Network.HttpApi;
import com.yutou.jianr_mg.Network.HttpUtils;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.ActivitysManager;
import com.yutou.jianr_mg.Tools.Utils;

import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView textView2;
    private EditText userName;
    private EditText editText2;
    private EditText password;
    private EditText rePassword;
    private EditText email;
    private Button login;
    private Button register;
    private ImageView ovo;

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

    private void initView() {
        textView2 = findViewById(R.id.textView2);
        userName = findViewById(R.id.userName);
        editText2 = findViewById(R.id.editText2);
        password = findViewById(R.id.password);
        rePassword = findViewById(R.id.rePassword);
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        ovo = findViewById(R.id.ovo);
        email = findViewById(R.id.email);


        login.setOnClickListener(this);
        register.setOnClickListener(this);
        ovo.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login:
                finish();
                break;
            case R.id.register:
                if(submit()){
                    try {
                        JSONObject json = new JSONObject();
                        json.put("uname",userName.getText().toString());
                        json.put("name",editText2.getText().toString());
                        json.put("pass",password.getText().toString());
                        json.put("email",email.getText().toString());
                        HttpUtils.post(HttpApi.HOME_URL+HttpApi.USER_REG, json, new HttpInterface() {
                            @Override
                            public void httpGetData(String string, int code) {
                                try {
                                    JSONObject json=new JSONObject(string);
                                    if(json.getInt("code")==200){
                                        ActivitysManager.finishActivity(LoadingActivity.class);
                                        Intent intent=new Intent(RegisterActivity.this, LoadingActivity.class);
                                        intent.putExtra("name",userName.getText().toString());
                                        intent.putExtra("password",password.getText().toString());
                                        startActivity(intent);
                                        finish();
                                    }else{
                                        Utils.toast(RegisterActivity.this,json.getString("msg"));
                                    }
                                }catch (Exception e){
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void httpError(Exception e) {
                                Utils.toast(RegisterActivity.this,e.getMessage());
                            }
                        });
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.ovo:
                Utils.toast(getApplicationContext(),"OvO?");
                break;
        }

    }

    private boolean submit() {
        // validate
        String userNameString = userName.getText().toString().trim();
        if (TextUtils.isEmpty(userNameString)) {
            Toast.makeText(this, "登录账号（字母+数字）", Toast.LENGTH_SHORT).show();
            return false;
        }

        String editText2String = editText2.getText().toString().trim();
        if (TextUtils.isEmpty(editText2String)) {
            Toast.makeText(this, "用户名（可中文）", Toast.LENGTH_SHORT).show();
            return false;
        }

        String passwordString = password.getText().toString().trim();
        if (TextUtils.isEmpty(passwordString)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return false;
        }

        String rePasswordString = rePassword.getText().toString().trim();
        if (TextUtils.isEmpty(rePasswordString)) {
            Toast.makeText(this, "再输入一次密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(!passwordString.equals(rePasswordString)){
            Toast.makeText(this, "密码不一致", Toast.LENGTH_SHORT).show();
            return false;
        }
        if(TextUtils.isEmpty(email.getText().toString().toLowerCase())){
            Toast.makeText(this, "请输入邮箱", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
        // TODO validate success, do something
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}
