package com.aip.gateway.api.repository;

import com.aip.gateway.api.model.request.RequestBase;
import jakarta.servlet.http.HttpServletRequest;

import java.util.UUID;

public class GlobalConstants {

    public static final String API_ENDPOINT = "/aip-api";

    public static final String API_IP_ADDRESS = "api-ip-address";
    public static final String API_START_TM = "api-start-tm";
    public static final String API_START_MILLISECONDS = "api-start-milliseconds";
    public static final String API_GUID = "api-guid";
    public static final String API_ID = "api-id";
    public static final String API_KEY = "api-key";
    public static final String API_RESULT_CODE = "api-result-code";
    public static final String API_RESULT_MESSAGE = "api-result-message";
    public static final String API_REQUEST = "api-request";
    public static final String API_RESULT = "api-result";

    public static final int API_DB_RELOAD = 1;
    public static final int API_DB_LINKED_SYSTEMS = 3;
    public static final int API_DB_LINKED_SERVERS = 4;
    public static final int API_DB_LINKED_API_KEYS = 5;
    public static final int API_DB_LINKED_DECRYPT_KEYS = 6;

    public static final int API_AIP_DOWNLOAD = 10;
    public static final int API_AIP_LABELS = 11;
    public static final int API_AIP_POLICIES = 12;
    public static final int API_AIP_PROTECTIONS = 13;

    public static final int API_FILE_INFO = 21;
    public static final int API_FILE_SET_LABEL = 22;
    public static final int API_FILE_DELETE_LABEL = 23;
    public static final int API_FILE_SET_PROTECTION = 24;
    public static final int API_FILE_DELETE_PROTECTIN = 25;
    public static final int API_FILE_SET_LABEL_PROTECTION = 26;
    public static final int API_FILE_DELETE_LABEL_PROTECTION = 27;

    public static final int API_STREAM_INFO = 31;
    public static final int API_STREAM_SET_LABEL = 32;
    public static final int API_STREAM_DELETE_LABEL = 33;
    public static final int API_STREAM_SET_PROTECTION = 34;
    public static final int API_STREAM_DELETE_PROTECTIN = 35;
    public static final int API_STREAM_SET_LABEL_PROTECTION = 36;
    public static final int API_STREAM_DELETE_LABEL_PROTECTION = 37;

    public static final int API_DUMMY = 99;

    public static final int API_FILE_DATA = 13;

    public static final String API_RESULT_SUCCESS = "성공";
    public static final String API_RESULT_FAIL = "실패";
    public static final String API_RESULT_ERROR = "오류";


    public static String getApiGuid(HttpServletRequest request)
    {
        String apiGuid = (String)request.getAttribute(API_GUID);
        if (apiGuid == null) {
            apiGuid = UUID.randomUUID().toString();
            request.setAttribute(API_GUID, apiGuid);
        }
        return apiGuid;
    }

    public static void setAuthorization(HttpServletRequest request, int apiId, String apiKey, RequestBase req) {
        request.setAttribute(GlobalConstants.API_ID, apiId);
        request.setAttribute(GlobalConstants.API_KEY, apiKey);
        if (req != null) {
            req.setApiGuid(getApiGuid(request));
        }
    }

    public static int getApiId(HttpServletRequest request) {
        Integer apiId = (Integer)request.getAttribute(API_ID);
        if (apiId == null) {
            return -1;
        }
        return apiId;
    }
    public static String getApiKey(HttpServletRequest request) {
        String apiKey = (String)request.getAttribute(API_KEY);
        if (apiKey == null) {
            apiKey = "x";
        }
        return apiKey;
    }
}
