package com.wd.workoffice.ui.activity.mes;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseMesBean;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.MesLoginEvent;
import com.wd.workoffice.bean.mesBean.MesClientBean;
import com.wd.workoffice.presenter.LoginPresenter;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.MainActivity;
import com.wd.workoffice.ui.activity.login.LoginActivity;
import com.wd.workoffice.ui.fg.MsgListFragment;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/*
 * mes
 * 关于登录页
 * */
public class MesLoginActivity extends WorkToolBarActivity {

    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.ll_pwd)
    LinearLayout llPwd;
    @BindView(R.id.btn_login)
    Button btnLogin;
    private int type;
    private LoginPresenter loginPresenter;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        btnLogin.setEnabled(false);
    }

    @Override
    public void initData() {
    }

    @Override
    protected void initEvent() {
        etAccount.addTextChangedListener(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 (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etPassword.getText().toString())) {
                    btnLogin.setEnabled(true);
                } else {
                    btnLogin.setEnabled(false);
                }
            }
        });
        etPassword.addTextChangedListener(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 (!TextUtils.isEmpty(s) && !TextUtils.isEmpty(etAccount.getText().toString())) {
                    btnLogin.setEnabled(true);
                } else {
                    btnLogin.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_mes_login;
    }


    @OnClick(R.id.btn_login)
    public void onViewClicked() {
        String account = etAccount.getText().toString();
        String pwd = etPassword.getText().toString();
        RtfUtils.getMesRtf().login(account, pwd).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseMesBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseMesBean data) {
                hideLoading();
                if (!data.isSuccess()) {
                    toast(data.getMessage());
                    return;
                }
                JSONObject userData = JSON.parseObject(data.getData().toString());
                if (!TextUtils.isEmpty(UserKeeper.getInstance().getLoginInfo()) &&
                        !TextUtils.equals(UserKeeper.getInstance().getUserErpId(), userData.getString("userCode"))) {
                    toast("MES和BAT账号不属于同一个员工，请确认后在登录");
                    return;
                }
                toast("登录成功");
                UserKeeper.getInstance().keepMesLogin(true);
                UserKeeper.getInstance().keepMesLoginInfo(JSON.toJSONString(userData));
                Intent intent = new Intent(MesLoginActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
                EventBus.getDefault().post(new MesLoginEvent());
            }
        });
    }
}
