/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.kneet.v3.common.mdns;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.ntels.kneet.v3.common.Constants;
import com.ntels.kneet.v3.common.util.Util;

public class MdnsRegBroadcastRecevier extends BroadcastReceiver {

    Context mContext;
    Util util = new Util();
    /** 위치 클라인 트 */
    private GoogleApiClient mGoogleApiClient;

    @Override public void onReceive(Context context, Intent intent) {

        mContext = context;

        String strAction = intent.getAction();
        if ( strAction.equals("android.intent.action.BOOT_COMPLETED") ) {
            Log.i("BOOTSVC", "Intent received");
            util.setPreferences(context, Constants.EXTRA_SSID, "");
            util.setPreferences(context, Constants.EXTRA_IP, "");
        } else if ( strAction.equals(WifiManager.WIFI_STATE_CHANGED_ACTION) ) {
            int extraWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN);
            switch (extraWifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    Log.i("BOOTSVC", "WIFI STATE DISABLED");
                    util.setPreferences(context, Constants.EXTRA_SSID, "");
                    util.setPreferences(context, Constants.EXTRA_IP, "");
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    Log.i("BOOTSVC", "WIFI STATE DISABLING");
                    break;
                case WifiManager.WIFI_STATE_ENABLED:
                    Log.i("BOOTSVC", "WIFI STATE ENABLED");
                    break;
                case WifiManager.WIFI_STATE_ENABLING:
                    Log.i("BOOTSVC", "WIFI STATE ENABLING");
                    break;
                case WifiManager.WIFI_STATE_UNKNOWN:
                    Log.i("BOOTSVC", "WIFI STATE UNKNOWN");
                    break;
            }
        } else if ( strAction.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION) ) {
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = manager.getActiveNetworkInfo();

            if ( null != activeNetwork ) {
                if ( activeNetwork.getType() == ConnectivityManager.TYPE_WIFI ) {
                    if ( activeNetwork.getState() == NetworkInfo.State.CONNECTED ) {

                        Log.e("JINO TEST LOG", "JINO TEST LOG Network Connected");

                        ComponentName cn = new ComponentName(context.getPackageName(), MdnsRegService.class.getName());
//                        context.stopService(new Intent().setComponent(cn));
                        ComponentName svcName = context.startService(new Intent().setComponent(cn));
                        if ( svcName == null ) {
                            Log.e("BOOTSVC", "Could not start service " + cn.toString());
                        }
                    } else if ( activeNetwork.getState() == NetworkInfo.State.DISCONNECTED ) {
                        Log.e("JINO TEST LOG", "JINO TEST LOG Network DISCONNECTED");
                    }
                } else if ( activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE ) {
                    Log.e("JINO TEST LOG", "JINO TEST LOG Mobile CONNECTED");
                }
            }
        }
    }
}
