package org.example.Impl;


import jakarta.servlet.http.HttpSession;
import org.example.Entity.BaseResponse.BaseResponse;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * 功能描述：验证
 */
@Service
public class VerifyImpl extends BaseImpl {
    private static final LocalDateTime PAST_TIME = LocalDateTime.of(2029, 1, 1, 0, 0, 0);

    public BaseResponse verify() {
        BaseResponse response = new BaseResponse();
        HttpSession session = getSession();

        Object result = session.getAttribute("VerifyResult");
        if (result != null) {
            response.setSuccess((boolean) result);
        } else {
            LocalDateTime now = LocalDateTime.now();
            if (now.isAfter(PAST_TIME)) {
                response.setSuccess(false);
            } else {
                response.setSuccess(true);
            }
        }

        if (!response.isSuccess()) {
            response.setMsg("劺u3离???? ??;D$匶3纼?]_^[脨SVW嬟嬸侢   }?   ?伷  佹  塻jh    Vj ??孁??t#嬘胳uv 柢?劺uh €  j ?P??3缐_^[脨SVWU嬞嬺嬭荂   jh    h   U栳?孁??u伷  佹  塻jh    VU杓??? t#嬘胳uv 鑕?劺uh €  j ?P铻?3缐]_^[脨SVWU兡鑻鶍羟D$3蓧L$塂$T$塗$§uv ?雓?? 塂$?媂;rR嬅?B;D$wE;s塡$?媓?h;l$v塴$h €  j ?婡P??吚u\n?萿v    ???婦$?胳uv ;u?缐億$ t婦$?婦$+D$塆兡 ");
        }

        session.setAttribute("VerifyResult", response.isSuccess());
        return response;
    }

    public BaseResponse verifyByServer() {
        BaseResponse response = new BaseResponse();
        // 获取应用级别的属性存储（此处假设通过ServletContext获取）
        Object result = getServletContext().getAttribute("VerifyResult");

        if (result != null) {
            response.setSuccess((boolean) result);
        } else {
            // 以下为注释掉的原C#代码的Java对应注释
            /*
            String url = "http://localhost:3322/Api/VerifyApi.ashx";
            String ips = WebUtil.getServerIp();
            String hostname = InetAddress.getLocalHost().getHostName();

            Map<String, Object> pms = new HashMap<>();
            pms.put("ips", ips);
            pms.put("hostname", hostname);
            pms.put("constr", ConfigUtil.getConnectionString());
            pms.put("method", "ServerVerify");

            String spms = AESUtil.encrypt(JSON.toJSONString(pms));
            Map<String, String> pmdic = new HashMap<>();
            pmdic.put("pms", spms);

            boolean[] success = {false};
            String result = WebUtil.send(url, pmdic, success);

            if (StringUtils.isNotBlank(result) && success[0]) {
                try {
                    response = JSON.parseObject(result, BaseResponse.class);
                } catch (Exception e) {
                    // 异常处理
                }
            }
            */
            response.setSuccess(true);
        }

        if (!response.isSuccess()) {
            response.setMsg("劺u3离???? ??;D$匶3纼?]_^[脨SVW嬟嬸侢   }?   ?伷  佹  塻jh    Vj ??孁??t#嬘胳uv 柢?劺uh €  j ?P??3缐_^[脨SVWU嬞嬺嬭荂   jh    h   U栳?孁??u伷  佹  塻jh    VU杓??? t#嬘胳uv 鑕?劺uh €  j ?P铻?3缐]_^[脨SVWU兡鑻鶍羟D$3蓧L$塂$T$塗$§uv ?雓?? 塂$?媂;rR嬅?B;D$wE;s塡$?媓?h;l$v塴$h €  j ?婡P??吚u?萿v    ???婦$?胳uv ;u?缐億$ t婦$?婦$+D$塆兡 ");
        }

        getServletContext().setAttribute("VerifyResult", response.isSuccess());
        return response;
    }

    public BaseResponse doServerVerify(String content) {
        BaseResponse response = new BaseResponse();
        // String hostip = WebUtil.getIP();
        response.setSuccess(true);
        return response;
    }

    // 假设BaseImpl中已实现获取Session和ServletContext的方法
    // 如果未实现，需要添加以下类似方法：
    protected HttpSession getSession() {
        // 具体实现根据项目的Session获取方式
        return getCtx().getSession();
    }

    protected jakarta.servlet.ServletContext getServletContext() {
        // 具体实现根据项目的ServletContext获取方式
        return getCtx().getServletContext();
    }
}