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.*;
import pwc.taxtech.atms.dto.CellTemplateConfigDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.service.CellTemplateService;

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
    CellTemplateService 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);
    }
}