package it.unipr.mobdev;

import it.unipr.dsg.mobdev.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

@SuppressLint("SetJavaScriptEnabled")
public class BookmarkWebViewActivity extends Activity{

	private WebView mWebView = null;
	private String url = null;
	private Activity mContext = null;
	private ProgressBar progressBar = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		Log.d(MainActivity.TAG,"BookmarkWebViewActivity onCreate()");
		
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
	    requestWindowFeature(Window.FEATURE_PROGRESS);
		
		setContentView(R.layout.webview);
		
		Bundle bundle = this.getIntent().getExtras();
		url  = bundle.getString("url");
		
		if(url == null)
			url = "http://www.google.com";
		
		this.mContext = this;
		
		progressBar = (ProgressBar)findViewById(R.id.progressbar);
		progressBar.setProgress(0);
		progressBar.setVisibility(View.VISIBLE);
		
		//Retrive WebView Element
		mWebView  = (WebView) findViewById(R.id.webview);
		
		//Enable Javascript
		mWebView.getSettings().setJavaScriptEnabled(true);
		 
		//Set WebClient
		mWebView.setWebViewClient(new BookmarkWebViewClient());
		mWebView.setWebChromeClient(new BookmarkWebChromeClient());
		
		//Load WebPage
		mWebView.loadUrl(url);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
	        mWebView.goBack();
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
	private class BookmarkWebViewClient extends WebViewClient {
	    @Override
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {
	        view.loadUrl(url);
	        
	        progressBar.setProgress(0);
	        progressBar.setVisibility(View.VISIBLE);
	        
	        Log.d(MainActivity.TAG,"BookmarkWebViewClient ---> Loading Url: " + url);
	        
	        return true;
	    }
	    
	    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
	        Toast.makeText(mContext, "Oh no! " + description, Toast.LENGTH_SHORT).show();
	    }
	}
	
	private class BookmarkWebChromeClient extends WebChromeClient {
	    @Override
	    public void onProgressChanged(WebView view, int progress) {
	    	 Log.d(MainActivity.TAG,"BookmarkWebChromeClient ---> onProgressChanged: " + progress);
	    	  
	    	 progressBar.setProgress(progress);
             
	    	 if(progress == 100) {
	    		 progressBar.setVisibility(View.GONE);
             }
	    }

		@Override
		public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
			Log.d(MainActivity.TAG,"BookmarkWebChromeClient ---> onConsoleMessage: " + consoleMessage.message());
			return super.onConsoleMessage(consoleMessage);
		}

	    /*OLD METHOD THAT NOW IS DEPRECATED
		@Override
		public void onConsoleMessage(String message, int lineNumber,String sourceID) {
			super.onConsoleMessage(message, lineNumber, sourceID);
			Log.d(MainActivity.TAG,"BookmarkWebChromeClient ---> onConsoleMessage: " + message);
		}
		*/
	    
	}
}
