package com.example.beijing_daxing;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableBoolean;

import com.example.beijing_daxing.base.BaseActivity;
import com.example.beijing_daxing.databinding.ActivityMainBinding;
import com.example.beijing_daxing.uitls.SharedPreferencesUtils;
import com.example.beijing_daxing.vm.LoginVm;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class MainActivity extends BaseActivity {
    private LoginVm loginVm;
    private ObservableBoolean isShow = new ObservableBoolean(false);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        loginVm = new LoginVm();
        String ipConfig = SharedPreferencesUtils.getstring("ipConfig", "119.45.202.115:8091");
        loginVm.setIpConfig(ipConfig);
        String epcConfig = SharedPreferencesUtils.getstring("epcConfig", "");
        loginVm.setEpcConfig(epcConfig);
        boolean isRemember = SharedPreferencesUtils.getboolean("isRemember", false);
        if (isRemember) {
            loginVm.setName(SharedPreferencesUtils.getstring("user", ""));
            loginVm.setPass(SharedPreferencesUtils.getstring("pass", ""));
            loginVm.setRemember(isRemember);
        }

        binding.setUser(loginVm);
        isShow.set(ipConfig.isEmpty() || epcConfig.isEmpty());
        binding.setIsShow(isShow);

        // EasyUpdate.checkUpdate(this,url + "/getVersion");
        // XUpdate.newBuild(this)
        //         .updateUrl(url + "/getVersion")
        //         .promptIgnoreDownloadError(true)
        //         .update();
    }

    public void login_click(View view) {
        String pass = loginVm.getPass();
        String name = loginVm.getName();
        String ipConfig = loginVm.getIpConfig();
        String epcConfig = loginVm.getEpcConfig();
        if (epcConfig.length()!=8){
            Toast.makeText(this, "请输入正确的EPC配置", Toast.LENGTH_SHORT).show();
            return;
        }
        setUrl(ipConfig);
        if (pass == null || name == null || pass.isEmpty() || name.isEmpty() || ipConfig.isEmpty() || epcConfig.isEmpty()) return;
        dialog.show();
        OkGo.<String>get(url + "/login/finduser").tag(this)
                .params("name", name)
                .params("pass", pass)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        dialog.dismiss();
                        String body = response.body();
                        if (body.equals("isEmpty")) {
                            Toast.makeText(MainActivity.this, "暂无权限", Toast.LENGTH_SHORT).show();
                        } else if (body.equals("Error")) {
                            Toast.makeText(MainActivity.this, "用户名密码错误", Toast.LENGTH_SHORT).show();
                        } else if (body.equals("ok")) {
                            boolean remember = loginVm.isRemember();
                            SharedPreferencesUtils.putstring("ipConfig", ipConfig);
                            SharedPreferencesUtils.putstring("epcConfig", epcConfig);
                            if (remember) {
                                SharedPreferencesUtils.putboolean("isRemember", remember);
                                SharedPreferencesUtils.putstring("user", name);
                                SharedPreferencesUtils.putstring("pass", pass);
                                Intent intent = new Intent(MainActivity.this, HomePageActivity.class);
                                intent.putExtra("epcConfig", epcConfig);
                                // intent.putExtra("user", name);
                                startActivity(intent);
                                finish();
                            }
                        }
                    }
                });
    }

    public void setting(View view) {
        isShow.set(!isShow.get());
    }

}