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

package com.ntels.onecable.admin.common.mdns;


import com.ntels.onecable.admin.common.Constants;
import com.ntels.onecable.admin.common.util.LogUtil;
import com.ntels.onecable.admin.common.util.Util;
import com.ntels.onecable.admin.common.websocket.WebSocketService;

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.os.Handler;
import android.util.Log;

/**
 * <pre>
 * 1. ClassName :   MdnsRegBroadcastRecevier
 * 2. FileName  :   MdnsRegBroadcastRecevier.java
 * 3. Package   :   com.ntels.onecable.admin.common.mdns
 * 4. Date      :   2016-12-06
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class MdnsRegBroadcastRecevier extends BroadcastReceiver {

    private static final String TAG = MdnsRegBroadcastRecevier.class.getSimpleName();

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

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

        mContext = context;

        String strAction = intent.getAction();
        if (strAction.equals("android.intent.action.BOOT_COMPLETED")) {
            Log.i("BOOTSVC", "Intent received");
            mUtil.setPreferences(context, Constants.EXTRA_SSID, "");
            mUtil.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");
                    mUtil.setPreferences(context, Constants.EXTRA_SSID, "");
                    mUtil.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");
                    /*new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ComponentName cn = new ComponentName(mContext.getPackageName(), WebSocketService.class.getName());
                            ComponentName svcName = mContext.startService(new Intent().setComponent(cn));
                            if ( svcName == null ) {
                                Log.e(TAG, "Could not start WebSocketService " + cn.toString());
                            }
                        }
                    }, 5000);*/
                    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();
//            boolean bDisconnected = false;

            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());
//                        mContext.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");
//                        bDisconnected = true;
                    }
                } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                    Log.e("JINO TEST LOG", "JINO TEST LOG Mobile CONNECTED");
                } else {

                }

                /*if ( !bDisconnected ) {
                    ComponentName cn = new ComponentName(mContext.getPackageName(), WebSocketService.class.getName());
                    ComponentName svcName = mContext.startService(new Intent().setComponent(cn));
                    if ( svcName == null ) {
                        Log.e(TAG, "Could not start WebSocketService " + cn.toString());
                    }
                }*/
            }
        } else {

        }

        if (strAction.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            ConnectivityManager connectivityManager = (ConnectivityManager)
                    context.getSystemService(Context.CONNECTIVITY_SERVICE);
            boolean networkEnable = false;

            if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null) {
                if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
                    LogUtil.i(TAG, "WiFi network is connected!!");
                    networkEnable = true;
                }
            }
            if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null) {
                if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected()) {
                    LogUtil.i(TAG, "Mobile network is connected!!");
                    networkEnable = true;
                }
            }
            if (networkEnable) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ComponentName cn = new ComponentName(context.getPackageName(), WebSocketService.class.getName());
                        ComponentName svcName = context.startService(new Intent().setComponent(cn));
                        if (svcName == null) {
                            Log.e(TAG, "Could not start WebSocketService " + cn.toString());
                        }
                    }
                }, 5000);
            }
        }
    }
}
