package org.example.SystemApi.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.Api.OptBaseHandler;
import org.example.Entity.Attributes.RequestCheck;
import org.example.Impl.BaseImpl;
import org.example.Impl.SystemImpl;
import org.example.Utils.NativeExtensionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;

import static org.example.Utils.NativeExtensionUtils.*;

@RestController
@Scope("prototype")
@RequestMapping("/Api/SystemAjaxApi")
public class SystemAjaxApi extends OptBaseHandler {
    private static final Logger log = LoggerFactory.getLogger(SystemAjaxApi.class);


    @RequestMapping(value = "/**", method = {RequestMethod.GET, RequestMethod.POST})
    public void handleRequest(HttpServletRequest Request, HttpServletResponse response) throws Exception {
        // 调用 BaseHandler 的 processRequest 处理逻辑
        super.processRequest(Request);
    }

    @Autowired
    private SystemImpl sysOperator;

    public void GetSystems() {
        response = sysOperator.GetSystems(ToInt32(getBImpl().Request("id")));
    }

    /// <summary>
    /// 获取系统名
    /// </summary>
    @RequestCheck(CheckLogin = false)
    public void GetSystemInfo() {
        response = sysOperator.GetSystemInfo();
    }

    /// <summary>
    /// 获取设备宝第三方API地址
    /// </summary>
    @RequestCheck(CheckLogin = false)
    public void GetEmaUrl() {
        response = sysOperator.GetEmaUrl();
    }


    /// <summary>
    /// 获取子系统 及子系统下的菜单，2020.11.09加入 seriesId的可选参数，根据 GetProSysType是否获取数据来决定是否传递该参数
    /// </summary>
    public void GetSysMenus() {
        BaseImpl bImpl = getBImpl();
        boolean all = toBoolean(bImpl.Request("all"));
        String pid = bImpl.Request("pid");
        pid = isNullOrEmpty(pid) ? "0" : pid;
        String target = bImpl.Request("target");
        response.setData(sysOperator.GetSysMenus(all ? "" : pid, target));
        response.setSuccess(response.getData() != null);
    }

    /// <summary>
    /// 获取系统名
    /// </summary>
    @RequestCheck(CheckLogin = false)
    public void GetSystemLoginInfo() {
        response.setData(sysOperator.GetSystemLoginInfo());
        response.setSuccess(true);
    }

    /// <summary>
    /// 获取多个系统 seriesId 的 下拉框数据，如果没有则忽略该下拉框
    /// 新版弃用 20210513,改为统一用logininfo接口获取
    /// </summary>
    @RequestCheck(CheckLogin = false)
    public void GetProSysType() {
        response = sysOperator.GetProSysType();
    }

    /// <summary>
    /// 新版弃用 20210513 改为统一用logininfo接口获取 20250113修改重新使用
    /// </summary>
    @RequestCheck(CheckLogin = false)
    public void GetDbServer() {
        response = sysOperator.GetDbServer();
    }

    @RequestCheck(CheckLogin = false)
    public void GetWebUpdateInfo() {
        BaseImpl bImpl = getBImpl();

        // 获取版本号参数并转换为int
        int ver = NativeExtensionUtils.ToInt32(bImpl.Request("ver"));

        // 获取系统数据参数
        String sysData = bImpl.Request("cinfo") + "";
        // 判断是否包含URL编码的左花括号("%7b")，如果有则进行解码
        if (sysData.indexOf("%7b") > -1) {
            try {
                sysData = URLDecoder.decode(sysData, StandardCharsets.UTF_8);
            } catch (Exception e) {
                // 处理解码异常，可根据实际需求添加日志或默认处理
                log.error("Exception caught", e);
            }
        }

        // 调用系统操作类获取更新信息并赋值给响应对象
        response = sysOperator.GetWebUpdateInfo(ver, sysData);
    }
}
