package com.codenetworkz.blog.externalcall;

import com.codenetworkz.blog.config.RestTemplateConfig;
import com.codenetworkz.blog.web.dtos.RequestTaskDto;
import com.codenetworkz.blog.web.dtos.ResponseTaskDto;
import com.codenetworkz.blog.web.dtos.ResponseTaskListDto;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;


import java.util.UUID;

@Service
public class ExternalCallTaskServiceImpl implements ExternalCallTaskService {

    private final RestTemplateConfig restTemplateConfig;

    @Value("${service.task.url}")
    private String fileUploadUrl;

   public ExternalCallTaskServiceImpl(RestTemplateConfig restTemplateConfig) {
        this.restTemplateConfig = restTemplateConfig;
   }

    @Override
    public ResponseTaskDto saveTask(String taskName, String description) {
        RequestTaskDto requestTaskDto = new RequestTaskDto();
        requestTaskDto.setTitle(taskName);
        requestTaskDto.setDesc(description);

        ResponseEntity responseEntity = restTemplateConfig.getRestTemplate().postForObject(fileUploadUrl, requestTaskDto, ResponseEntity.class);
        if(responseEntity.getStatusCode().is2xxSuccessful()) {
            ResponseTaskDto body = (ResponseTaskDto) responseEntity.getBody();
            return body;
        }
        return null;
    }

    @Override
    public ResponseTaskListDto getAllTask() {

        ResponseEntity<ResponseTaskListDto> responseEntity = restTemplateConfig.getRestTemplate().getForEntity(fileUploadUrl, ResponseTaskListDto.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return  responseEntity.getBody();
        }
        return null;
    }


    @Override
    public ResponseTaskDto getTaskById(UUID taskId) {
        ResponseEntity<ResponseTaskDto> responseEntity = restTemplateConfig.getRestTemplate().getForEntity(fileUploadUrl+"/"+taskId, ResponseTaskDto.class);

        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return  responseEntity.getBody();
        }
        return null;
    }

    @Override
    public ResponseTaskDto updateTask(UUID taskId, String taskName, String description) {
        RequestTaskDto requestTaskDto = new RequestTaskDto();
        requestTaskDto.setTitle(taskName);
        requestTaskDto.setDesc(description);

        ResponseEntity<ResponseTaskDto> responseEntity = restTemplateConfig.getRestTemplate()
                .exchange(fileUploadUrl + "/" + taskId, HttpMethod.PUT, new HttpEntity<>(requestTaskDto), ResponseTaskDto.class);

        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return  responseEntity.getBody();
        }
        return null;
    }

    @Override
    public ResponseTaskDto deleteTask(UUID taskId) {
        ResponseEntity<ResponseTaskDto> responseEntity = restTemplateConfig.getRestTemplate().getForEntity(fileUploadUrl+"/"+taskId, ResponseTaskDto.class);

        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return responseEntity.getBody();
        }
        return null;
    }

    @Override
    public ResponseTaskDto completeTask(UUID taskId) {
        String url = fileUploadUrl + "/" + taskId + "/complete";

        ResponseEntity<ResponseTaskDto> responseEntity = restTemplateConfig.getRestTemplate().getForEntity(url, ResponseTaskDto.class);

        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            return responseEntity.getBody();
        }
       return null;
    }

}
