package pwc.taxtech.atms.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import pwc.taxtech.atms.dto.ApiResultDto;
import pwc.taxtech.atms.service.impl.ReportAnalysisService;

import javax.annotation.Resource;
import java.util.Collections;

@RestController
@RequestMapping("/api/v1/reportAnalysis")
public class ReportAnalysisController extends BaseController {
    @Resource
    private ReportAnalysisService reportAnalysisService;

    @GetMapping("/burdenRateData/{projectId}")
    public ApiResultDto getBurdenRateData(@PathVariable String projectId) {
        if (StringUtils.isBlank(projectId)) {
            return ApiResultDto.success(Collections.emptyList());
        }
        try {
            return ApiResultDto.success(reportAnalysisService.getBurdenRateData(projectId));
        } catch (Exception e) {
            logger.error("getBurdenRateData error.", e);
        }
        return ApiResultDto.fail();
    }

    @GetMapping("/dispersion/{projectId}")
    public ApiResultDto dispersion(@PathVariable String projectId) {
        if (StringUtils.isBlank(projectId)) {
            return ApiResultDto.success(Collections.emptyList());
        }
        try {
            return ApiResultDto.success(reportAnalysisService.getDispersion(projectId));
        } catch (Exception e) {
            logger.error("getDispersion error.", e);
        }
        return ApiResultDto.fail();
    }

    @GetMapping("/incomeRate/{projectId}")
    public ApiResultDto incomeRate(@PathVariable String projectId) {
        if (StringUtils.isBlank(projectId)) {
            return ApiResultDto.success(Collections.emptyList());
        }
        try {
            return ApiResultDto.success(reportAnalysisService.getIncomeRate(projectId));
        } catch (Exception e) {
            logger.error("incomeRate error.", e);
        }
        return ApiResultDto.fail();
    }

    @GetMapping("/incomeVolatility/{projectId}")
    public ApiResultDto incomeVolatility(@PathVariable String projectId) {
        if (StringUtils.isBlank(projectId)) {
            return ApiResultDto.success(Collections.emptyList());
        }
        try {
            return ApiResultDto.success(reportAnalysisService.getIncomeVolatility(projectId));
        } catch (Exception e) {
            logger.error("incomeVolatility error.", e);
        }
        return ApiResultDto.fail();
    }

    @GetMapping("/vatIncomeRate/{projectId}/{period}")
    public ApiResultDto getVatIncomeRate(@PathVariable String projectId, @PathVariable Integer period) {
        if (StringUtils.isBlank(projectId)) {
            return ApiResultDto.success(Collections.emptyList());
        }
        try {
            return ApiResultDto.success(reportAnalysisService.getVatIncomeRate(projectId, period));
        } catch (Exception e) {
            logger.error("getVatIncomeRate error.", e);
        }
        return ApiResultDto.fail();
    }

    @GetMapping("/vatIncomeLine/{projectId}")
    public ApiResultDto getVatIncomeLine(@PathVariable String projectId) {
        if (StringUtils.isBlank(projectId)) {
            return ApiResultDto.success(Collections.emptyList());
        }
        try {
            return ApiResultDto.success(reportAnalysisService.getVatIncomeLine(projectId));
        } catch (Exception e) {
            logger.error("getVatIncomeLine error.", e);
        }
        return ApiResultDto.fail();
    }

    @GetMapping("/deduction/{projectId}")
    public ApiResultDto getDeduction(@PathVariable String projectId) {
        if (StringUtils.isBlank(projectId)) {
            return ApiResultDto.success(Collections.emptyList());
        }
        try {
            return ApiResultDto.success(reportAnalysisService.getDeduction(projectId));
        } catch (Exception e) {
            logger.error("getDeduction error.", e);
        }
        return ApiResultDto.fail();
    }
}