package com.networkprofiles.widget;
/** Service to refresh the widget **/
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.graphics.Color;
import android.os.IBinder;
import android.provider.Settings;
import android.widget.RemoteViews;

import com.networkprofiles.R;
import com.networkprofiles.utils.MyNetControll;

public class NPWidgetService extends Service {

	private Boolean mobile;
	private Boolean cell;
	private Boolean gps;
	private Boolean display;
	private Boolean sound;
	private int [] widgetIds;
	private RemoteViews rv;
	
	private AppWidgetManager wm;
	private MyNetControll myController;
	
	
	public void onCreate() {
		wm = AppWidgetManager.getInstance(NPWidgetService.this);//this.getApplicationContext()
		myController = new MyNetControll(NPWidgetService.this);
		myController.myStart();
		//rv = new RemoteViews(getPackageName(), R.layout.npwidgetwifi);

	}
	
	public int onStartCommand(Intent intent, int flags, int startId) {
//		/** Check the state of the network devices and set the text for the text fields **/
//		//set the TextView for 3G data
//		if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED || telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTING) {
//			rv.setTextViewText(R.id.textMobile, "3G data is ON");
//		}
//		if(telephonyManager.getDataState() == TelephonyManager.DATA_DISCONNECTED) {
//			rv.setTextViewText(R.id.textMobile, "3G data is OFF");
//		}
//		//set the TextView for WiFi
//		if(wifim.getWifiState() == WifiManager.WIFI_STATE_ENABLED || wifim.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
//			rv.setTextViewText(R.id.textWifi, "Wifi is ON");
//		}
//		if(wifim.getWifiState() == WifiManager.WIFI_STATE_DISABLED || wifim.getWifiState() == WifiManager.WIFI_STATE_DISABLING) {
//			rv.setTextViewText(R.id.textWifi, "Wifi is OFF");
//		}
//		//set the TextView for Bluetooth
//		if(myBluetooth.getState() == BluetoothAdapter.STATE_ON || myBluetooth.getState() == BluetoothAdapter.STATE_TURNING_ON) {
//			rv.setTextViewText(R.id.textBluetooth, "Bluetooth is ON");
//		} 
//		if(myBluetooth.getState() == BluetoothAdapter.STATE_OFF || myBluetooth.getState() == BluetoothAdapter.STATE_TURNING_OFF) {
//			rv.setTextViewText(R.id.textBluetooth, "Bluetooth is Off");
//		}
//		//set the TextView for Cell voice
//		if(Settings.System.getInt(NPWidgetService.this.getContentResolver(),
//				Settings.System.AIRPLANE_MODE_ON, 0) == 0) {
//			rv.setTextViewText(R.id.textCell, "Cell is ON");
//		} else {
//			rv.setTextViewText(R.id.textCell, "Cell is OFF");
//		}
//		//set the TextView for GPS
//		locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
//		if(locationProviders.contains("gps")) {
//			rv.setTextViewText(R.id.textGps, "GPS is ON");
//		} else {
//			rv.setTextViewText(R.id.textGps, "GPS is OFF");
//		}
		
		/** Check which textView was clicked and switch the state of the corresponding device **/
		widgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
		mobile = intent.getBooleanExtra("mobile", false);
		cell = intent.getBooleanExtra("cell", false);
		gps = intent.getBooleanExtra("gps", false);
		display = intent.getBooleanExtra("display", false);
		sound = intent.getBooleanExtra("sound", false);
		
		if(widgetIds.length > 0) {
			if(mobile) {
				rv = new RemoteViews(getPackageName(), R.layout.npwidgetmobiledata);
				rv.setTextColor(R.id.textWidgetMobileData, Color.GREEN);
				updateWidgets();
				rv.setTextColor(R.id.textWidgetMobileData, Color.WHITE);
				myController.mobileOnOff();
			}
			if(cell) {
				rv = new RemoteViews(getPackageName(), R.layout.npwidgetcell);
				rv.setTextColor(R.id.textWidgetCell, Color.GREEN);
				
				if(Settings.System.getInt(NPWidgetService.this.getContentResolver(),
							Settings.System.AIRPLANE_MODE_ON, 0) == 0) {
					myController.cellOff();
				} else {
					myController.cellOn();
				}
				if(Settings.System.getInt(NPWidgetService.this.getContentResolver(),
							Settings.System.AIRPLANE_MODE_ON, 0) == 0) {
					rv.setTextViewText(R.id.textWidgetCell, "Cell ON");
				} else {
					rv.setTextViewText(R.id.textWidgetCell, "Cell OFF");
				}
				
				updateWidgets();
				rv.setTextColor(R.id.textWidgetCell, Color.WHITE);
			}
			if(gps) {
				rv = new RemoteViews(getPackageName(), R.layout.npwidgetgps);
				rv.setTextColor(R.id.textWidgetGps, Color.GREEN);
				updateWidgets();
				rv.setTextColor(R.id.textWidgetGps, Color.WHITE);
				myController.gpsOnOff();
			}
			if(display) {
				rv = new RemoteViews(getPackageName(), R.layout.npwidgetdisplay);
				rv.setTextColor(R.id.textWidgetDisplay, Color.GREEN);
				updateWidgets();
				rv.setTextColor(R.id.textWidgetDisplay, Color.WHITE);
				myController.displayOnOff();
			}
			if(sound) {
				rv = new RemoteViews(getPackageName(), R.layout.npwidgetsound);
				rv.setTextColor(R.id.textViewWidgetSound, Color.GREEN);
				updateWidgets();
				rv.setTextColor(R.id.textViewWidgetSound, Color.WHITE);
				myController.soundSettings();
			}
		}
		updateWidgets();
		stopSelf();
		return 1;
	}
	
	public void onStart(Intent intent, int startId) {
		
	}
	
	public IBinder onBind(Intent arg0) {
		return null;
	}
	
	private void updateWidgets() {
		for(int id : widgetIds) {
			wm.updateAppWidget(id, rv);
		}
	}
	
}//close class NPWidgetService