package com.nexstreaming.app.apis;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;

import com.nexstreaming.app.nexplayersample.R;
import com.nexstreaming.app.nexplayersample.R.id;
import com.nexstreaming.app.nexplayersample.R.layout;

public class DolbyDialog {
	
	private Activity		mActivity = null;
	private IDolbyListener	mListener = null;
	private AlertDialog		mDolbyDialog = null;

	private int mDolbyDAP = 0;
	private int mDolbyEndPoint = 0;
	private int mDolbyVirtualizer = 0;
    private int mDolbyEnhancementGain = 0;
	private int mDolbyOutputReferenceLevel = 0;
	private int mDolbyPresentationIndex = 0;
	private int mDolbyMainAssoPref = 0;

	private int mDolbyDAPOrg = 0;
	private int mDolbyEndPointOrg = 0;
	private int mDolbyVirtualizerOrg = 0;
	private int mDolbyEnhancementGainOrg = 0;
	private int mDolbyOutputReferenceLevelOrg = 0;
	private int mDolbyPresentationIndexOrg = 0;
	private int mDolbyMainAssoPrefOrg = 0;

	public final static int DOLBY_PROPERTY_DAP = 2201;
	public final static int DOLBY_PROPERTY_END_POINT = 2202;
	public final static int DOLBY_PROPERTY_VIRTUALIZER = 2203;
	public final static int DOLBY_PROPERTY_ENHANCEMENT_GAIN = 2204;
	public final static int DOLBY_PROPERTY_OUTPUT_REFERENCE_LEVEL = 2205;
	public final static int DOLBY_PROPERTY_PRESENTATION_INDEX = 2206;
	public final static int DOLBY_PROPERTY_MAIN_ASSO_PREF = 2207;

	private Spinner mDolbyDAPSpinner = null;
	private Spinner mDolbyEndpointSpinner = null;
	private Spinner mDolbyVirtualizerSpinner = null;
	private SeekBar mDolbyEnhancementGainSeekbar = null;
	private TextView mDolbyEnhancementGainTextView = null;
	private SeekBar mDolbyOutputReferenceLevelSeekbar = null;
	private TextView mDolbyOutputReferenceLevelTextView = null;
	private SeekBar mDolbyPresentationIndexSeekbar = null;
	private TextView mDolbyPresentationIndexTextView = null;
	private SeekBar mDolbyMainAssoPrefSeekbar = null;
	private TextView mDolbyMainAssoPrefTextView = null;

	public enum DOLBY_BUTTON {
		DOLBY_DAP,
		DOLBY_END_POINT,
		DOLBY_VIRTUALIZER,
		DOLBY_ENHANCEMENT_GAIN,
		DOLBY_OUTPUT_REFERENCE_LEVEL,
		DOLBY_PRESENTATION_INDEX,
		DOLBY_MAIN_ASSO_PREF
	}

	public interface IDolbyListener {
		void onDolbyDialogUpdated(DOLBY_BUTTON button, int value);
	}
	
	public DolbyDialog(Activity activity, IDolbyListener listener, int dap, int endPoint, int virtualizer, int enhancementGain, int outputReferenceLevel, int presentationIndex, int mainAssoPref) {
		mActivity = activity;
		mListener = listener;

		mDolbyDAPOrg = dap;
		mDolbyEndPointOrg = endPoint;
		mDolbyVirtualizerOrg = virtualizer;
		mDolbyEnhancementGainOrg = enhancementGain;
		mDolbyOutputReferenceLevelOrg = outputReferenceLevel;
		mDolbyPresentationIndexOrg = presentationIndex;
		mDolbyMainAssoPrefOrg = mainAssoPref;

		mDolbyDAP = mDolbyDAPOrg;
		mDolbyEndPoint = mDolbyEndPointOrg;
		mDolbyVirtualizer = mDolbyVirtualizerOrg;
		mDolbyEnhancementGain = mDolbyEnhancementGainOrg;
		mDolbyOutputReferenceLevel = mDolbyOutputReferenceLevelOrg;
		mDolbyPresentationIndex = mDolbyPresentationIndexOrg;
		mDolbyMainAssoPref = mDolbyMainAssoPrefOrg;
	}

	private AlertDialog createDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
		builder.setTitle(R.string.dolby_set_up);
		View view = View.inflate(mActivity, R.layout.dolby_dialog_layout, null);
		setupUIComponents(view);
		AlertDialog dialog = builder.setView(view).create();
		WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
		params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		dialog.getWindow().setAttributes(params);

		return dialog;
	}

	public void createAndShow() {
		if( mDolbyDialog == null ) {
			mDolbyDialog = createDialog();
		}

		if( !mDolbyDialog.isShowing() ) {
			updateUIComponents();
			mDolbyDialog.show();
		}
	}

	private void updateUIComponents() {
		mDolbyDAPSpinner.setSelection(mDolbyDAP);
		mDolbyEndpointSpinner.setSelection(mDolbyEndPoint-1);
		mDolbyVirtualizerSpinner.setSelection(mDolbyVirtualizer);
		mDolbyEnhancementGainSeekbar.setProgress(mDolbyEnhancementGain);
		mDolbyEnhancementGainTextView.setText(String.valueOf(mDolbyEnhancementGain));
		mDolbyOutputReferenceLevelSeekbar.setProgress(mDolbyOutputReferenceLevel+14);
		mDolbyOutputReferenceLevelTextView.setText(String.valueOf(mDolbyOutputReferenceLevel+14 == 0 ? "Default Value" : String.valueOf(mDolbyOutputReferenceLevel)));
		mDolbyPresentationIndexSeekbar.setProgress(mDolbyPresentationIndex);
		mDolbyPresentationIndexTextView.setText(mDolbyPresentationIndex == -1 ? "Default Value" : String.valueOf(mDolbyPresentationIndex));
		mDolbyMainAssoPrefSeekbar.setProgress(mDolbyMainAssoPref+32);
		mDolbyMainAssoPrefTextView.setText(String.valueOf(mDolbyMainAssoPref));
	}

	private void resetUIComponents() {
		mDolbyDAPSpinner.setSelection(mDolbyDAPOrg);
		mDolbyEndpointSpinner.setSelection(mDolbyEndPointOrg-1);
		mDolbyVirtualizerSpinner.setSelection(mDolbyVirtualizerOrg);
		mDolbyEnhancementGainSeekbar.setProgress(mDolbyEnhancementGainOrg);
		mDolbyEnhancementGainTextView.setText(String.valueOf(mDolbyEnhancementGainOrg));
		mDolbyOutputReferenceLevelSeekbar.setProgress(mDolbyOutputReferenceLevelOrg+14);
		mDolbyOutputReferenceLevelTextView.setText(String.valueOf(mDolbyOutputReferenceLevelOrg+14 == 0 ? "Default Value" : String.valueOf(mDolbyOutputReferenceLevelOrg)));
		mDolbyPresentationIndexSeekbar.setProgress(mDolbyPresentationIndexOrg);
		mDolbyPresentationIndexTextView.setText(mDolbyPresentationIndexOrg == -1 ? "Default Value" : String.valueOf(mDolbyPresentationIndexOrg));
		mDolbyMainAssoPrefSeekbar.setProgress(mDolbyMainAssoPrefOrg+32);
		mDolbyMainAssoPrefTextView.setText(String.valueOf(mDolbyMainAssoPrefOrg));
	}

	private void setupUIComponents(View view) {
		ArrayAdapter<String> adapterDAP = new ArrayAdapter<String>(mActivity,
				layout.simple_spinner_item, id.textview, mActivity.getResources().getStringArray(R.array.pref_dolby_dap_entries));
		mDolbyDAPSpinner = (Spinner)view.findViewById(id.dolby_dap_spinner);
		mDolbyDAPSpinner.setAdapter(adapterDAP);
		mDolbyDAPSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		ArrayAdapter<String> adapterEndPoint = new ArrayAdapter<String>(mActivity,
				layout.simple_spinner_item, id.textview, mActivity.getResources().getStringArray(R.array.pref_dolby_end_point_entries));
		mDolbyEndpointSpinner = (Spinner)view.findViewById(id.dolby_end_point_spinner);
		mDolbyEndpointSpinner.setAdapter(adapterEndPoint);
		mDolbyEndpointSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		ArrayAdapter<String> adapterVirtualizer = new ArrayAdapter<String>(mActivity,
				layout.simple_spinner_item, id.textview, mActivity.getResources().getStringArray(R.array.pref_dolby_virtualizer_entries));
		mDolbyVirtualizerSpinner = (Spinner)view.findViewById(id.dolby_virtualizer_spinner);
		mDolbyVirtualizerSpinner.setAdapter(adapterVirtualizer);
		mDolbyVirtualizerSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		mDolbyEnhancementGainTextView = (TextView)view.findViewById(id.dolby_enhancement_gain_value);
		mDolbyEnhancementGainSeekbar = (SeekBar)view.findViewById(id.dolby_enhancement_gain_seekbar);
		mDolbyEnhancementGainSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				mDolbyEnhancementGainTextView.setText(String.valueOf(progress));
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});

		mDolbyOutputReferenceLevelTextView = (TextView)view.findViewById(id.dolby_output_reference_level_value);
		mDolbyOutputReferenceLevelSeekbar = (SeekBar)view.findViewById(id.dolby_output_reference_level_seekbar);
		mDolbyOutputReferenceLevelSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				mDolbyOutputReferenceLevelTextView.setText(progress == 0 ? "Default Value" : String.valueOf(progress-14));
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});

		mDolbyPresentationIndexTextView = (TextView)view.findViewById(id.dolby_presentation_index_value);
		mDolbyPresentationIndexSeekbar = (SeekBar)view.findViewById(id.dolby_presentation_index_seekbar);
		mDolbyPresentationIndexSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				mDolbyPresentationIndexTextView.setText(progress == 0 ? "Default Value" : String.valueOf(progress-1));
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});

		mDolbyMainAssoPrefTextView = (TextView)view.findViewById(id.dolby_main_asso_pref_value);
		mDolbyMainAssoPrefSeekbar = (SeekBar)view.findViewById(id.dolby_main_asso_pref_seekbar);
		mDolbyMainAssoPrefSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				mDolbyMainAssoPrefTextView.setText(String.valueOf(progress-32));
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
		});


		Button resetButton = (Button)view.findViewById(id.dolby_reset_button);
		resetButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				resetUIComponents();
			}
		});

		Button saveButton = (Button)view.findViewById(id.dolby_save_button);
		saveButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				mDolbyDAP = mDolbyDAPSpinner.getSelectedItemPosition();
				mDolbyEndPoint = mDolbyEndpointSpinner.getSelectedItemPosition() + 1;
				mDolbyVirtualizer = mDolbyVirtualizerSpinner.getSelectedItemPosition();
				mDolbyEnhancementGain = mDolbyEnhancementGainSeekbar.getProgress();

				mDolbyOutputReferenceLevel = mDolbyOutputReferenceLevelSeekbar.getProgress() - 14;
				mDolbyMainAssoPref = mDolbyMainAssoPrefSeekbar.getProgress() - 32;

				int progressValue = mDolbyPresentationIndexSeekbar.getProgress();
				mDolbyPresentationIndex = progressValue == 0 ? 0xffff : progressValue - 1;

				if( mListener != null ) {
					mListener.onDolbyDialogUpdated(DOLBY_BUTTON.DOLBY_DAP, mDolbyDAP);
					mListener.onDolbyDialogUpdated(DOLBY_BUTTON.DOLBY_END_POINT, mDolbyEndPoint);
					mListener.onDolbyDialogUpdated(DOLBY_BUTTON.DOLBY_VIRTUALIZER, mDolbyVirtualizer);
					mListener.onDolbyDialogUpdated(DOLBY_BUTTON.DOLBY_ENHANCEMENT_GAIN, mDolbyEnhancementGain);
					mListener.onDolbyDialogUpdated(DOLBY_BUTTON.DOLBY_OUTPUT_REFERENCE_LEVEL, mDolbyOutputReferenceLevel);
					mListener.onDolbyDialogUpdated(DOLBY_BUTTON.DOLBY_PRESENTATION_INDEX, mDolbyPresentationIndex);
					mListener.onDolbyDialogUpdated(DOLBY_BUTTON.DOLBY_MAIN_ASSO_PREF, mDolbyMainAssoPref);
				}
				dismiss();
			}
		});
	}

	public void dismiss() {
		if( (mDolbyDialog!=null) &&
				(mDolbyDialog.isShowing() == true) ) {
			updateUIComponents();
			mDolbyDialog.dismiss();
		}
	}
}
