package triaina.webview;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.inject.Inject;

import triaina.commons.utils.SystemUtils;
import triaina.injector.TriainaEnvironment;
import triaina.injector.fragment.TriainaFragment;
import triaina.webview.annotation.Bridge;
import triaina.webview.config.WebViewBridgeConfigurator;
import triaina.webview.entity.Params;
import triaina.webview.entity.device.EnvironmentSetParams;
import triaina.webview.entity.device.FormPictureSelectParams;
import triaina.webview.entity.device.FormPictureSelectResult;
import triaina.webview.entity.device.NetBrowserOpenParams;

public abstract class AbstractWebViewBridgeFragment extends TriainaFragment {
    private static final String TAG = AbstractWebViewBridgeFragment.class.getSimpleName();

    private WebViewBridge mWebViewBridge;

    @Inject
    private WebViewBridgeConfigurator mConfigurator;

    @Inject
    private WebViewRestoreManager mRestoreManager;

    @Inject
    private TriainaEnvironment mEnvironment;

    private boolean mIsRestored;
    private Bundle mWebViewStateOnDestroyView;

    final public String[] getDomains() {
        return mWebViewBridge.getDomainConfig().getDomains();
    }

    public void call(String channel, Params params) {
        mWebViewBridge.call(channel, params);
    }

    public void call(String channel, Params params, Callback<?> callback) {
        mWebViewBridge.call(channel, params, callback);
    }

    final public WebViewBridge getWebViewBridge() {
        return mWebViewBridge;
    }

    final public WebViewBridgeConfigurator getConfigurator() {
        return mConfigurator;
    }

    @Bridge("system.environment.set")
    public void doEnvironmentSet(EnvironmentSetParams params) {
        mEnvironment.set(params.getName(), params.getValue());
    }

    @Bridge("system.form.picture.select")
    public void doFormPictureSelect(FormPictureSelectParams params, Callback<FormPictureSelectResult> callback) {
        // TODO need to implement Triaina Framework original logic
    }

    @Bridge("system.net.browser.open")
    public void doNetBrowserOpen(NetBrowserOpenParams params) {
        SystemUtils.launchExternalBrowser(getActivity(), Uri.parse(params.getUrl()));
    }

    @Bridge("system.web.error")
    public void doWebError() {
        // ignore
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflatedView = mConfigurator.loadInflatedView(this, inflater, container);
        mWebViewBridge = mConfigurator.loadWebViewBridge(this, inflatedView);
        mWebViewStateOnDestroyView = null;
        mConfigurator.configure(mWebViewBridge);
        mConfigurator.registerBridge(mWebViewBridge, this);
        configureSettings();
        configureClients();
        return inflatedView;
    }

    protected void configureSettings() {
        mConfigurator.configureSetting(mWebViewBridge);
    }

    protected void configureClients() {
        mWebViewBridge.setWebChromeClient(new TriainaWebChromeClient(getActivity()));
        mWebViewBridge.setWebViewClient(new TriainaWebViewClient());
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mIsRestored = mRestoreManager.restoreWebView(mWebViewBridge, savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mWebViewStateOnDestroyView != null)
            outState.putAll(mWebViewStateOnDestroyView);
        // XXX
        if (mWebViewBridge != null)
            storeWebView(outState);
    }

    protected void storeWebView(Bundle outState) {
        mRestoreManager.storeWebView(mWebViewBridge, outState, getActivity().getTaskId());
    }

    @Override
    public void onResume() {
        super.onResume();
        mWebViewBridge.resume();
    }

    @Override
    public void onPause() {
        mWebViewBridge.pause();
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        mWebViewStateOnDestroyView = new Bundle();
        storeWebView(mWebViewStateOnDestroyView);
        try {
            mWebViewBridge.destroy();
        } catch (Exception exp) {
            Log.w(TAG, exp.getMessage() + "", exp);
        }
        mWebViewBridge = null;
        super.onDestroyView();
    }

    public boolean isRestored() {
        return mIsRestored;
    }
}

@@ -1175,55 +1175,8 @@
 red;
-%0A    private Bundle mWebViewStateOnDestroyView;
 %0A%0A  
@@ -2692,51 +2692,8 @@
 w);%0A
-        mWebViewStateOnDestroyView = null;%0A
     
@@ -3541,128 +3541,8 @@
 e);%0A
-        if (mWebViewStateOnDestroyView != null)%0A            outState.putAll(mWebViewStateOnDestroyView);%0A        // XXX%0A
     
@@ -3986,32 +3986,32 @@
 %0A%0A    @Override%0A
+
     public void 
@@ -4032,109 +4032,8 @@
 ) %7B%0A
-        mWebViewStateOnDestroyView = new Bundle();%0A        storeWebView(mWebViewStateOnDestroyView);%0A
     

Remove unclarified webview.saveState logic