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 AddBookmarkActivity extends Activity {

	private TextView resultTextView = null;
	private EditText nameField = null;
	private EditText urlField = null;

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

		resultTextView = (TextView)findViewById(R.id.bookmarkResultTextView);

		nameField = (EditText)findViewById(R.id.bookmarkField);
		urlField = (EditText)findViewById(R.id.urlField);

		//Get Drawable Object from Resources
		/*
		ImageView imageView = (ImageView)findViewById(R.id.imageView);
		Drawable drawable = getResources().getDrawable(R.drawable.bookmark_icon);
		imageView.setImageDrawable(drawable);
		
		//Get String from strings.xml
		getResources().getString(R.string.app_name);
		*/
		Button addButton = (Button)findViewById(R.id.addButton);
		
		addButton.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)
				{
					if(BookmarkManager.getInstance().addBookmark(new BookmarkDescriptor(bookmarkName.trim(), url.trim())) == 0)
					{
						Log.d(MainActivity.TAG, "Bookmark:["+bookmarkName+"@"+url+"]");
						resultTextView.setText("["+bookmarkName+"@"+url+"] Saved !");
						resultTextView.setTextColor(Color.GRAY);
						
						nameField.setText("");
						urlField.setText("");
					}
					else
					{
						Log.e(MainActivity.TAG, "Error ! Bookmark already in the list ! ");
						resultTextView.setText("Error ! Bookmark already in the list ! ");
						resultTextView.setTextColor(Color.RED);
					}
				}
				else
				{
					Log.e(MainActivity.TAG, "Error ! Invalid Bookmark Name or Url ");
					resultTextView.setText("Error ! Invalid Bookmark Name or Url ");
					resultTextView.setTextColor(Color.RED);
				}
			}
		});

	}

	@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();
	}


}