package com.arms.api.almproject.strategy;

import com.arms.api.almproject.model.dto.ProjectDTO;
import com.arms.api.almproject.model.vo.ProjectVO;
import com.arms.api.serverinfo.model.vo.ServerInfoVO;
import com.arms.api.serverinfo.service.ServerInfoService;
import com.arms.api.util.alm.JiraApi;
import com.arms.api.util.alm.JiraUtil;
import com.arms.api.util.errors.ErrorLogUtil;
import lombok.AllArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
@AllArgsConstructor
public class CloudJiraProjectStrategy implements ProjectStrategy {

    private final JiraUtil jiraUtil;

    private final JiraApi jiraApi;

    private final ServerInfoService serverInfoService;

    @Override
    public ProjectVO getProject(ServerInfoVO serverInfoVO, String projectKeyOrId) {
        String endpoint = jiraApi.getEndpoint().getProject() + "/" + projectKeyOrId;
        WebClient webClient = jiraUtil.createJiraCloudCommunicator(serverInfoVO.getUri(), serverInfoVO.getUserId(), serverInfoService.getDecryptPasswordOrToken(serverInfoVO.getPasswordOrToken()));

        try {
            ProjectDTO projectDTO = jiraUtil.get(webClient, endpoint, ProjectDTO.class).block();
            return this.convertProjectVO(projectDTO);
        }
        catch (Exception e) {
            String errorMessage = ErrorLogUtil.exceptionLoggingAndReturn(e, this.getClass().getName(),
                    String.format("%s[%s] :: getProject Error 프로젝트[%s] ",
                            serverInfoVO.getType(), serverInfoVO.getUri(), projectKeyOrId));
            throw new IllegalArgumentException(errorMessage);
        }
    }

    @Override
    public List<ProjectVO> getProjectList(ServerInfoVO serverInfoVO) {
        String endpoint = jiraApi.getEndpoint().getProject();
        WebClient webClient = jiraUtil.createJiraCloudCommunicator(serverInfoVO.getUri(), serverInfoVO.getUserId(), serverInfoService.getDecryptPasswordOrToken(serverInfoVO.getPasswordOrToken()));

        try {
            List<ProjectDTO> projectList = jiraUtil.get(webClient, endpoint,
                    new ParameterizedTypeReference<List<ProjectDTO>>() {}).block();

            return Optional.ofNullable(projectList)
                    .orElse(Collections.emptyList())
                    .stream()
                    .filter(Objects::nonNull)
                    .map(this::convertProjectVO)
                    .collect(Collectors.toList());
        }
        catch (Exception e) {
            String errorMessage = ErrorLogUtil.exceptionLoggingAndReturn(e, this.getClass().getName(),
                    String.format("%s[%s] :: getProjectList Error", serverInfoVO.getType(), serverInfoVO.getUri()));
            throw new IllegalArgumentException(errorMessage);
        }
    }

    private ProjectVO convertProjectVO(ProjectDTO projectDTO) {
        return ProjectVO.builder()
                .id(projectDTO.getId())
                .key(projectDTO.getKey())
                .name(projectDTO.getName())
                .self(projectDTO.getSelf()).build();
    }
}