package com.rdiot.yx485.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;

import com.rdiot.yx485.BaseActivity;
import com.rdiot.yx485.main.MainActivityTab;
import com.rdiot.yx485.R;
import com.rdiot.yx485.http.Url;
import com.rdiot.yx485.http.entity.BaseReplyBean;
import com.rdiot.yx485.login.model.LoginSmsBean;
import com.zcolin.frame.http.ZHttp;
import com.zcolin.frame.http.response.ZResponse;
import com.zcolin.frame.util.SpUtil;

import okhttp3.Response;

public class LoginsmsActivity extends BaseActivity {
    ImageView back;
    TextView nb,tv_get_sms_code;
    AppCompatEditText et_phone,et_code ;
    AppCompatButton btn_login;
    ProgressBar loading;
    CheckBox isxuanze;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置沉浸式状态栏
        setContentView(R.layout.fra_login_sms);
        initView();
    }
    private void initView(){
        back = findViewById(R.id.back);
        nb = findViewById(R.id.nb);
        tv_get_sms_code = findViewById(R.id.tv_get_sms_code);
        et_phone = findViewById(R.id.et_phone);
        et_code = findViewById(R.id.et_code);
        btn_login = findViewById(R.id.btn_login);
        loading = findViewById(R.id.loading);
        isxuanze = findViewById(R.id.isxuanze);
        btn_login.setOnClickListener(v -> {
            //登录
            loginsms();
        });

        tv_get_sms_code.setOnClickListener(v -> {
            //获取验证码
            getSmsCode();
        });
        back.setOnClickListener(v -> {
            //返回
            finish();
        });
        nb.setOnClickListener(v -> {
            //返回
            Intent intent = new Intent(this, LoginWithPwdActivity.class);
            startActivity(intent);
            finish();
        });

    }

    private void getSmsCode() {
        String phone = et_phone.getText().toString().trim();
        if (phone.isEmpty()) {
            //提示输入手机号
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!phone.matches("^1[3-9]\\d{9}$")) {
            //提示输入手机号
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        CodeRequestBean codeRequestBean = new CodeRequestBean();
        codeRequestBean.tel = phone;
        codeRequestBean.bzty=1;
        ZHttp.postJson(Url.LOGINCODE, codeRequestBean, new ZResponse<BaseReplyBean>() {
            @Override
            public void onSuccess(Response response, BaseReplyBean resObj) {
                if (resObj != null) {
                    Toast.makeText(LoginsmsActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void loginsms() {
        if(!isxuanze.isChecked()){
            Toast.makeText(LoginsmsActivity.this, "请勾选用户协议和隐私政策", Toast.LENGTH_SHORT).show();
            return;
        }
        String phone = et_phone.getText().toString().trim();
        String code = et_code.getText().toString().trim();
        if (phone.isEmpty() || code.isEmpty()) {
            //提示输入手机号
            Toast.makeText(this, "请输入手机号和验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        LoginSmsBean loginSmsBean = new LoginSmsBean();
        loginSmsBean.code= code;
        loginSmsBean.phone= phone;
        ZHttp.postJson(Url.LOGINSMS, loginSmsBean, new ZResponse<LoginReplyBean>() {
            @Override
            public void onSuccess(Response response, LoginReplyBean bean) {
                if (bean != null) {
                    if (bean != null && bean.data!= null) {
                        loading.setVisibility(View.GONE);
                        SpUtil.putString("access_token", bean.data.access_token);
                        SpUtil.putString("token_type", bean.data.token_type);
                        SpUtil.putBoolean("app_first_login", bean.data.app_first_login);
                        SpUtil.putInt("expires_at", bean.data.expires_at);
                        Intent intent=new Intent(LoginsmsActivity.this, MainActivityTab.class);
                        startActivity(intent);
                        finish();
                    } else {
                        loading.setVisibility(View.GONE);
                        Toast.makeText(LoginsmsActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}