package com.dskj.trustlend;

import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProviders;

import com.azhon.basic.base.BaseActivity;
import com.dskj.trustlend.api.Api;
import com.dskj.trustlend.api.BaseObserver;
import com.dskj.trustlend.api.Result;
import com.dskj.trustlend.bean.UserBean;
import com.dskj.trustlend.databinding.ActivityLoginBinding;
import com.dskj.trustlend.databinding.ActivityRegBinding;
import com.dskj.trustlend.home.MainViewModel;
import com.dskj.trustlend.utils.GsonUtils;
import com.dskj.trustlend.utils.ToastUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class RegActivity extends BaseActivity<MainViewModel, ActivityRegBinding> {

    @Override
    protected int initLayout() {
        return R.layout.activity_reg;
    }

    @Override
    protected void initView() {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);

    }

    @Override
    protected void initData() {
        dataBinding.loginTv.getPaint().setFlags(dataBinding.loginTv.getPaint().getFlags() | dataBinding.loginTv.getPaint().UNDERLINE_TEXT_FLAG);
        dataBinding.loginTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(RegActivity.this,LoginActivity.class));
                overridePendingTransition(0,0);
                finish();
            }
        });

        dataBinding.regBt.setOnClickListener(v -> {
            if(TextUtils.isEmpty(dataBinding.agentCodeTv.getText().toString())){
                ToastUtils.showShort(dataBinding.agentCodeTv,"Agent Code  cannot be Empty");
                return;
            }
            if(TextUtils.isEmpty(dataBinding.fullNameTv.getText().toString())){
                ToastUtils.showShort(dataBinding.agentCodeTv,"Full Name  cannot be Empty");
                return;
            }
            if(TextUtils.isEmpty(dataBinding.idNumberTv.getText().toString())){
                ToastUtils.showShort(dataBinding.agentCodeTv,"ID Number  cannot be Empty");
                return;
            }
            if(TextUtils.isEmpty(dataBinding.passTv.getText().toString())){
                ToastUtils.showShort(dataBinding.agentCodeTv,"Password  cannot be Empty");
                return;
            }
            if(TextUtils.isEmpty(dataBinding.passTv1.getText().toString())){
                ToastUtils.showShort(dataBinding.agentCodeTv,"Confirm Password cannot be Empty");
                return;
            }
            if(TextUtils.isEmpty(dataBinding.phoneTv.getText().toString())){
                ToastUtils.showShort(dataBinding.agentCodeTv,"Phone  cannot be Empty");
                return;
            }
            if(!dataBinding.passTv.getText().toString().equals(dataBinding.passTv1.getText().toString())){
                ToastUtils.showShort(dataBinding.agentCodeTv,"Password inconsistency");
                return;
            }
            toSumbit();
        });
    }

    private void toSumbit() {
        Api.getInstance().getLogin(dataBinding.areaTv.getText().toString()+dataBinding.phoneTv.getText().toString(), dataBinding.passTv.getText().toString(),
                        dataBinding.agentCodeTv.getText().toString().trim(),dataBinding.fullNameTv.getText().toString(),dataBinding.idNumberTv.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Result<UserBean>>() {

                    @Override
                    public void onSuccess(Result<UserBean> feedbackResp) {
                        if (feedbackResp.data != null) {
                            InitApp.setUserToken(feedbackResp.data.getAuth());
                            InitApp.saveString("user_bean", GsonUtils.beanToJSONString(feedbackResp.data));
                            startActivity(new Intent(RegActivity.this, MainActivity.class));
                            overridePendingTransition(0,0);
                            finish();
                        }
                    }

                    @Override
                    public void onError(int code, String msg) {
                        ToastUtils.showShort(dataBinding.loginBt, msg);
                    }
                });
    }

    @Override
    protected MainViewModel initViewModel() {
        return ViewModelProviders.of(this).get(MainViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }
}