package cn.dankal.yunlaboratory.ui.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import api.UserServiceFactory;
import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.DKUserManager;
import cn.dankal.basiclib.DankalApplication;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.mvp.BasePresenter;
import cn.dankal.basiclib.model.BaseModel;
import cn.dankal.basiclib.model.LoginModel;
import cn.dankal.basiclib.rx.MapFunc;
import cn.dankal.basiclib.rx.NormalSubscriber;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.yunlaboratory.R;
import cn.dankal.yunlaboratory.ui.widget.MainButton;
import io.reactivex.disposables.Disposable;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.iv_onback)
    ImageView ivOnback;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_login)
    MainButton btnLogin;

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

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

    @Override
    protected void initComponents() {


        tvTitle.setText("检测平台");

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (etAccount.getText().length() > 0 && etPwd.getText().length() > 0) {
                    btnLogin.setEnabled(true);
                } else {
                    btnLogin.setEnabled(false);
                }
            }
        };

        etAccount.addTextChangedListener(textWatcher);
        etPwd.addTextChangedListener(textWatcher);

        if (DankalApplication.isLogin()){
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }


    @OnClick({R.id.iv_onback, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_onback:

                break;
            case R.id.btn_login:

                String account = etAccount.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();


                if (TextUtils.isEmpty(account)) {
                    ToastUtils.showShort("请输入账号");
                    return;
                }

                if (TextUtils.isEmpty(pwd)) {
                    ToastUtils.showShort("请输入密码");
                    return;
                }


                UserServiceFactory.login(account, pwd).map(new MapFunc<>()).subscribe(new NormalSubscriber<BaseModel<LoginModel>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoadingDialog();
                    }

                    @Override
                    public void onNext(BaseModel<LoginModel> loginModelBaseModel) {

                        DKUserManager.saveUserInfo(loginModelBaseModel.getData());

                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        dismissLoadingDialog();
                    }
                });

                break;
        }
    }
}
