package fsenablers.android.settings;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import fsenablers.android.R;

import fsenablers.android.activity.Constants;
import fsenablers.android.activity.WebAppActivity;

public class EditSettingsDialogFragment extends DialogFragment {
	
	// private EditText editTextDocRootUrl;
	Spinner spinnerDocRootUrl;
	private WebAppActivity mActivity;
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		// Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        
     // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        View v = inflater.inflate(R.layout.dialog_edit_settings, null);
        

        
        spinnerDocRootUrl = (Spinner) v.findViewById(R.id.settings_docRootUrlSpinner );
        
        
        // Create an ArrayAdapter using the docRootUrlList and a default spinner layout
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>( getActivity(), android.R.layout.simple_spinner_item, mActivity.getDocRootList() );

              
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinnerDocRootUrl.setAdapter(adapter);  
        
        int currentPosition = adapter.getPosition( getDocRootUrl() );
        if( currentPosition >= 0 ) {
        	spinnerDocRootUrl.setSelection( currentPosition );
        }
        
        builder.setView( v )
        
        // Add action buttons
           .setPositiveButton( "Save", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
            	   EditSettingsDialogFragment.this.save();
               }
           })
           .setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
            	   EditSettingsDialogFragment.this.cancel();
               }
           });      
        
        // Create the AlertDialog object and return it
        return builder.create();	
		
		

        
	}
	
	public void onAttach( Activity a ) {
		super.onAttach( a );
		mActivity = (WebAppActivity)a;
	}
	
	
	private void cancel() {
		getDialog().cancel();
		mActivity.onFinishEditSettings( false, null );
	}
	private void save() {

		String sUrl = spinnerDocRootUrl.getSelectedItem().toString();
		mActivity.onFinishEditSettings( true, sUrl );
	      
		
	}
	
	private String getDocRootUrl() {
		// Restore preferences

        SharedPreferences settings = getActivity().getSharedPreferences( Constants.PREFERENCES_SETTINGS, 0);
        
        
        String sDocRootUrl = settings.getString( Constants.SETTINGS_DOCROOT_URI, Constants.DEFAULT_DOCROOT_URI );
        
        return sDocRootUrl;		
		
	}

	
	
}
