package com.viettel.campaign.web.rest.controller;

import com.viettel.campaign.web.dto.CustomerDTO;
import com.viettel.campaign.web.dto.CustomerListDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.service.CustomerService;
import org.apache.log4j.Logger;
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.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ipcc/customer")
@CrossOrigin("*")
public class CustomerController {

    private static final Logger LOGGER = Logger.getLogger(CustomerController.class);

    @Autowired(required=true)
    CustomerService customerService;

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

    @GetMapping("/findCustomerByName")
    @ResponseBody
    public ResponseEntity findAllCustomerName(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam("sort") String sort, @RequestParam("name") String name) {
        Map result = customerService.listCustByName(page, pageSize, sort, name);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @PostMapping(value = "/create")
    @ResponseBody
    public ResultDTO createCustomer(@RequestBody @Valid CustomerDTO customerDTO) {
        ResultDTO result = new ResultDTO();
        //LogUtil logUtil = new LogUtil();
        //logUtil.initKpiLog("createCust");
        try {
            //LOGGER.info("Returning createCustomer: start");
            result = customerService.createCustomer(customerDTO);
            //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 deleteCustomer(@RequestBody @Valid CustomerDTO customerDTO) {
        ResultDTO result = new ResultDTO();
        result = customerService.deleteCustomer(customerDTO);
        return result;
    }

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


    // --------------- customer list table ----------------- //

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

    @PostMapping("/createCustomerList")
    @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 = customerService.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("/updateCustomerList")
    @ResponseBody
    public ResultDTO updateCustomerList(@RequestBody @Valid CustomerListDTO customerListDTO) {
        ResultDTO result = new ResultDTO();
        //LogUtil logUtil = new LogUtil();
        //logUtil.initKpiLog("createCust");
        try {
            //LOGGER.info("Returning createCustomer: start");
            result = customerService.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("/deleteCustomerList")
    @ResponseBody
    public ResultDTO deleteCustomerList(@RequestBody @Valid CustomerListDTO customerListDTO) {
        ResultDTO result = new ResultDTO();
        result = customerService.deleteCustomerList(customerListDTO);
        return result;
    }

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

    @GetMapping("/searchCustomerList")
    @ResponseBody
    public ResponseEntity searchCustomerList(@RequestParam("customerListCode") String customerListCode, @RequestParam("customerListName") String customerListName, @RequestParam("dateFrom") Date dateFrom, @RequestParam("dateTo") Date dateTo, @RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam("sort") String sort) {
        ResultDTO result = customerService.searchCustomerList(customerListCode, customerListName, dateFrom, dateTo, page, pageSize, sort);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }
}
