package com.example.beijing_daxing;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;

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

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;
import com.uhf.api.cls.Reader;
import com.xuexiang.xupdate.XUpdate;
import com.xuexiang.xupdate.easy.EasyUpdate;

import java.util.List;

public class MainActivity extends BaseActivity {
    private LoginVm loginVm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        loginVm = new LoginVm();
        boolean isRemember = SharedPreferencesUtils.getboolean("isRemember", false);
        if (isRemember) {
            loginVm.setName(SharedPreferencesUtils.getstring("user", ""));
            loginVm.setPass(SharedPreferencesUtils.getstring("pass", ""));
            loginVm.setRemember(isRemember);
        }
        binding.setUser(loginVm);
        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();
        if (pass == null || name == null || pass.isEmpty() || name.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 {
                            boolean remember = loginVm.isRemember();
                            if (remember) {
                                SharedPreferencesUtils.putboolean("isRemember", remember);
                                SharedPreferencesUtils.putstring("user", name);
                                SharedPreferencesUtils.putstring("pass", pass);
                                Intent intent = new Intent(MainActivity.this, HomePageActivity.class);
                                intent.putExtra("json", body);
                                intent.putExtra("user", name);
                                startActivity(intent);
                                finish();
                            }
                        }
                    }
                });
    }


}