package com.saki.ui;


import com.setqq.script.Script;
import com.saki.qq.global.ScriptManager;
import com.saki.sqv9.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ScriptView extends Activity implements OnClickListener {
    private WebView mWebview;

    class WebC extends WebViewClient {
        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler,
                                       SslError error) {
            handler.proceed();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Uri u = Uri.parse(url);
            String scheme = u.getScheme();
            if (scheme.equals("http")||scheme.equals("https")||scheme.equals("file")){
                return super.shouldOverrideUrlLoading(view,url);
            } else {
                Intent intent = new Intent(Intent.ACTION_VIEW, u);
                startActivity(intent);
                finish();
                return true;
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            Uri u = request.getUrl();
            String scheme = u.getScheme();
            if (scheme.equals("http")||scheme.equals("https")||scheme.equals("file")){
                return super.shouldOverrideUrlLoading(view,request);
            } else {
                Intent intent = new Intent(Intent.ACTION_VIEW, u);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
                return true;
            }
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay_lua);
        String pn = getIntent().getStringExtra("script");
        mWebview = findViewById(R.id.webView1);
        Script script = ScriptManager.getInstance().findScript(pn);
        mWebview.addJavascriptInterface(script.instance,"script");
        mWebview.removeJavascriptInterface("searchBoxJavaBridge_");
        mWebview.removeJavascriptInterface("accessibility");
        mWebview.removeJavascriptInterface("accessibilityTraversal");
        mWebview.getSettings().setDefaultTextEncodingName("utf-8");// 避免中文乱码
        mWebview.requestFocusFromTouch();
        mWebview.setWebViewClient(new WebC());
        mWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        WebSettings settings = mWebview.getSettings();

        settings.setJavaScriptEnabled(true);

        settings.setNeedInitialFocus(false);

        settings.setSupportZoom(true);

        settings.setUseWideViewPort(true);

        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

        settings.setLoadsImagesAutomatically(true);
        mWebview.loadUrl(script.getView());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK && mWebview!=null && mWebview.canGoBack()){
            mWebview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        mWebview.stopLoading();
        mWebview.removeAllViews();
        mWebview.destroy();
        mWebview = null;
    }

}
