package pwc.taxtech.atms.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pwc.taxtech.atms.dto.CellTemplateConfigDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.service.impl.CellTemplateServiceImpl;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping(value = "api/v1/celltemplate")
public class CellTemplateController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(AccountController.class);
    @Autowired
    CellTemplateServiceImpl cellTemplateService;

    @RequestMapping(value = "configList/{templateId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto<List<CellTemplateConfigDto>> getConfigList(@PathVariable Long templateId) {
        OperationResultDto<List<CellTemplateConfigDto>> result = new OperationResultDto<>();

        if (!isParamValid(templateId)) {
            result.setData(Collections.emptyList());
        }

        try {
            return cellTemplateService.getCellConfigList(templateId);
        } catch (Exception e) {
            logger.error("CellTemplateController.GetCellConfigList", e);
        }

        return result;
    }

    @RequestMapping(value = "config/{cellTemplateId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto<CellTemplateConfigDto> getConfig(@PathVariable Long cellTemplateId) {
        if (!isParamValid(cellTemplateId)) {
            return new OperationResultDto<>();
        }

        try {
            return cellTemplateService.getCellConfig(cellTemplateId);
        } catch (Exception e) {
            logger.error("CellTemplateController.GetConfig", e);
        }

        return new OperationResultDto<>();
    }

    @RequestMapping(value = "config", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody
    OperationResultDto postConfig(@RequestBody CellTemplateConfigDto cellTemplateConfigDto) {
        return cellTemplateService.saveOrEdit(cellTemplateConfigDto);
    }
}