package com.bjzc.bhzhgd.activity;


import android.annotation.SuppressLint;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.TextView;
import com.bjzc.bhzhgd.R;
import com.bjzc.bhzhgd.base.BaseActivity3;
import com.bjzc.bhzhgd.bean.PhoneInfo;
import com.bjzc.bhzhgd.bean.UserInfo;
import com.bjzc.bhzhgd.contans.Contans;
import com.bjzc.bhzhgd.utils.GsonUtils;
import com.bjzc.bhzhgd.utils.HttpListener;
import com.bjzc.bhzhgd.utils.NohttpUtils;
import com.bjzc.bhzhgd.utils.UiUtlis;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;
import java.util.ArrayList;
import java.util.List;

public class UserChangeActivity extends BaseActivity3 {

    TextView tvNfc;

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_change;
    }

    @Override
    public void initView() {
        initToolbar("用户切换", "", new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvNfc = findViewById(R.id.tv_nfc);
        initNFC();
    }

    @Override
    public void initData() {

    }


    @Override
    public void handleNfc(String result) {
        super.handleNfc(result);
        tvNfc.setText(result);
    }

    private void NfcOnlineLogin() {
        String json = getPhoneInfo();
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.LOGIN, RequestMethod.POST);
        request.setDefineRequestBodyForJson(json);
        NohttpUtils.getInstance().add(UserChangeActivity.this, 0, request, callback, true, true, "登录中...");
    }
    @SuppressLint("MissingPermission")
    public String getPhoneInfo() {
        String imei;
        TelephonyManager manager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        if (Build.VERSION.SDK_INT < 29) {
            imei = manager.getDeviceId();//IMEI
        } else {
            imei = Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);
        }
        String model = Build.MODEL;//手机型号
        String sysVersion = Build.VERSION.RELEASE;//系统版本
        String phonenum = manager.getLine1Number();//手机号码
        if (phonenum == null) {
            phonenum = "";
        }
        PhoneInfo info = new PhoneInfo();
        info.setTotal(1);
        List<PhoneInfo.UserInfo> list = new ArrayList<>();
        PhoneInfo.UserInfo userInfo = new PhoneInfo.UserInfo();
        userInfo.setImeinum(imei);
        userInfo.setSysversion(sysVersion);
        userInfo.setPhonemodel(model);
        if (phonenum.equals("")) {
        } else {
            userInfo.setPhonenum(phonenum.substring(3));
        }
        userInfo.setUsername("用户名");
        userInfo.setPassword("密码s");
        list.add(userInfo);
        info.setRows(list);
        String json = GsonUtils.GsonString(info);
        return json;
    }
    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {
            String json = response.get();
            UserInfo userInfo = GsonUtils.GsonToBean(json, UserInfo.class);
            if (userInfo != null) {
                UserInfo.User user = userInfo.getRows().get(0);
                String status = user.getStatus();
                String username = user.getUsernames();
                switch (status) {
                    case "0":
                        showToast("用户名或密码错误");
                        break;
                    case "1":
//                        SPUtils.put(context, "save", savePwUser.isChecked());
//                        SPUtils.put(context, Contans.USERID, etUse.getText().toString());
//                        SPUtils.put(context, Contans.USERPWD, etPwd.getText().toString());
//                        SPUtils.put(context, Contans.USERNAME, username);
//                        SPUtils.put(context, Contans.BZBH, user.getBzbh() == null ? "" : user.getBzbh());
//                        SPUtils.put(context, Contans.BZMC, user.getBzmc() == null ? "" : user.getBzmc());
//                        SPUtils.put(context, Contans.PERMISSIONSRESULT, user.getPermissionsResult() == null ? "" : user.getPermissionsResult());
//                        Intent intent = new Intent(context, MainActivityJava.class);
//                        startActivity(intent);
//                        finish();
                        break;
                    case "2":
                        showToast("登录异常...");
                        break;
                    case "3":
                        showToast("未激活或未授权...");
                        break;
                    case "4":
                        showToast("禁止登陆...");
                        break;
                }
            } else {
                showToast(UiUtlis.getString(context, R.string.data_error));
            }
        }

        @Override
        public void onFailed(int what, Response<String> response) {
        }
    };
}
