package org.reflexfrp.reflexdom;

import android.app.Activity;
import android.os.Handler;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.nio.charset.StandardCharsets;

public class MainWidget {
  private static Object startMainWidget(Activity a, String url, long jsaddleCallbacks, final String initialJS) {
    CookieManager.setAcceptFileSchemeCookies(true); //TODO: Can we do this just for our own WebView?

    // Remove title and notification bars
    a.requestWindowFeature(Window.FEATURE_NO_TITLE);

    final WebView wv = new WebView(a);
    wv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    a.setContentView(wv);
    final WebSettings ws = wv.getSettings();
    ws.setJavaScriptEnabled(true);
    ws.setAllowFileAccessFromFileURLs(true);
    ws.setAllowUniversalAccessFromFileURLs(true);
    ws.setDomStorageEnabled(true);
    wv.setWebContentsDebuggingEnabled(true);
    // allow video to play without user interaction
    wv.getSettings().setMediaPlaybackRequiresUserGesture(false);

    wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView _view, String _url) {
          wv.evaluateJavascript(initialJS, null);
        }
      });

    wv.addJavascriptInterface(new JSaddleCallbacks(jsaddleCallbacks), "jsaddle");

    wv.loadUrl(url);

    final Handler hnd = new Handler();
    return new Object() {
      public final void evaluateJavascript(final byte[] js) {
        final String jsStr = new String(js, StandardCharsets.UTF_8);
        hnd.post(new Runnable() {
            @Override
            public void run() {
              wv.evaluateJavascript(jsStr, null);
            }
          });
      }
    };
  }

  private static class JSaddleCallbacks {
    private final long callbacks;
    private native void startProcessing(long callbacks);
    private native void processMessage(long callbacks, byte[] msg);
    private native byte[] processSyncMessage(long callbacks, byte[] msg);

    public JSaddleCallbacks(long _callbacks) {
      callbacks = _callbacks;
    }

    @JavascriptInterface
    public boolean postReady() {
      startProcessing(callbacks);
      return true;
    }

    @JavascriptInterface
    public boolean postMessage(final String msg) {
      processMessage(callbacks, msg.getBytes(StandardCharsets.UTF_8));
      return true;
    }

    @JavascriptInterface
    public String syncMessage(final String msg) {
      return new String(processSyncMessage(callbacks, msg.getBytes(StandardCharsets.UTF_8)),
                        StandardCharsets.UTF_8);
    }
  }
}

@@ -362,16 +362,182 @@
 wClient;
+%0Aimport android.webkit.WebChromeClient;%0Aimport android.webkit.PermissionRequest;%0Aimport android.net.Uri;%0Aimport android.annotation.TargetApi;%0Aimport android.os.Build;
 %0A%0Aimport
@@ -639,16 +639,22 @@
 nWidget(
+final 
 Activity
@@ -1637,24 +1637,781 @@
         %7D%0A  
+  %7D);%0A%0A    wv.setWebChromeClient(new WebChromeClient() %7B%0A        // Need to accept permissions to use the camera and audio%0A        @Override%0A        public void onPermissionRequest(final PermissionRequest request) %7B%0A            a.runOnUiThread(new Runnable() %7B%0A                @TargetApi(Build.VERSION_CODES.LOLLIPOP)%0A                @Override%0A                public void run() %7B%0A                    // Make sure the request is coming from the file system ...%0A                    if(request.getOrigin().toString().startsWith(%22file://%22)) %7B%0A                        request.grant(request.getResources());%0A                    %7D%0A                    else %7B%0A                        request.deny();%0A                    %7D%0A                %7D%0A            %7D);%0A        %7D%0A
     %7D);%0A%0A   

Handle onPermissionRequest