package com.urbanairship.richpush.sample;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.HttpAuthHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.actionbarsherlock.app.SherlockFragment;
import com.urbanairship.UAirship;
import com.urbanairship.richpush.RichPushManager;
import com.urbanairship.richpush.RichPushMessageJavaScript;
import com.urbanairship.richpush.RichPushUser;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

@SuppressLint("SetJavaScriptEnabled")
public class MessageFragment extends SherlockFragment {

    private static final String MESSAGE_ID_KEY = "com.urbanairship.richpush.URL_KEY";

    WebView browser;

    public static MessageFragment newInstance(String messageId) {
        MessageFragment message = new MessageFragment();
        Bundle arguments = new Bundle();
        arguments.putString(MESSAGE_ID_KEY, messageId);
        message.setArguments(arguments);
        return message;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.browser = new WebView(container.getContext());
        this.browser.setLayoutParams(container.getLayoutParams());
        return this.browser;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        this.initializeBrowser();
        this.loadMessage();
    }

    // public api

    public String getMessageId() {
        return this.getArguments().getString(MESSAGE_ID_KEY);
    }

    public void show(FragmentManager manager, int layoutId, String tag) {
        manager.beginTransaction().add(layoutId, this, tag).commit();
    }

    public void dismiss() {
        this.getSherlockActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
    }

    public void loadMessage() {
        this.browser.loadUrl(RichPushManager.shared().getRichPushUser().getInbox().getMessageUrl(this.getMessageId()));
    }

    // helpers

    @SuppressWarnings("unchecked")
    private void initializeBrowser() {
        this.browser.getSettings().setJavaScriptEnabled(true);
        this.browser.getSettings().setDomStorageEnabled(true);
        this.browser.getSettings().setAppCacheEnabled(true);
        this.browser.getSettings().setAllowFileAccess(true);
        this.browser.getSettings().setAppCacheMaxSize(1024*1024*8);
        this.browser.getSettings().setAppCachePath(UAirship.shared().getApplicationContext().getCacheDir().getAbsolutePath());
        this.browser.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        this.browser.setWebChromeClient(new WebChromeClient());

        this.browser.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
                RichPushUser user = RichPushManager.shared().getRichPushUser();
                handler.proceed(user.getId(), user.getPassword());
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                MessageFragment.this.browser.loadUrl(url);
                return true;
            }

        });

        Class<? extends RichPushMessageJavaScript> jsInterfaceClass = RichPushManager.getJsInterface();
        if (jsInterfaceClass != null) {
            try {
                Constructor<RichPushMessageJavaScript> constructor =
                        (Constructor<RichPushMessageJavaScript>)jsInterfaceClass.getConstructor(View.class,
                                String.class);
                RichPushMessageJavaScript jsInterface = constructor.newInstance(this.browser,
                        this.getArguments().getString(MESSAGE_ID_KEY));
                this.browser.addJavascriptInterface(jsInterface, RichPushManager.getJsIdentifier());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (java.lang.InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

    }

}

@@ -36,16 +36,107 @@
 ample;%0A%0A
+import java.lang.reflect.Constructor;%0Aimport java.lang.reflect.InvocationTargetException;%0A%0A
 import a
@@ -163,24 +163,24 @@
 ppressLint;%0A
-
 import andro
@@ -510,24 +510,25 @@
 ViewClient;%0A
+%0A
 import com.a
@@ -562,24 +562,56 @@
 ckFragment;%0A
+import com.urbanairship.Logger;%0A
 import com.u
@@ -632,16 +632,64 @@
 irship;%0A
+import com.urbanairship.richpush.RichPushInbox;%0A
 import c
@@ -726,24 +726,74 @@
 ushManager;%0A
+import com.urbanairship.richpush.RichPushMessage;%0A
 import com.u
@@ -840,16 +840,16 @@
 Script;%0A
+
 import c
@@ -892,99 +892,8 @@
 r;%0A%0A
-import java.lang.reflect.Constructor;%0Aimport java.lang.reflect.InvocationTargetException;%0A%0A
 @Sup
@@ -2322,119 +2322,416 @@
     
-this.browser.loadUrl(RichPushManager.shared().getRichPushUser().getInbox().getMessageUrl(this.getMessageId()));
+String id = this.getMessageId();%0A        RichPushInbox inbox = RichPushManager.shared().getRichPushUser().getInbox();%0A        RichPushMessage message = inbox.getMessage(id);%0A        if (message != null) %7B%0A            String bodyUrl = message.getMessageBodyUrl();%0A            this.browser.loadUrl(bodyUrl);%0A        %7D%0A        else %7B%0A            Logger.info(%22Couldn't retrieve message for ID: %22 + id);%0A        %7D
 %0A   

load message body url from model object