package com.arms.api.almproject.strategy;

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.JiraUtil;
import com.arms.api.util.errors.ErrorLogUtil;
import com.atlassian.jira.rest.client.api.JiraRestClient;
import com.atlassian.jira.rest.client.api.domain.BasicProject;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Component
@AllArgsConstructor
public class OnpremiseJiraProjectStrategy implements ProjectStrategy {

    private final JiraUtil jiraUtil;

    private final ServerInfoService serverInfoService;

    @Override
    public ProjectVO getProject(ServerInfoVO serverInfoVO, String projectKeyOrId) {

        try(JiraRestClient restClient = jiraUtil.createJiraOnPremiseCommunicator(serverInfoVO.getUri(),
                serverInfoVO.getUserId(),
                serverInfoService.getDecryptPasswordOrToken(serverInfoVO.getPasswordOrToken()))) {
            BasicProject basicProject = restClient.getProjectClient()
                    .getProject(projectKeyOrId)
                    .claim();

            return this.convertProjectVO(basicProject);
        }
        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) {

        try (JiraRestClient restClient = jiraUtil.createJiraOnPremiseCommunicator(serverInfoVO.getUri(),
                serverInfoVO.getUserId(),
                serverInfoService.getDecryptPasswordOrToken(serverInfoVO.getPasswordOrToken()))){
            Iterable<BasicProject> basicProjectList = restClient.getProjectClient()
                    .getAllProjects()
                    .claim();

            return StreamSupport.stream(basicProjectList.spliterator(), false)
                    .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(BasicProject project) {
        return ProjectVO.builder()
                .id(project.getId().toString())
                .key(project.getKey())
                .name(project.getName())
                .self(project.getSelf().toString()).build();
    }
}