package com.decoding.twodtg.demobarcodereader2dtg;

import java.util.List;

import decoding.twodtg.com.twodtgdecodelib2.BarcodeDecoder;
import decoding.twodtg.com.twodtgdecodelib2.BarcodeDecoderLibrary;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceGroup;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

public class PrefsActivity extends PreferenceActivity implements Preference.OnPreferenceClickListener
{
	private CheckBoxPreference GS1;
	private CheckBoxPreference PharmaCode;
	private CheckBoxPreference PDF417;
	private MyListPreference ShowResult;
	private CheckBoxPreference useImager;
	private BarcodeDecoderLibrary lib = new BarcodeDecoderLibrary();
	private boolean useDMProtection = false;
	private CheckBoxPreference activated;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
    	super.onCreate(savedInstanceState);
    	addPreferencesFromResource(R.xml.prefs);

		GS1 = (CheckBoxPreference) findPreference("GS1");
		GS1.setOnPreferenceClickListener(this);
		PharmaCode = (CheckBoxPreference) findPreference("PharmaCode");
		PharmaCode.setOnPreferenceClickListener(this);
		PDF417 = (CheckBoxPreference) findPreference("PDF417");
		PDF417.setOnPreferenceClickListener(this);
		activated = (CheckBoxPreference) findPreference("Activated");
		activated.setOnPreferenceClickListener(this);

		useImager = (CheckBoxPreference) findPreference("UseImagerIfExistsAsVideoSource");
		useImager.setOnPreferenceClickListener(this);

		activated.setChecked(lib.isActivated(this));
		ShowResult = (MyListPreference)findPreference("ShowResult");

		BarcodeDecoderLibrary decLib = new BarcodeDecoderLibrary();
		useDMProtection = decLib.DMProtectionSupported();
		if(!useDMProtection)
		{
			// "Mobile Data Matrix Security Decoder" not supported.
			// Hide unused settings
			PreferenceCategory mCategory = (PreferenceCategory) findPreference("Protection");
			if(mCategory != null)
			{
				PreferenceScreen screen = getPreferenceScreen();
				if(screen != null)
					screen.removePreference(mCategory);
			}
		}
		if(ShowResult != null)
		{
			final Activity a=this;
			ShowResult.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
			{
				@Override
				public boolean onPreferenceChange(Preference preference, Object newValue)
				{
					if(newValue.equals("BLT_SERIAL"))
					{
						Intent intent=new Intent(a, BltDiscoveryActivity.class);
						startActivity(intent);
					}
					else if(newValue.equals("BLT_SERIAL_KEY"))
					{
						Intent intent=new Intent(a, BltDiscoveryActivity.class);
						Bundle b = new Bundle();
						b.putBoolean("isKEYBRD", true);
						intent.putExtras(b);
						startActivity(intent);
					}
					return true;
				}
			});
		}

		SetCameraResolutions();
	}

	private void SetCameraResolutions()
	{
		if(!BarcodeDecoder.isStandardCamera() && useImager.isChecked())
		{
			String[] res = new String[1];
			res[0] = "Not supported";
			ListPreference lp = (ListPreference)findPreference("FrameSize");
			lp.setEntries(res);
			lp.setEntryValues(res);
			return;
		}

		try
		{
			int camId = 0;
			int sizeId = -1;
			String[] res = new String[1];
			res[0] = "Fixed";
			//if(BarcodeDecoder.isStandardCamera())
			{
				int count = Camera.getNumberOfCameras();
				if (count > 0) {
					CameraInfo cameraInfo = new CameraInfo();
					for (int i = 0; i < count; i++) {
						Camera.getCameraInfo(i, cameraInfo);
						if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
							camId = i;
							break;
						}
					}
					Camera cam = Camera.open(camId);
					Parameters param = cam.getParameters();
					List<Camera.Size> previewSize = param.getSupportedPreviewSizes();
					res = new String[previewSize.size()];
					for (int i = 0; i < previewSize.size(); i++)
					{
						Camera.Size it = previewSize.get(i);
						String str = it.width + "x" + it.height;
						res[i] = str;
						if((it.width == 640 && it.height == 480) || (it.width == 480 && it.height == 640))
							sizeId = i;
					}
					cam.release();
					cam = null;
				}
			}
			ListPreference lp = (ListPreference)findPreference("FrameSize");
			lp.setEntries(res);
			lp.setEntryValues(res);
			if(sizeId >= 0 && lp.getValue() == null)
				lp.setValueIndex(sizeId);
			else
				lp.setValue(lp.getValue());
		}
		catch(Exception e)
		{
			Utils.Failed(e);
		}
	}
	
	private void setDefaultKeyValue(String key, String value)
	{
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		String str = sharedPref.getString(key, "");
		if(str == null || str.length() == 0)
		{
			SharedPreferences.Editor editor = sharedPref.edit();
			editor.putString(key, value);
			editor.commit();
		}
	}

  public boolean onPreferenceClick(Preference preference)
  {
	  if(useImager == preference)
		{
			SetCameraResolutions();
			return true;
		}
		else if(!lib.isIndastrialVersion())
	  {
			GS1.setChecked(false);
			PharmaCode.setChecked(false);
			PDF417.setChecked(false);
			Intent intent = new Intent(this, ToSiteActivity.class);
			startActivity(intent);
			return true;
	  }
		else if(activated == preference)
		{
			if(lib.isActivated(this))
				DeactivationActivity.ToOnlineDeactivation(this);
			else
				ActivationActivity.ToOnlineActivation(this);
			return true;
		}
		return false;
  }
}


