package pwc.taxtech.atms.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pwc.taxtech.atms.common.message.ErrorMessage;
import pwc.taxtech.atms.controller.BaseController;
import pwc.taxtech.atms.dto.ApiResultDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.analysis.AnalysisDomesticlParam;
import pwc.taxtech.atms.dto.analysis.AnalysisInternationlParam;
import pwc.taxtech.atms.dto.vatdto.CertifiedInvoicesListParam;
import pwc.taxtech.atms.exception.ServiceException;
import pwc.taxtech.atms.service.impl.AnalysisServiceImpl;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Auther: Gary J Li
 * @Date: 14/03/2019 11:29
 * @Description:
 */
@RestController
@RequestMapping("/api/v1/Analysis/")
public class AnalysisController extends BaseController {

    @Autowired
    private AnalysisServiceImpl analysisServiceImpl;

    @ResponseBody
    @RequestMapping(value = "displayAnalysisImportData", method = RequestMethod.POST)
    public ApiResultDto displayAnalysisImportData(@RequestBody AnalysisDomesticlParam param) {
        try{
            return ApiResultDto.success(analysisServiceImpl.displayAnalysisImportData(param));
        }catch (Exception e){
            return ApiResultDto.fail();
        }
    }

    @ResponseBody
    @RequestMapping(value = "displayAnalysisInternationalImportData", method = RequestMethod.POST)
    public ApiResultDto displayAnalysisInternationalImportData(@RequestBody AnalysisInternationlParam param) {
        try{
            return ApiResultDto.success(analysisServiceImpl.displayAnalysisInternationalImportData(param));
        }catch (Exception e){
            return ApiResultDto.fail();
        }
    }

    @RequestMapping(value = "downloadDomesticFile/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void downloadDomesticFile(@RequestBody AnalysisDomesticlParam param, HttpServletResponse response) {
        logger.debug("enter downloadDomesticFile");
        String fileName="testFile";
        analysisServiceImpl.downloadDomesticFile(response, param, fileName);
    }

    @RequestMapping(value = "downloadInternationalFile/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void downloadInternationalFile(@RequestBody AnalysisInternationlParam param, HttpServletResponse response) {
        logger.debug("enter downloadInternationalFile");
        String fileName="testFile";
        analysisServiceImpl.downloadInternationalFile(response, param, fileName);
    }


    @ResponseBody
    @RequestMapping(value = "getAnalysisInternationalCompanyList", method = RequestMethod.GET)
    public List<String> getAnalysisInternationalCompanyList(@RequestParam Integer type, @RequestParam String period) {
        return analysisServiceImpl.getAnalysisInternationalCompanyList(type,period);
    }

    @ResponseBody
    @RequestMapping(value = "getAnalysisInternationalCountryList", method = RequestMethod.GET)
    public List<String> getAnalysisInternationalCountryList(@RequestParam Integer type, @RequestParam String period) {
        return analysisServiceImpl.getAnalysisInternationalCountryList(type,period);
    }

    @ResponseBody
    @RequestMapping(value = "DomesitcExcelFile", method = RequestMethod.POST)
    public OperationResultDto importDomesitcExcelFile(@RequestParam MultipartFile file, @RequestParam String period, @RequestParam Integer type) {
        try {
            String valMsg = valParameter(file,period,type);
            if(StringUtils.isNotEmpty(valMsg)){
                return OperationResultDto.error(valMsg);
            }
            return analysisServiceImpl.importDomesitcExcelFile(file,period, type);
        } catch (ServiceException e) {
            return OperationResultDto.error(e.getMessage());
        } catch (Exception e) {
            logger.error("importDomesitcExcelFile error.", e);
            return OperationResultDto.error(ErrorMessage.SystemError);
        }
    }

    @ResponseBody
    @RequestMapping(value = "InternationalExcelFile", method = RequestMethod.POST)
    public OperationResultDto importDomesitcExcelFile(@RequestParam MultipartFile file, @RequestParam String period,
                                                      @RequestParam Integer type,@RequestParam String companyName,@RequestParam String country) {
        try {
            String valMsg = valParameter(file,period,type);
            if(StringUtils.isNotEmpty(valMsg)){
                return OperationResultDto.error(valMsg);
            }
            return analysisServiceImpl.importInterNationalExcelFile(file,period, type,companyName,country);
        } catch (ServiceException e) {
            return OperationResultDto.error(e.getMessage());
        } catch (Exception e) {
            logger.error("importDomesitcExcelFile error.", e);
            return OperationResultDto.error(ErrorMessage.SystemError);
        }
    }

    private String valParameter(MultipartFile file,String periodDate,Integer type){
        if (null == file) {
            return ErrorMessage.NoFile;
        }
        if(StringUtils.isEmpty(periodDate)){
            return ErrorMessage.DidntSelectedPeriod;
        }
        if(null==type){
            return ErrorMessage.DidntSelectedImportType;
        }
        return null;
    }
}