package com.friday.tooldriver.constants.uipath;

import com.alibaba.fastjson.TypeReference;
import com.friday.tooldriver.dto.uipath.common.UiPathUrlParam;
import com.friday.tooldriver.dto.uipath.request.FolderDto;
import com.friday.tooldriver.dto.uipath.request.MachineDto;
import com.friday.tooldriver.dto.uipath.request.ProcessReleaseDto;
import com.friday.tooldriver.dto.uipath.response.*;
import com.friday.tooldriver.dto.uipath.response.*;
import org.springframework.http.HttpMethod;

public enum UIPathApiEnum {

    AUTHENTICATE(
            "/api/Account/Authenticate",
            HttpMethod.POST,
            LoginResponseDto.class,
            UiPathApiErrorCode.AUTHENTICATE_ERROR),
    CREATE_FOLDER(
            "/odata/Folders",
            HttpMethod.POST,
            FolderDto.class,
            UiPathApiErrorCode.CREATE_FOLDER_ERROR),
    GET_FOLDERS(
            "/odata/Folders",
            HttpMethod.GET,
            new TypeReference<ODataResponseDto<FolderDto>>() {
            },
            UiPathApiErrorCode.GET_FOLDERS_ERROR),
    GET_FOLDER(
            "/odata/Folders(EntityId)",
            HttpMethod.GET,
            FolderDto.class,
            UiPathApiErrorCode.GET_FOLDER_ERROR),
    DELETE_FOLDER(
            "/odata/Folders(EntityId)",
            HttpMethod.DELETE,
            FolderDto.class,
            UiPathApiErrorCode.DELETE_FOLDER_ERROR),
    UPLOAD_PACKAGE(
            "/odata/Processes/UiPath.Server.Configuration.OData.UploadPackage",
            HttpMethod.POST,
            new TypeReference<ODataResponseDto<BulkItemDto>>() {
            },
            UiPathApiErrorCode.UPLOAD_PACKAGE_ERROR),
    DELETE_PACKAGE(
            "/odata/Processes('EntityId')",
            HttpMethod.DELETE,
            String.class,
            UiPathApiErrorCode.DELETE_PACKAGE_ERROR),
    DOWNLOAD_PACKAGE(
            "/odata/Processes/UiPath.Server.Configuration.OData.DownloadPackage(key='EntityId')",
            HttpMethod.GET,
            String.class,
            UiPathApiErrorCode.DOWNLOAD_PACKAGE_ERROR),
    RELEASE_PROCESS(
            "/odata/Releases",
            HttpMethod.POST,
            ProcessReleaseDto.class,
            UiPathApiErrorCode.RELEASE_PROCESS_ERROR),
    RELEASE_PROCESS_TO_SPECIFIC_VERSION(
            "/odata/Releases(EntityId)",
            HttpMethod.PATCH,
            String.class,
            UiPathApiErrorCode.RELEASE_PROCESS_TO_SPECIFIC_VERSION_ERROR),
    DELETE_RELEASE(
            "/odata/Releases(EntityId)",
            HttpMethod.DELETE,
            String.class,
            UiPathApiErrorCode.DELETE_RELEASE_ERROR),
    GET_RELEASE(
            "/odata/Releases(EntityId)",
            HttpMethod.GET,
            ProcessReleaseDto.class,
            UiPathApiErrorCode.GET_RELEASE_ERROR),
    IDENTITY_ADD_USER(
            "/identity/api/User",
            HttpMethod.POST,
            IdentityUserResponseDto.class,
            UiPathApiErrorCode.ADD_IDENTITY_USER_ERROR),
    ADD_USER(
            "/odata/Users",
            HttpMethod.POST,
            UserResponseDto.class,
            UiPathApiErrorCode.ADD_USER_ERROR),
    ASSIGN_USER(
            "/odata/Folders/UiPath.Server.Configuration.OData.AssignUsers",
            HttpMethod.POST,
            String.class,
            UiPathApiErrorCode.ASSIGN_USER_ERROR),
    REMOVE_USER(
            "/odata/Folders(EntityId)/UiPath.Server.Configuration.OData.RemoveUserFromFolder",
            HttpMethod.POST,
            String.class,
            UiPathApiErrorCode.REMOVE_USER_ERROR),
    ASSIGN_TEMPLATE(
            "/odata/Folders/UiPath.Server.Configuration.OData.UpdateMachinesToFolderAssociations",
            HttpMethod.POST,
            String.class,
            UiPathApiErrorCode.ASSIGN_TEMPLATE_ERROR),
    GET_PROCESS_VERSION(
            "/odata/Processes/UiPath.Server.Configuration.OData.GetProcessVersions(processId='EntityId')",
            HttpMethod.GET,
            new TypeReference<ODataResponseDto<ProcessResponseDto>>() {
            },
            UiPathApiErrorCode.GET_PROCESS_VERSION_ERROR),
    DELETE_USER(
            "/odata/Users(EntityId)",
            HttpMethod.DELETE,
            String.class,
            UiPathApiErrorCode.DELETE_USER_ERROR),
    GET_USERS(
            "/odata/Users",
            HttpMethod.GET,
            new TypeReference<ODataResponseDto<UserResponseDto>>() {
            },
            UiPathApiErrorCode.GET_USERS_ERROR),
    GET_USER(
            "/odata/Users(EntityId)",
            HttpMethod.GET,
            UserResponseDto.class,
            UiPathApiErrorCode.GET_USER_ERROR),
    UPDATE_USER(
            "/odata/Users(EntityId)",
            HttpMethod.PUT,
            String.class,
            UiPathApiErrorCode.UPDATE_USER_ERROR),
    GET_GLOBAL_SESSIONS(
            "/odata/Sessions/UiPath.Server.Configuration.OData.GetGlobalSessions",
            HttpMethod.GET,
            new TypeReference<ODataResponseDto<SessionResponseDto>>() {
            },
            UiPathApiErrorCode.GET_GLOBAL_SESSIONS_ERROR),
    GET_MACHINE(
            "/odata/Machines(EntityId)",
            HttpMethod.GET,
            MachineDto.class,
            UiPathApiErrorCode.GET_MACHINE_ERROR),
    GET_LICENSES_RUNTIME(
            "/odata/LicensesRuntime/UiPath.Server.Configuration.OData.GetLicensesRuntime(robotType='EntityId')",
            HttpMethod.GET,
            new TypeReference<ODataResponseDto<LicensesRuntimeResponseDto>>() {
            },
            UiPathApiErrorCode.GET_LICENSES_RUNTIME_ERROR),
    LICENSES_RUNTIME_TOGGLE(
            "/odata/LicensesRuntime('EntityId')/UiPath.Server.Configuration.OData.ToggleEnabled",
            HttpMethod.POST,
            String.class,
            UiPathApiErrorCode.LICENSES_RUNTIME_TOGGLE_ERROR),
    GET_LICENSES(
            "/odata/Settings/UiPath.Server.Configuration.OData.GetLicense",
            HttpMethod.GET,
            LicenseNumDto.class,
            UiPathApiErrorCode.GET_LICENSES_ERROR),
    START_JOB(
            "/odata/Jobs/UiPath.Server.Configuration.OData.StartJobs",
            HttpMethod.POST,
            new TypeReference<ODataResponseDto<JobResponseDto>>() {
            },
            UiPathApiErrorCode.START_JOB_ERROR),
    GET_MACHINE_SESSION_RUNTIME(
            "/odata/Sessions/UiPath.Server.Configuration.OData.GetMachineSessionRuntimes",
            HttpMethod.GET,
            new TypeReference<ODataResponseDto<MachineSessionRuntimeDto>>() {
            },
            UiPathApiErrorCode.GET_MACHINE_SESSION_RUNTIME_ERROR),
    RESTART_JOB(
            "/odata/Jobs/UiPath.Server.Configuration.OData.RestartJob",
            HttpMethod.POST,
            JobResponseDto.class,
            UiPathApiErrorCode.RESTART_JOB_ERROR),
    RESUME_JOB(
            "/odata/Jobs/UiPath.Server.Configuration.OData.ResumeJob",
            HttpMethod.POST,
            JobResponseDto.class,
            UiPathApiErrorCode.RESUME_JOB_ERROR),
    STOP_JOB(
            "/odata/Jobs(EntityId)/UiPath.Server.Configuration.OData.StopJob",
            HttpMethod.POST,
            String.class,
            UiPathApiErrorCode.STOP_JOB_ERROR),
    STOP_JOBS(
            "/odata/Jobs/UiPath.Server.Configuration.OData.StopJobs",
            HttpMethod.POST,
            String.class,
            UiPathApiErrorCode.STOP_JOBS_ERROR),
    GET_JOB(
            "/odata/Jobs(EntityId)",
            HttpMethod.GET,
            JobResponseDto.class,
            UiPathApiErrorCode.GET_JOB_ERROR),
    GET_JOBS(
            "/odata/Jobs",
            HttpMethod.GET,
            new TypeReference<ODataResponseDto<JobResponseDto>>() {
            },
            UiPathApiErrorCode.GET_JOBS_ERROR)
    ;

    private String url;
    private HttpMethod httpMethod;
    private Class responseClass;
    private TypeReference responseTypeReference;
    private UiPathApiErrorCode systemCode;

    UIPathApiEnum(String url, HttpMethod httpMethod, Class responseClass, UiPathApiErrorCode systemCode) {
        this.url = url;
        this.httpMethod = httpMethod;
        this.responseClass = responseClass;
        this.systemCode = systemCode;
    }

    UIPathApiEnum(String url, HttpMethod httpMethod, TypeReference responseTypeReference, UiPathApiErrorCode systemCode) {
        this.url = url;
        this.httpMethod = httpMethod;
        this.responseTypeReference = responseTypeReference;
        this.systemCode = systemCode;
    }


    public String getUrl(String baseUrl, UiPathUrlParam urlParam) {
        StringBuilder sb = new StringBuilder();
        sb.append(baseUrl);
        if (!baseUrl.equals("/") && !this.url.startsWith("/")) {
            sb.append("/");
        }
        String uri = this.url;
        if(this.url.contains("EntityId")){
            uri = uri.replace("EntityId", urlParam.getId());
        }
        sb.append(uri);
        if (urlParam != null) {
            sb.append("?").append(urlParam.toString());
        }
        return sb.toString();
    }

    public String getUrl(String baseUrl) {
        return getUrl(baseUrl, null);
    }

    public HttpMethod getHttpMethod() {
        return this.httpMethod;
    }

    public <T> Class<T> getResponseClass() {
        return this.responseClass;
    }

    public <T> TypeReference<T> getResponseTypeReference() {
        return this.responseTypeReference;
    }

    public UiPathApiErrorCode getSystemCode() {
        return this.systemCode;
    }

}
