/*
 * Copyright (C) 2025 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.example.wkttestapp.utils;

import android.content.Context;
import android.webkit.JavascriptInterface;


import com.xuexiang.xui.utils.XToastUtils;

import cn.hutool.core.lang.Pair;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;


/**
 * @author ：张俊
 * @date ：Created in 2025/3/3 13:57
 * @description：
 */
public class WebAppJsInterface {

    public static Pair<Double, Double> location;


    @JavascriptInterface
    public void showInfo(String message) {
        XToastUtils.info(message);
    }


    @JavascriptInterface
    public void showSuccess(String message) {
        XToastUtils.success(message);
    }

    @JavascriptInterface
    public void showWarning(String message) {
        XToastUtils.warning(message);
    }

    @JavascriptInterface
    public void onLocationSelected(String locationData) {
        // 解析JSON数据
        try {
            JSONObject data = new JSONObject(locationData);
            String name = data.getStr("name");
            double lat = data.getDouble("lat");
            double lng = data.getDouble("lng");
            String address = data.getStr("address");
            String screenshot = data.getStr("screenshot");
            String format = StrUtil.format("名称：【{}】，地址【{}】，经度【{}】，纬度【{}】", name, address, lat, lng);
            XToastUtils.info(format);


        } catch (JSONException e) {
            XToastUtils.warning("处理位置数据更新失败");
        }
    }


}
