package it.unipr.dsg.mobdev;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EditBookmarkActivity extends Activity {

	private TextView resultTextView = null;
	private EditText nameField = null;
	private EditText urlField = null;
	private Button saveButton;
	private int bookmarkPosition = -1;
	private Button removeButton; 

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(MainActivity.TAG, "onCreate()");
		setContentView(R.layout.edit_bookmark);

		Bundle bundle = this.getIntent().getExtras();
		bookmarkPosition = bundle.getInt("bookmarkPosition");
		
		resultTextView = (TextView)findViewById(R.id.bookmarkResultTextView);
		nameField = (EditText)findViewById(R.id.bookmarkField);
		urlField = (EditText)findViewById(R.id.urlField);
		saveButton = (Button)findViewById(R.id.saveButton);
		removeButton = (Button)findViewById(R.id.removeButton);
		
		if(bookmarkPosition >= 0)
		{

			BookmarkDescriptor bookmarkDescr = BookmarkManager.getInstance().getBookmarkList().get(bookmarkPosition);
			
			nameField.setText(bookmarkDescr.getName());
			urlField.setText(bookmarkDescr.getUrl());
			saveButton.setText("Save");
			
			saveButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Log.d(MainActivity.TAG, "AddButton -> onClick()");

					String bookmarkName = nameField.getText().toString();
					String url = urlField.getText().toString();

					if(bookmarkName != null && bookmarkName.length() > 0 && url != null && url.length() > 0)
					{
						Log.d(MainActivity.TAG, "Bookmark:["+bookmarkName+"@"+url+"]");
						resultTextView.setText("["+bookmarkName+"@"+url+"]");
						resultTextView.setTextColor(Color.GRAY);
						
						BookmarkManager.getInstance().replaceBookmark(bookmarkPosition,new BookmarkDescriptor(bookmarkName.trim(), url.trim()));
						
						finish();
					}
					else
					{
						Log.e(MainActivity.TAG, "Error ! Invalid Bookmark Name or Url ");
						resultTextView.setText("Error ! Invalid Bookmark Name or Url ");
						resultTextView.setTextColor(Color.RED);
					}
				}
			});
			
			removeButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					BookmarkManager.getInstance().removeBookmark(bookmarkPosition);
					finish();
				}
			});
		}
		else
		{
			resultTextView.setText("Bookmark Position ERROR !");
			nameField.setEnabled(false);
			urlField.setEnabled(false);
			saveButton.setEnabled(false);
		}

	}

	@Override
	protected void onDestroy() {
		Log.d(MainActivity.TAG, "onDestroy()");
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		Log.d(MainActivity.TAG, "onPause()");
		super.onPause();
	}

	@Override
	protected void onRestart() {
		Log.d(MainActivity.TAG, "onRestart()");
		super.onRestart();
	}

	@Override
	protected void onResume() {
		Log.d(MainActivity.TAG, "onResume()");
		super.onResume();
	}

	@Override
	protected void onStart() {
		Log.d(MainActivity.TAG, "onStart()");
		super.onStart();
	}

	@Override
	protected void onStop() {
		Log.d(MainActivity.TAG, "onStop()");
		super.onStop();
	}


}