package com.beloko.idtech;

import java.io.File;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class OptionsFragment extends Fragment{
	String LOG = "OptionsFragment";

	TextView basePathTextView;
	TextView musicPathTextView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);


	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mainView = inflater.inflate(R.layout.fragment_options, null);

		basePathTextView = (TextView)mainView.findViewById(R.id.base_path_textview);
		musicPathTextView = (TextView)mainView.findViewById(R.id.music_path_textview);
		
		basePathTextView.setText(AppSettings.belokoBaseDir);
		musicPathTextView.setText(AppSettings.musicBaseDir);
		
		Button chooseDir = (Button)mainView.findViewById(R.id.choose_base_button);
		chooseDir.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DirectoryChooserDialog directoryChooserDialog = 
						new DirectoryChooserDialog(getActivity(), 
								new DirectoryChooserDialog.ChosenDirectoryListener() 
						{
							@Override
							public void onChosenDir(String chosenDir) 
							{
								updateBaseDir(chosenDir);
							}
						}); 

				directoryChooserDialog.chooseDirectory(AppSettings.belokoBaseDir);
			}
		});

		Button chooseMusicDir = (Button)mainView.findViewById(R.id.choose_music_button);
		chooseMusicDir.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DirectoryChooserDialog directoryChooserDialog = 
						new DirectoryChooserDialog(getActivity(), 
								new DirectoryChooserDialog.ChosenDirectoryListener() 
						{
							@Override
							public void onChosenDir(String chosenDir) 
							{
								updateMusicDir(chosenDir);
							}
						}); 

				directoryChooserDialog.chooseDirectory(AppSettings.musicBaseDir);
			}
		});
		
		CheckBox vibrate = (CheckBox)mainView.findViewById(R.id.enable_vibrate_checkbox);
		vibrate.setChecked(AppSettings.vibrate);
		
		vibrate.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				AppSettings.vibrate = isChecked;
				AppSettings.setBoolOption(getActivity(), "vibrate", AppSettings.vibrate);
			}
		});

		return mainView;
	}

	private void updateBaseDir(String dir)
	{
		File fdir = new File(dir);

		if (!fdir.isDirectory())
		{
			showError(dir + " is not a directory");
			return;
		}

		if (!fdir.canWrite())
		{
			showError(dir + " is not a writable");
			return;
		}

		AppSettings.belokoBaseDir = dir;
		AppSettings.setStringOption(getActivity(), "base_path", AppSettings.belokoBaseDir);
		AppSettings.createDirectories();

		basePathTextView.setText(AppSettings.belokoBaseDir);
	}

	private void updateMusicDir(String dir)
	{
		File fdir = new File(dir);

		if (!fdir.isDirectory())
		{
			showError(dir + " is not a directory");
			return;
		}


		AppSettings.musicBaseDir = dir;
		AppSettings.setStringOption(getActivity(), "music_path", AppSettings.musicBaseDir);
		musicPathTextView.setText(AppSettings.musicBaseDir);
		CDAudioPlayer.initFiles(AppSettings.musicBaseDir);
	}
	
	private void showError(String error)
	{
		AlertDialog.Builder dialogBuilder = new Builder(getActivity());
		dialogBuilder.setTitle(error);
		dialogBuilder.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() 
		{
			@Override
			public void onClick(DialogInterface dialog, int which) 
			{

			}
		});

		final AlertDialog errdialog = dialogBuilder.create();
		errdialog.show();
	}

}
