package com.example.beijing_daxing.broadcast;

import static com.example.beijing_daxing.base.BaseActivity.url;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

import com.example.beijing_daxing.base.MyRecultCall;
import com.example.beijing_daxing.base.MyResult;
import com.example.beijing_daxing.been.RFIDLocation;
import com.example.beijing_daxing.been.Stock;
import com.example.beijing_daxing.uitls.SharedPreferencesUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.litepal.LitePal;

import java.util.List;

public class MyNetWorkReceiver extends BroadcastReceiver {
    private String wifiName = "";
    private Gson gson;

    public MyNetWorkReceiver() {
        gson=new Gson();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);

            Log.e("TAG", "正在连接:" + networkInfo.isConnectedOrConnecting());
            Log.e("TAG", "已连接:" + networkInfo.isConnected());
            if (networkInfo != null && networkInfo.isConnected()) {
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ssid = wifiInfo.getSSID();
                if (ssid != null) {
                    var newWifiNameState = ssid.equals("\"wang\"");
                    if (newWifiNameState && !wifiName.equals(ssid)) {
                        wifiName = ssid;
                        // 执行你的业务逻辑
                        Log.e("TAG", "onReceive:连接成功");
                        OkGo.<String>post(url + "/xj/selectLite")
                                .params("user", SharedPreferencesUtils.getstring("user", ""))
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(Response<String> response) {
                                        var body = response.body();
                                        if (!body.equals("null")){
                                            List<RFIDLocation>   list = gson.fromJson(body, new TypeToken<List<RFIDLocation>>() {}.getType());
                                            if (list!=null &&!list.isEmpty()){
                                                LitePal.deleteAll(RFIDLocation.class);
                                                LitePal.saveAll(list);
                                            }
                                        }
                                    }
                                });


                        return;
                    }

                }
            }
            Log.e("TAG", "onReceive:连接断开");
            wifiName = "";
        }

    }
}