//**************************************************
//
//  Copyright 2012 SOASTA, Inc.
//
//  File:  AUTWebChromeClient.java
//  Contains the AUTWebChromeClient class.
//
//**************************************************

package com.soasta.android.touchtest.web;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class SimpleWebChromeClient extends WebChromeClient
{
  private MainActivity m_activity;
  private WebView m_webView;
  private static final String LOG_TAG = SimpleWebChromeClient.class.getSimpleName();

  public SimpleWebChromeClient(MainActivity activity, WebView webView)
  {
    m_activity = activity;
    m_webView = webView;
  }
  
  @Override
  public void onProgressChanged(WebView view, int progress)
  {
    // Convert from the WebChromeClient progress scale (0-100)
    // to the Activity progress scale (0-10000).
    m_activity.setProgress(progress * 100);
  }
  
  @Override
  public void onConsoleMessage(String message, int lineNumber, String sourceID) {
    Log.d(LOG_TAG, "[" + Thread.currentThread().getName() + "] " + m_webView + "(L" + lineNumber + ") :" + message);
  }

  @Override
  public boolean onConsoleMessage(ConsoleMessage cm) {
    Log.d(LOG_TAG, "[" + Thread.currentThread().getName() + "] " + m_webView + "(L" + cm.lineNumber() + ") :" + cm.message());
    return true;
  }

  @Override
  public boolean onJsAlert(final WebView view, final String url, final String message, final JsResult result)
  {
    new AlertDialog.Builder(m_activity)
      .setTitle("Alert")
      .setMessage(message)
      .setPositiveButton(android.R.string.ok,
        new AlertDialog.OnClickListener()
        {
          @Override
          public void onClick(DialogInterface dialog, int which)
          {
            result.confirm();
          }
        })
      .setCancelable(false)
      .create()
      .show();
    return true;
  }

  @Override
  public boolean onJsConfirm(final WebView view, final String url, final String message, final JsResult result)
  {
    new AlertDialog.Builder(m_activity)
    .setTitle("Confirm")
    .setMessage(message)
    .setPositiveButton(android.R.string.ok,
      new DialogInterface.OnClickListener()
      {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
          result.confirm();
        }
      })
    .setNegativeButton(android.R.string.cancel,
      new DialogInterface.OnClickListener()
      {
        @Override
        public void onClick(DialogInterface dialog, int which)
        {
          result.cancel();
        }
      })
    .create()
    .show();
    return true;
  }

  @Override
  public boolean onJsPrompt(final WebView view, final String url, final String message, final String defaultValue, final JsPromptResult result)
  {
    return false;
  }
}