package com.dskj.daikuan.ui.activity;

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.daikuan.InitApp;
import com.dskj.daikuan.R;
import com.dskj.daikuan.api.Api;
import com.dskj.daikuan.api.BaseObserver;
import com.dskj.daikuan.api.Result;
import com.dskj.daikuan.bean.ErrorMesage;
import com.dskj.daikuan.bean.UserBean;
import com.dskj.daikuan.databinding.ActivityLoginBinding;
import com.dskj.daikuan.utils.GsonUtils;
import com.dskj.daikuan.utils.LogUtils;
import com.dskj.daikuan.utils.ToastUtils;
import com.dskj.daikuan.viewModel.home.MainViewModel;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 主页
 * @author 
 */
public class LoginActivity extends BaseActivity<MainViewModel, ActivityLoginBinding> {

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

    @Override
    protected void initView() {
        dataBinding.changeType.setText(Html.fromHtml("sign up"));

        viewModel.getErrorMesageMutableLiveData().observe(this,bean->{
            ToastUtils.showShort(dataBinding.loginBt,bean.errorMessage);
        });
        viewModel.getUserBeanMutableLiveData().observe(this,userBean -> {

        });

        dataBinding.changeType.setOnClickListener(view -> {
            if(dataBinding.invcodeEt.getVisibility() ==View.VISIBLE) {
                dataBinding.invcodeEt.setVisibility(View.GONE);
                dataBinding.passEt1.setVisibility(View.GONE);

                dataBinding.loginBt.setText("Login");
                dataBinding.changeType.setText(Html.fromHtml("sign up"));

            }else{
                dataBinding.invcodeEt.setVisibility(View.VISIBLE);
                dataBinding.passEt1.setVisibility(View.VISIBLE);

                dataBinding.loginBt.setText("register");
                dataBinding.changeType.setText(Html.fromHtml("sign in"));
            }
        });

        dataBinding.loginBt.setOnClickListener(view -> {

            if(TextUtils.isEmpty(dataBinding.nameEt.getText().toString())){
                ToastUtils.showShort(dataBinding.loginBt,"Account cannot be empty");
                return;
            }

            if(TextUtils.isEmpty(dataBinding.passEt.getText().toString())){
                ToastUtils.showShort(dataBinding.loginBt,"Password cannot be empty");
                return;
            }
            if(dataBinding.invcodeEt.getVisibility() ==View.VISIBLE){

                if(TextUtils.isEmpty(dataBinding.passEt1.getText().toString())){
                    ToastUtils.showShort(dataBinding.loginBt,"Confirm Password cannot be empty");
                    return;
                }

                if(TextUtils.isEmpty(dataBinding.invcodeEt.getText().toString())){
                    ToastUtils.showShort(dataBinding.loginBt,"Invitation code cannot be empty");
                    return;
                }

                if(!dataBinding.passEt1.getText().toString().trim().equals(dataBinding.passEt.getText().toString().trim())){
                    ToastUtils.showShort(dataBinding.loginBt,"Password inconsistency");

                    return;
                }

                Api.getInstance().getLogin(dataBinding.nameEt.getText().toString(), dataBinding.passEt.getText().toString(),dataBinding.invcodeEt.getText().toString().trim())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<>() {

                            @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(LoginActivity.this, ShenQingActivity.class));
                                    finish();
                                }
                            }

                            @Override
                            public void onError(int code, String msg) {
                                if (code == -1) {

                                    if (msg.equals("密码错误") || msg.equals("邀请码不存在")) {
                                        ToastUtils.showShort(dataBinding.loginBt, msg);
                                        return;
                                    }
                                    dataBinding.invcodeEt.setVisibility(View.VISIBLE);
                                    dataBinding.passEt1.setVisibility(View.VISIBLE);

                                    dataBinding.loginBt.setText("register");
                                    dataBinding.changeType.setText(Html.fromHtml("sign in"));
                                } else {
                                    ToastUtils.showShort(dataBinding.loginBt, msg);
                                }
                            }
                        });
            }else {
                Api.getInstance().getLogin(dataBinding.nameEt.getText().toString(), dataBinding.passEt.getText().toString())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new BaseObserver<>() {

                            @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(LoginActivity.this, ShenQingActivity.class));
                                    finish();
                                }
                            }

                            @Override
                            public void onError(int code, String msg) {
                                if (code == -1) {
                                    if (msg.equals("密码错误") || msg.equals("邀请码不存在")) {
                                        ToastUtils.showShort(dataBinding.loginBt, msg);
                                        return;
                                    }
                                    dataBinding.invcodeEt.setVisibility(View.VISIBLE);
                                    dataBinding.passEt1.setVisibility(View.VISIBLE);
                                    dataBinding.loginBt.setText("register");
                                    dataBinding.changeType.setText(Html.fromHtml("sign in"));
                                } else {
                                    ToastUtils.showShort(dataBinding.loginBt, msg);

                                }
                            }
                        });
            }

        });

        dataBinding.nameEt.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                dataBinding.nameEt.setBackgroundResource(R.drawable.inputbg_true);
            } else {
                dataBinding.nameEt.setBackgroundResource(R.drawable.inputbg_false);
            }
        });

        dataBinding.passEt.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                dataBinding.passEt.setBackgroundResource(R.drawable.inputbg_true);
            } else {
                dataBinding.passEt.setBackgroundResource(R.drawable.inputbg_false);
            }
        });

        dataBinding.invcodeEt.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                dataBinding.invcodeEt.setBackgroundResource(R.drawable.inputbg_true);
            } else {
                dataBinding.invcodeEt.setBackgroundResource(R.drawable.inputbg_false);
            }
        });

        dataBinding.passEt1.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                dataBinding.passEt1.setBackgroundResource(R.drawable.inputbg_true);
            } else {
                dataBinding.passEt1.setBackgroundResource(R.drawable.inputbg_false);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();

    }


    @Override
    protected void initData() {
        dataBinding.setModel(viewModel);
    }

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

    }

    @Override
    protected void showError(Object obj) {

    }


}
