package com.viettel.campaign.web.rest;

import com.viettel.campaign.service.CustomerListService;
import com.viettel.campaign.web.dto.CustomerListDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/ipcc/customerlist")
@CrossOrigin(origins = "*")
public class CustomerListController {

    @Autowired(required=true)
    CustomerListService customerListService;

    @GetMapping("/findAll")
    @ResponseBody
    public ResponseEntity findAllCustomerList(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam("sort") String sort) {
        Map result = customerListService.getCustomerList(page, pageSize, sort);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @PostMapping("/create")
    @ResponseBody
    public ResultDTO createCustomerList(@RequestBody @Valid CustomerListDTO customerListDTO) {
        ResultDTO result = new ResultDTO();
        //LogUtil logUtil = new LogUtil();
        //logUtil.initKpiLog("createCust")
        try {
            //LOGGER.info("Returning createCustomer: start");
            result = customerListService.createCustomerList(customerListDTO);
            //LOGGER.info("Returning createCustomer:" + result.getErrorCode());
            //logUtil.endKpiLog(customerDTO, 0, result.getErrorCode(), result.getDetail(), CustomerController.class, customerDTO.getAgentProcess(), this.serverPort);
        } catch (Exception e) {
            result.setErrorCode("-1");
//            LOGGER.error(e);
            //logUtil.endKpiLog(customerDTO, 1, result.getErrorCode(), e.getMessage(), CustomerController.class, customerDTO.getAgentProcess(), this.serverPort);
        }
        return result;
//        return new ResponseEntity<>("", HttpStatus.OK);
    }

    @PutMapping("/update")
    @ResponseBody
    public ResultDTO updateCompleteCode(@RequestBody @Valid CustomerListDTO customerListDTO) {
        ResultDTO result = new ResultDTO();
        //LogUtil logUtil = new LogUtil();
        //logUtil.initKpiLog("createCust");
        try {
            //LOGGER.info("Returning createCustomer: start");
            result = customerListService.updateCustomerList(customerListDTO);
            //LOGGER.info("Returning createCustomer:" + result.getErrorCode());
            //logUtil.endKpiLog(customerDTO, 0, result.getErrorCode(), result.getDetail(), CustomerController.class, customerDTO.getAgentProcess(), this.serverPort);
        } catch (Exception e) {
            result.setErrorCode("-1");
//            LOGGER.error(e);
            //logUtil.endKpiLog(customerDTO, 1, result.getErrorCode(), e.getMessage(), CustomerController.class, customerDTO.getAgentProcess(), this.serverPort);
        }
        return result;
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResultDTO deleteCompleteCode(@RequestBody @Valid CustomerListDTO customerListDTO) {
        ResultDTO result = new ResultDTO();
        result = customerListService.deleteCustomerList(customerListDTO);
        return result;
    }

    @PostMapping("/ids")
    @ResponseBody
    public ResultDTO deleteIds(@RequestBody @Valid List<Long> ids) {
        ResultDTO result = new ResultDTO();
        result = customerListService.deleteIds(ids);
        return result;
    }
}
