package com.dskj.trustlend;

import android.content.Intent;
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.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 LoginActivity extends BaseActivity<MainViewModel, ActivityLoginBinding> {

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

    @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.regBt.setOnClickListener(v -> {
            startActivity(new Intent(LoginActivity.this,RegActivity.class));
            overridePendingTransition(0,0);
            finish();
        });
        dataBinding.loginBt.setOnClickListener(v -> {

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

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

            Api.getInstance().getLoginById(dataBinding.idNumberTv.getText().toString(), dataBinding.passTv.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());
                                if(dataBinding.rememberMeCb.isChecked()){
                                    InitApp.saveString("idNumber",dataBinding.idNumberTv.getText().toString().trim());
                                    InitApp.saveString("pass_text",dataBinding.passTv.getText().toString().trim());
                                }
                                InitApp.saveBoolean("isRemember",dataBinding.rememberMeCb.isChecked());

                                InitApp.saveString("user_bean", GsonUtils.beanToJSONString(feedbackResp.data));
                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                                finish();
                            }
                        }

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

//            Api.getInstance().getLogin("+85210086", dataBinding.passTv.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());
//                                if(dataBinding.rememberMeCb.isChecked()){
//                                    InitApp.saveString("idNumber",dataBinding.idNumberTv.getText().toString().trim());
//                                    InitApp.saveString("pass_text",dataBinding.passTv.getText().toString().trim());
//                                }
//                                InitApp.saveString("user_bean", GsonUtils.beanToJSONString(feedbackResp.data));
//                                startActivity(new Intent(LoginActivity.this, MainActivity.class));
//                                finish();
//                            }
//                        }
//
//                        @Override
//                        public void onError(int code, String msg) {
//                            ToastUtils.showShort(dataBinding.loginBt, msg);
//                        }
//                    });
        });

        dataBinding.rememberMeCb.setChecked(InitApp.getBoolean("isRemember",false));
        if(dataBinding.rememberMeCb.isChecked()){
            dataBinding.idNumberTv.setText(InitApp.getString("idNumber",""));
            dataBinding.passTv.setText(InitApp.getString("pass_text",""));

        }
    }

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

    @Override
    protected void showError(Object obj) {

    }
}