package com.java110.user.cmd.property;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.user.dao.property.IAppVersionV1ServiceDao;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

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

@Java110Cmd(serviceCode = "property.checkVersion")
public class CheckVersionCmd extends Cmd {

    @Autowired
    private IAppVersionV1ServiceDao appVersionV1ServiceDao;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) {
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) {
        int currentCode = reqJson.getIntValue("versionCode");
        String platform = reqJson.containsKey("platform") ? reqJson.getString("platform") : "android";

        Map<String, Object> params = new HashMap<>();
        params.put("platform", platform);
        Map latest = appVersionV1ServiceDao.queryLatestVersion(params);

        JSONObject result = new JSONObject();
        result.put("hasUpdate", false);

        if (latest != null) {
            Object versionCodeObj = latest.get("version_code");
            int latestCode = 0;
            if (versionCodeObj instanceof Number) {
                latestCode = ((Number) versionCodeObj).intValue();
            } else if (versionCodeObj != null) {
                latestCode = Integer.parseInt(versionCodeObj.toString());
            }

            if (latestCode > currentCode) {
                result.put("hasUpdate", true);
                result.put("versionCode", latestCode);
                result.put("versionName", latest.get("version_name") != null ? latest.get("version_name").toString() : "");
                String ossUrl = latest.get("download_url") != null ? latest.get("download_url").toString() : "";
                result.put("downloadUrl", convertToProxyUrl(ossUrl, context));
                result.put("forceUpdate", latest.get("force_update") != null && (Integer.parseInt(latest.get("force_update").toString()) == 1));
                result.put("updateMessage", latest.get("update_message") != null ? latest.get("update_message").toString() : "");
            }
        }

        context.setResponseEntity(ResultVo.createResponseEntity(result));
    }

    private String convertToProxyUrl(String ossUrl, ICmdDataFlowContext context) {
        if (ossUrl == null || !ossUrl.contains(".oss-")) return ossUrl;
        try {
            java.net.URL url = new java.net.URL(ossUrl);
            String path = url.getPath();
            if (path.startsWith("/")) path = path.substring(1);
            String host = context.getReqHeaders().get("host");
            String scheme = host != null && host.contains("localhost") ? "http" : "https";
            return scheme + "://" + host + "/app/downloadApk?file=" + path;
        } catch (Exception e) {
            return ossUrl;
        }
    }
}
