package com.mayi.fastdevelop.commonpage;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;

import com.mayi.fastdevelop.R;
import com.mayi.fastdevelop.base.BaseActivity;
import com.mayi.fastdevelop.commonpage.amap.MapLocationActivity;
import com.mayi.fastdevelop.comnon.Key;
import com.mayi.fastdevelop.comnon.RequestCode;
import com.mayi.fastdevelop.util.BitmapUtil;
import com.mayi.fastdevelop.util.DialogUtils;
import com.mayi.fastdevelop.util.SpUtil;
import com.mayi.fastdevelop.view.CustomTitleBar;
import com.mayi.fastdevelop.view.dialog.BaseDialog;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.smtt.export.external.interfaces.GeolocationPermissionsCallback;
import com.tencent.smtt.export.external.interfaces.WebResourceError;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class WebActivity extends BaseActivity {

    private WebView webView;
    private CustomTitleBar title;
    private Handler handler;
    private ValueCallback<Uri> uploadMessage;
    private ValueCallback<Uri[]> uploadMessageAboveL;
    private String mCurrentPhotoPath;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        webView = findViewById(R.id.webview);
        title = findViewById(R.id.title);
        title.setOnClickLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webPageBack();
            }
        });
        handler = new Handler();
        initWebView();
        webView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedIcon(WebView view, Bitmap icon) {
                super.onReceivedIcon(view, icon);
            }

            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissionsCallback geolocationPermissionsCallback) {
                geolocationPermissionsCallback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, geolocationPermissionsCallback);
            }

            //For Android  >= 5.0
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                uploadMessageAboveL = filePathCallback;
                uploadPicture();
                return true;
            }

            //For Android  >= 4.1
            public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
                uploadMessage = valueCallback;
                uploadPicture();
            }
        });
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {//处理网页加载失败时
                super.onReceivedError(view, errorCode, description, failingUrl);
                Throwable thr = new Throwable("web加载错误： errorCode" + errorCode + " \ndescription=" + description + " \nfailingUrl=" + failingUrl);
                CrashReport.postCatchedException(thr);
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {//处理网页加载失败时
                super.onReceivedError(view, request, error);
                Throwable thr = new Throwable("web加载错误： errorCode" + error.getErrorCode() + " \ndescription=" + error.getDescription() + " \nfailingUrl=" + request.getUrl());
                CrashReport.postCatchedException(thr);
            }
        });
        initView();
    }

    /**
     * web返回上一页
     */
    private void webPageBack() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            finish();
        }
    }

    private void initView() {
        String url = getIntent().getStringExtra("url");
        if (TextUtils.isEmpty(url)) {
            return;
        }
        if (url.contains("isNeedBackButton")) {
            title.setVisibility(View.VISIBLE);
        } else {
            title.setVisibility(View.GONE);
        }
        if (url.contains("userAppTitle=")) {
            int start = url.indexOf("userAppTitle=");
            String text = null;
            if (url.indexOf("&", start) != -1) {
                text = url.substring(start + "userAppTitle=".length(), url.indexOf("&", start));
            } else {
                text = url.substring(start + "userAppTitle=".length());
            }
            String keyWord = "";
            try {
                keyWord = URLDecoder.decode(text, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            title.setTextCenter(keyWord);
        }
        webView.loadUrl(url);
    }

    /**
     * 选择相机或者相册
     */
    public void uploadPicture() {
        BaseDialog dialog = DialogUtils.showSelectPicturesDialog(this, new DialogUtils.DefaultDialogListener() {
            @Override
            public void onClickLeft() {
                //请求拍照权限
                if (ActivityCompat.checkSelfPermission(WebActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    takePhoto();
                } else {
                    ActivityCompat.requestPermissions(WebActivity.this, new String[]{Manifest.permission.CAMERA}, RequestCode.REQUEST_CODE_PERMISSION_CAMERA);
                }
            }

            @Override
            public void onClickRight() {
                chooseAlbumPic();
            }
        });
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                //一定要返回null,否则<input type='file'>
                if (uploadMessage != null) {
                    uploadMessage.onReceiveValue(null);
                    uploadMessage = null;
                }
                if (uploadMessageAboveL != null) {
                    uploadMessageAboveL.onReceiveValue(null);
                    uploadMessageAboveL = null;
                }
            }
        });
    }

    /**
     * 拍照
     */
    public void takePhoto() {
        StringBuilder fileName = new StringBuilder();
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileName.append(System.currentTimeMillis()).append("_upload.jpg");
        File tempFile = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), fileName.toString());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri uri = FileProvider.getUriForFile(this, "com.mayi.fastdevelop", tempFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        } else {
            Uri uri = Uri.fromFile(tempFile);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        }
        mCurrentPhotoPath = tempFile.getAbsolutePath();
        startActivityForResult(intent, RequestCode.REQUEST_CODE_CAMERA);
    }

    /**
     * 选择相册照片
     */
    public void chooseAlbumPic() {
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "Image Chooser"), RequestCode.REQUEST_CODE_ALBUM);
    }

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

    //app调用web方法
    public void appCallJs(String method, String data) {
        webView.loadUrl("javascript:appCallJs.invoking;(" + method + "," + data + ")");
    }


    @JavascriptInterface
    public void invoking(final String method, final String data) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.equals("goCallPhone", method)) {//去打电话
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri uri = Uri.parse("tel:" + data);
                    intent.setData(uri);
                    startActivity(intent);
                } else if (TextUtils.equals("backPage", method)) {//返回上一页
                    webPageBack();
                } else if (TextUtils.equals("goPage", method)) {//跳转原生页面
                    try {
                        JSONObject jsonObject = new JSONObject(data);
                        goPage(jsonObject.optString("page"), jsonObject.optString("parameter"));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else if (TextUtils.equals("openDebug", method)) {//开启Debug模式
                    WebView.setWebContentsDebuggingEnabled(true);
                } else if (TextUtils.equals("goBrowser", method)) {//跳转浏览器
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(data);
                    intent.setData(content_url);
                    startActivity(intent);
                } else if (TextUtils.equals("getUserInfo", method)) {//给web传递用户信息
                    appCallJs("setUserInfo", SpUtil.get(Key.USER_INFO, ""));
                }
            }
        });
    }

    /**
     * 跳转原生页面
     * 待扩展
     *
     * @param pageName
     * @param parameter
     */
    public void goPage(String pageName, String parameter) {
        if (TextUtils.equals(pageName, "MapLocation")) {//地图定位页面
            Intent intent = new Intent(this, MapLocationActivity.class);
            startActivityForResult(intent, RequestCode.MAP_LOCATION_ACTIVITY_RESULTCODE);
        }
    }

    private void initWebView() {
        WebSettings settings = webView.getSettings();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(2);
        }
        settings.setBlockNetworkImage(false);
        settings.setSupportMultipleWindows(false);
        settings.setDefaultTextEncodingName("utf-8");
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false);   //垂直不显示
        settings.setUseWideViewPort(true);   //适应屏幕
        settings.setLoadWithOverviewMode(true);
        settings.setSupportZoom(true);   //支持屏幕缩放
        settings.setBuiltInZoomControls(true);
        settings.setDisplayZoomControls(false);   //不显示webview缩放按钮
        settings.setDatabaseEnabled(true);
        String dir = getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        settings.setGeolocationEnabled(true);    //启用地理定位
        settings.setGeolocationDatabasePath(dir);    //设置定位的数据库路径
        settings.setDomStorageEnabled(true);   //最重要的方法，一定要设置，这就是出不来的主要原因
        webView.addJavascriptInterface(this, "jsCallApp");
    }

    private void uploadImage(Uri imageUri) {
        if (uploadMessage != null) {
            uploadMessage.onReceiveValue(imageUri);
            uploadMessage = null;
        }
        if (uploadMessageAboveL != null) {
            if (imageUri == null) {
                uploadMessageAboveL.onReceiveValue(null);
            } else {
                uploadMessageAboveL.onReceiveValue(new Uri[]{imageUri});
            }
            uploadMessageAboveL = null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode==RequestCode.MAP_LOCATION_ACTIVITY_RESULTCODE){
            if (resultCode== Activity.RESULT_OK){
                showToast(data.getStringExtra(Key.AVTIVITY_RESULT));
            }
        }else if (requestCode == RequestCode.REQUEST_CODE_ALBUM || requestCode == RequestCode.REQUEST_CODE_CAMERA) {
            if (uploadMessage == null && uploadMessageAboveL == null) {
                return;
            }
            if (resultCode != RESULT_OK) {//取消拍照或者图片选择时
                uploadImage(null);  //一定要返回null,否则<input file> 就是没有反应
            } else if (resultCode == RESULT_OK) { //拍照成功和选取照片时
                Uri imageUri = null;
                switch (requestCode) {
                    case RequestCode.REQUEST_CODE_ALBUM:
                        if (data != null) {
                            imageUri = data.getData();
                        }
                        uploadImage(imageUri);
                        break;
                    case RequestCode.REQUEST_CODE_CAMERA:
                        zoomsInPhoto();
                        showLoadingDialog();
                        break;
                }
            }
            return;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 缩小照片尺寸
     */
    private void zoomsInPhoto() {
        AsyncTask asyncTask = new AsyncTask() {
            @Override
            protected Object doInBackground(Object[] objects) {
                if (!TextUtils.isEmpty(mCurrentPhotoPath)) {
                    BitmapUtil.zoomsInImageFile(mCurrentPhotoPath, mCurrentPhotoPath,
                            Bitmap.CompressFormat.JPEG, 900, 80);
                }
                return null;
            }

            @Override
            protected void onPostExecute(Object o) {
                super.onPostExecute(o);
                Uri uri = null;
                if (!TextUtils.isEmpty(mCurrentPhotoPath)) {
                    File file = new File(mCurrentPhotoPath);
                    uri = Uri.fromFile(file);
                }
                uploadImage(uri);
                dismssLoadingDialog();
            }
        };
        asyncTask.execute();
    }

}
