package com.bokecc.cloudclass.demo.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.bokecc.cloudclass.demo.R;
import com.bokecc.common.utils.Tools;
import com.bokecc.room.drag.view.activity.SaasMeetingStudentRoomActivity;
import com.bokecc.room.drag.view.base.CCRoomActivity;

import org.json.JSONObject;

public class LiveActivity extends Activity {
    private static final String TAG = "LiveActivity";
    private WebView webView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live);
        webView = findViewById(R.id.webview);
        Intent intent = getIntent();
        initWebView(intent.getStringExtra("liveUrl"));
    }

    private void initWebView(String url) {
        WebSettings settings = webView.getSettings();
        settings.setDomStorageEnabled(true);
        settings.setJavaScriptEnabled(true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webView.getSettings().setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        webView.setWebViewClient(new WebViewClient() {

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

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                String errMsg = "onReceivedError,description:" + description + ",errorCode" + errorCode + ",failingUrl:" + failingUrl;
                Tools.log(TAG, errMsg);
            }
        });
        webView.loadUrl(url);
        webView.addJavascriptInterface(new JavascriptHandler(), "android");
    }

    class JavascriptHandler {
        @JavascriptInterface
        public String isAppWebView() {
            return "true";
        }

        @JavascriptInterface
        public void onLeaveAndOpenNative(final String json) {
            Tools.log(TAG, "onLeaveAndOpenNative " + json);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    openNative(json);
                }
            });
        }
    }

    private void openNative(String json) {
        try {
            JSONObject object = new JSONObject(json);
            String roomId = object.optString("roomId");
            String accontId = object.optString("accontId");
            String token = object.optString("token");
            Intent intent = new Intent();
            intent.setClass(this, SaasMeetingStudentRoomActivity.class);
            intent.putExtra(CCRoomActivity.SeesionidKey, token);
            intent.putExtra(CCRoomActivity.RoomIdKey, roomId);
            intent.putExtra(CCRoomActivity.UserAccountKey, accontId);
            intent.putExtra(CCRoomActivity.AreaCodeKey, "0");
            intent.putExtra(CCRoomActivity.KeyLayoutMode, 5);
            intent.putExtra(CCRoomActivity.ShowAudienceBtn, true);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivityForResult(intent, 0x1);
            finish();
        } catch (Exception e) {
            Tools.log(TAG, e.toString());
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.stopLoading();
        webView.destroy();
    }
}
