package com.nsgk.ruralWeb;

import android.app.Activity;
import android.location.LocationManager;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Handler;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;

import com.nsgk.ruralWeb.utils.ContextUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EnvWindowObject
{
    private static final String ID_TAG = EnvWindowObject.class.getName();
    private static final String FUSED_PROVIDER = "fused";

    private final Handler m_handler;
    private final Context m_context;
    private final WebView m_webView;

    // 最近一次获取到的定位坐标, 如果获取定位失败则返回此值. 可能不需要, 因为PASSIVE_PROVIDER就为最近的定位
    private String lastLocation = null;

    public EnvWindowObject(Context context, Handler handler, WebView webView)
    {
        m_context = context;
        m_handler = handler;
        m_webView = webView;
    }

    @JavascriptInterface
    public void Toast(final String message)
    {
        RunOnUIThread(new Runnable() {
            @Override
            public void run()
            {
                Toast.makeText(m_context, message, Toast.LENGTH_LONG).show();
            }
        });
    }

    @JavascriptInterface
    public void Log(final String message)
    {
        Log.i(ID_TAG, message);
    }

    @JavascriptInterface
    public String GetLocation(final String type)
    {
        try
        {
            if(ActivityCompat.checkSelfPermission(m_context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(m_context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
            {
                Activity activity = (Activity) m_context;

                if (activity.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION) && activity.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)) // do not ask
                {
                    Toast.makeText(m_context, "请先允许定位服务", Toast.LENGTH_LONG).show();
                    ContextUtils.RequestLocationPermission(activity, FullscreenActivity.PERMISSION_LOCATION_REQUEST_CODE);
                }
                else
                {
                    activity.requestPermissions(new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION }, FullscreenActivity.PERMISSION_LOCATION_REQUEST_CODE);
                }

                return lastLocation;
            }

            LocationManager lm = (LocationManager) m_context.getSystemService(Context.LOCATION_SERVICE);

            List<String> allProviders = lm.getAllProviders();
            Map<String, LocationInfo> map = new HashMap<>();
            for(String provider : allProviders)
            {
                Location lastKnownLocation = lm.getLastKnownLocation(provider);
                if(null == lastKnownLocation)
                    continue;
                LocationInfo loc = new LocationInfo(provider, lastKnownLocation.getLongitude(), lastKnownLocation.getLatitude());
                map.put(provider, loc);
            }
            LocationInfo loc = null;
            if(null != type && !type.isEmpty() && map.containsKey(type))
                loc = map.get(type);
            else if(map.containsKey(LocationManager.NETWORK_PROVIDER))
                loc = map.get(LocationManager.NETWORK_PROVIDER);
            else if(map.containsKey(LocationManager.GPS_PROVIDER))
                loc = map.get(LocationManager.GPS_PROVIDER);
            else if(map.containsKey(LocationManager.PASSIVE_PROVIDER))
                loc = map.get(LocationManager.GPS_PROVIDER);
            else if(map.containsKey(FUSED_PROVIDER))
                loc = map.get(FUSED_PROVIDER);

            if(null != loc)
                lastLocation = loc.longitude + "," + loc.latitude;
        }
        catch(Throwable e)
        {
            e.printStackTrace();
        }
        return lastLocation;
    }

    protected void RunOnUIThread(Runnable runnable)
    {
        if(null != m_handler)
            m_handler.post(runnable);
        else if(m_context instanceof Activity)
            ((Activity)m_context).runOnUiThread(runnable);
        else
        {
            Log.e(ID_TAG, "无法在UI线程中执行");
        }
    }

    protected void CallJSFunc(final String func, Object...args)
    {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < args.length; i++)
        {
            sb.append("'").append(args[i].toString()).append("'");
            if(i < args.length - 1)
                sb.append(", ");
        }
        final String arg = sb.toString();
        RunOnUIThread(new Runnable() {
            @Override
            public void run()
            {
                String script = "javascript:typeof(" + func + ") == 'function' && " + func + "(" + arg + ");";
                Log.e(ID_TAG, String.format("调用js函数: 函数(%s), 参数(%s)", func, arg));
                m_webView.evaluateJavascript(script, new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        Log.e(ID_TAG, String.format("js函数 %s 返回: %s", func, value));
                    }
                });
            }
        });
    }

    protected void CallJSFunc_beforeAndroid4_4(final String func, Object...args)
    {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < args.length; i++)
        {
            sb.append("'").append(args[i].toString()).append("'");
            if(i < args.length - 1)
                sb.append(", ");
        }
        final String arg = sb.toString();
        RunOnUIThread(new Runnable() {
            @Override
            public void run() {
                Log.e(ID_TAG, String.format("调用js函数: 函数(%s), 参数(%s)", func, arg));
                String script = "javascript:typeof(" + func + ") == 'function' && " + func + "(" + arg + ");";
                Log.e(ID_TAG, script);
                m_webView.loadUrl(script, null);
            }
        });
    }
}