package thappstudios.android.miniapps.preferences;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.SwitchPreference;
import android.support.v4.preference.PreferenceFragment;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import thappstudios.android.miniapps.R;
import thappstudios.android.miniapps.apps.App;
import thappstudios.android.miniapps.menu.Menu;
import thappstudios.android.miniapps.service.AppService;
import thappstudios.android.miniapps.service.StopService;
import thappstudios.android.miniapps.service.WindowService;
import thappstudios.android.miniapps.util.SeekBarPreference;
import thappstudios.android.miniapps.util.SeekBarPreference.OnValueChangeListener;
import thappstudios.android.miniapps.util.Util;

public class PreferenceFragmentGeneral extends PreferenceFragment
		implements OnPreferenceChangeListener, OnValueChangeListener, OnPreferenceClickListener {

	private SwitchPreference mSwitchPreferenceRunning;
	private SeekBarPreference mSeekBarPreferenceActivationArea;
	private CheckBoxPreference mCheckBoxPreferenceRunning;
	private ListPreference mListPreferenceMenuSide;
	private Intent intentWindowService;
	private Preference mPreferenceHideNotification;

	private PreferenceManager mPreferenceManager;

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.prefs_general);
		intentWindowService = new Intent(getActivity(), WindowService.class);

		mPreferenceManager = new PreferenceManager(getActivity());

		if (Util.AndroidVersionMinICS())
			mSwitchPreferenceRunning = (SwitchPreference) findPreference(PreferenceManager.PREFERENCE_KEY_MENU_RUNNING);
		else
			mCheckBoxPreferenceRunning = (CheckBoxPreference) findPreference(
					PreferenceManager.PREFERENCE_KEY_MENU_RUNNING);

		if (mPreferenceManager.loadBoolean(StopService.STOP_FROM_NOTIFICATION)) {
			if (Util.AndroidVersionMinICS())
				mSwitchPreferenceRunning.setChecked(false);
			else
				mCheckBoxPreferenceRunning.setChecked(false);
			mPreferenceManager.saveBoolean(StopService.STOP_FROM_NOTIFICATION, false);

		}

		if (Util.AndroidVersionMinICS() && mSwitchPreferenceRunning != null)
			mSwitchPreferenceRunning.setOnPreferenceChangeListener(this);
		else
			mCheckBoxPreferenceRunning.setOnPreferenceChangeListener(this);

		if ((Util.AndroidVersionMinICS() && mSwitchPreferenceRunning != null && mSwitchPreferenceRunning.isChecked())
				|| (mCheckBoxPreferenceRunning != null && mCheckBoxPreferenceRunning.isChecked())) {
			startAll();

		}

		mSeekBarPreferenceActivationArea = (SeekBarPreference) findPreference(
				PreferenceManager.PREFERENCE_KEY_OVERLAY_WIDTH);
		mSeekBarPreferenceActivationArea.setOnValueChangeListener(this);

		mListPreferenceMenuSide = (ListPreference) findPreference(PreferenceManager.PREFERENCE_KEY_MENU_POSITION);
		mListPreferenceMenuSide.setOnPreferenceChangeListener(this);

		mPreferenceHideNotification = findPreference(PreferenceManager.PREFERENCE_KEY_HIDE_NOTIFICATION);
		mPreferenceHideNotification.setOnPreferenceChangeListener(this);

	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		switch (preference.getKey()) {
		case PreferenceManager.PREFERENCE_KEY_HIDE_NOTIFICATION:
			WindowService.getWindowService().updateNotification("", WindowService.NOTIFICATION_ID, -1);
			return true;
		}
		return false;
	}

	@Override
	public boolean onTouch(View view, MotionEvent event) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressLint("NewApi")
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		switch (preference.getKey()) {
		case PreferenceManager.PREFERENCE_KEY_MENU_RUNNING:
			boolean start = (Boolean) newValue;
			if (start)
				startAll();
			else if (start == false)
				stopAll();

			return true;
		case PreferenceManager.PREFERENCE_KEY_MENU_POSITION:
			ListPreference pref = (ListPreference) preference;
			if (pref != null) {
				String s = PreferenceManager.getDefaultSharedPreferences(getActivity())
						.getString(PreferenceManager.PREFERENCE_KEY_MENU_POSITION, "1");
				Util.getLayoutParams(Util.LAYOUT_WINDOW).gravity = s.equals("1") ? Gravity.LEFT : Gravity.RIGHT;
				Util.getLayoutParams(Util.LAYOUT_MENU).gravity = s.equals("1") ? Gravity.LEFT : Gravity.RIGHT;
				if (WindowService.getWindowService() != null && Menu.IS_OPEN == false)
					WindowService.getWindowService().updateOverlayView(Util.getLayoutParams(Util.LAYOUT_WINDOW));

				else if (Menu.IS_OPEN)
					WindowService.getWindowService().updateMenu(Util.getLayoutParams(Util.LAYOUT_MENU));

			}
			return true;

		case PreferenceManager.PREFERENCE_KEY_HIDE_NOTIFICATION:
			WindowService.getWindowService().updateNotification("", WindowService.NOTIFICATION_ID, -1);
			if ((Util.AndroidVersionMinICS() && mSwitchPreferenceRunning.isChecked())
					|| (Util.AndroidVersionMinICS() == false && mCheckBoxPreferenceRunning.isChecked())) {
				stopAll();
				startAll();
			}
			return true;

		}
		return false;
	}

	private void stopAll() {
		if (Menu.IS_OPEN)
			WindowService.getWindowService().closeMenu();

		if (AppService.APP_IS_OPEN && App.getApp() != null) {
			int size = AppService.started_apps.size();

			for (int i = 0; i < size; i++)
				AppService.destroy(AppService.uuids.get(i));

			AppService.APP_IS_OPEN = false;
		}
		Util.counter = 0;
		getActivity().stopService(intentWindowService);
		getActivity().stopService(new Intent(getActivity(), AppService.class));
	}

	private void startAll() {
		getActivity().startService(intentWindowService);
	}

	@SuppressLint("NewApi")
	@Override
	public void onValueChange(int value) {
		// TODO Auto-generated method stub

		if ((mSwitchPreferenceRunning != null && mSwitchPreferenceRunning.isChecked())
				|| (mCheckBoxPreferenceRunning != null && mCheckBoxPreferenceRunning.isChecked())) {
			WindowManager.LayoutParams params = Util.getLayoutParams(Util.LAYOUT_WINDOW);
			params.width = value;
			WindowService.getWindowService().updateOverlayView(params);
		}
	}

	public void resetSettings() {
		onValueChange(25);
		mSeekBarPreferenceActivationArea.onProgressChanged(mSeekBarPreferenceActivationArea.getSeekBar(), 25, false);
		mListPreferenceMenuSide.setValue("2");
	}
}
