package com.viettel.campaign.web.rest;

import com.viettel.campaign.model.ApParam;
import com.viettel.campaign.service.ApParamService;
import com.viettel.campaign.web.rest.controller.CustomerController;
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 java.util.List;

@Controller
@RequestMapping("/ipcc/apParam")
@CrossOrigin(origins = "*")
public class ApParamController {

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

    @Autowired(required = true)
    ApParamService apParamService;

    @GetMapping(path = "/findAlls")
    @ResponseBody
    public ResponseEntity listAllCustomer(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam("sort") String sort) {
        Iterable<ApParam> listCustomer = apParamService.getAllParams(page, pageSize, sort);
        if (listCustomer == null) {
            return new ResponseEntity(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity(listCustomer.iterator(), HttpStatus.OK);
    }

    @PostMapping(path = "/findByName")
    @ResponseBody
    public ResponseEntity searchParamByName(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam("sort") String sort, @RequestParam(name = "parName") String parName) {
        List<ApParam> lst = apParamService.getParamByName(page, pageSize, sort, parName);
        if (lst.isEmpty())
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);

        return new ResponseEntity(lst, HttpStatus.OK);
    }

    @RequestMapping(path = "/findParamByParType", method = RequestMethod.GET)
    public ResponseEntity findParamByParType(@RequestParam String parType) {
        return new ResponseEntity<>(apParamService.findParamByParType(parType), HttpStatus.OK);
    }

    @GetMapping(path = "/findAllParam")
    @ResponseBody
    public ResponseEntity findAllParam() {
        List<ApParam> findAll = apParamService.findAllParam();
        if(findAll.isEmpty()) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return new ResponseEntity(findAll, HttpStatus.OK);
    }
}

