package com.backendsys.service.Ai.Aizn;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.backendsys.exception.CustException;
import com.fzzixun.openapi.sdk.client.OpenClient;
import com.fzzixun.openapi.sdk.request.AppTokenRequest;
import com.fzzixun.openapi.sdk.response.AppTokenResponse;
import com.fzzixun.openapi.sdk.response.CommonResponse;
import org.springframework.stereotype.Service;

@Service
public class AiznServiceImpl implements AiznService{

    private String url = "https://sbappstoreapi.ziniao.com";
    private String appId = "202404231232292775081406464";
    private String privateKeyIsv = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCNl8AKGVm9WnO67gEeLXeHKE2MBp16h0BKmHWrhdurM8uu1fIfRgR4K6AguE97mGqfCYBSad8cfP1dbrA529cZPXXFegl7tSBMxciK7i3kPTqPRS3sUbLQZHHdceoxWXDGA6oa3xxl3I6MuqMIO8vzYrdkY+A5h7sKY59tiYl3uq/mhJnq+FylkiQt3BLy7ATXVWFqMybAt6Pznawlaj2pbCVdSbM7hIKD8StgorAd9a7AnnstiJH7NmtppGS720qXeLHmgkse1hHf8gIel3nbi9iB7XAdC2iM8it425BuWaDb1y0SYZ7EZmhu8f45bsCxUJdd9QEC1dDR90pW0rJFAgMBAAECggEAUEB3VwIBTAJtX8CwH4OToKZmNXw2W5E3DlCVOQ9YbxOfWUvnvHP56oaHbqQoYJwFqng/I/bfvwzUqdUnytNgeezMyI2fvF7vnLPpM11F43wUqcmC+tNfbx/PLbz4CNpxwbmn+y0DsnQs03ShHPSPZzZHTX4SQ2k7yOqoHPR8VsA5g8fIXcYJeVggSG3zJWMB8a7o4r3UvDVfeOVkMj/IOdmwBRRBPiG+JAWNKuGJH6iiSuFN+hVKs7zwdSkLWgQgBNdo9Uss9cm1NGH/4nxYhuM2T+ZyLgq9DCo0TlHb+QvBaex22vGU/IYZobRxWjo2CudxO03rOh4fLzTgWziAbQKBgQD0c7vbg8hVKsuWSkMh/mfEGPIkLgVdVCbObCpMFdNvtevU6x1dS2tXokZ19xbx7xrS7vZgNqfJ9WVGo0OwcFZ4Y6alYCxnVqmtRzEgFyvfd3kHn5Rib7V11vi+viiKbmSIMmPNH9/xMmmp5HCNp57OP//RNtpiYDQ9jI7tpdltCwKBgQCUSBf3Gxrj2IWpoAqT9dT3+yXSS7OONWGpQrDn071o4kPhRjGq9fFYG5v3LcY9zJd70yz382cV4WOIiqDZxBGfYbe5JD31lX/eKZepGviZAsvF1Cp9urMgR+NahGrgfOHNfnr/r2dmTqORnTBnfQI1N7Ig0xAgY5akgy3vez7P7wKBgQCs9KnIwa/fWl0bI8Xl6ls+qqvAH3OaOe0PBgbTOGoV0vBeAFARb0gug9k4o69vU851ya82GdWTlfWnCeLdlk4H9fG1dptULkP1HrbKTrqy7JQMacMR9pm8G3J5eo+yijLFDmoAevn2bMXH96IPB7bblPqJ6zaOfiTYG3gQ+tQgSwKBgHbhwg0Edy+bRbZ0MGbl3iQe//i1ZrSqCa2JYYnMEfyIA4IWI/X8Ds3j/MxUr66WXBETguccB1bciE9NTZ6p+rL8xi6yZtaBUK4OTKBwlkUen77w1kxNJhXIbPgCWCm5FdQKBpT9zPmSLMOu8cLWz7FZAQAeF7zP+1PUNDzfY8GTAoGAQG79mbMSMpxpneuksIPPK0/w+LRAd1bsExXbIWqdO45rGITTpspv42mTICnX1vqbMPDJezPNyYJHhtR5n9rZziOepr3cG2oLvWdUftMatH058tP1VKEi5VH338ND59rgbJGru/nWnjFZ/GnBIpXaGT+itQoNvwZ8jd+of6oM/F8=";

    /**
     * 紫鸟浏览器 -> 紫鸟开放平台
     * https://open.ziniao.com/manage
     *
     * 应用管理 -> 创建企业自建应用 ->
     *      [应用] -> 权限管理 -> 接口权限申请 -> (权限名称:	AI图片) 开通权限
     *      [应用] -> 应用首页 -> 配置IP白名单
     */

    /**
     * [Method] 8.3.1.3 获取应用token
     * https://open.ziniao.com/docSupport?docId=84#8.3.1.3%E8%8E%B7%E5%8F%96%E5%BA%94%E7%94%A8token27
     */
    public String getAppToken() {
        OpenClient client = new OpenClient(url, appId, privateKeyIsv);
        // 创建请求对象
        AppTokenRequest request = new AppTokenRequest();
        // 发送请求
        CommonResponse response = client.execute(request);
        if (response.isSuccess()) {
            AppTokenResponse appTokenResponse = response.getDataObj(AppTokenResponse.class);
            return appTokenResponse.getAppAuthToken();
        } else {
            throw new CustException(response.getErrorMsg());
        }
    }

    public OpenClient getOpenClient() {
        return new OpenClient(url, appId, privateKeyIsv);
    }

    /**
     * 返回值 结构体
     */
    public JSONObject checkResponse(CommonResponse response) {
        if (response.isSuccess()) {
            // 返回结果
            String data = response.getData();
            JSONObject resp = JSONUtil.parseObj(data);
            return resp;
            //
        } else {
            System.out.println(response.getBody());
            System.out.println(response.getErrorMsg());
            throw new CustException(response.getErrorMsg());
        }
    }

}
