package com.viettel.campaign.web.rest;

import com.viettel.campaign.model.ccms_full.Customer;
import com.viettel.campaign.model.ccms_full.CustomizeFieldObject;
import com.viettel.campaign.model.ccms_full.CustomizeFields;
import com.viettel.campaign.service.CustomerService;
import com.viettel.campaign.utils.BundleUtils;
import com.viettel.campaign.utils.Constants;
import com.viettel.campaign.utils.RedisUtil;
import com.viettel.campaign.web.dto.*;
import com.viettel.campaign.web.dto.request_dto.CustomerRequestDTO;
import com.viettel.campaign.web.dto.request_dto.CustomizeRequestDTo;
import com.viettel.campaign.web.dto.request_dto.SearchCustomerRequestDTO;
import com.viettel.econtact.filter.UserSession;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.*;

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

    private static final Logger LOGGER = LoggerFactory.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, @RequestParam("customerListId") long customerListId, @RequestParam("companySiteId") long companySiteId) {
        ResultDTO result = customerService.getAllCustomer(page, pageSize, sort, customerListId, companySiteId);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @GetMapping("/findCustomerId")
    @ResponseBody
    public ResponseEntity<ResultDTO> findAllCustomerName(@RequestParam("customerId") Long customerId) {
        ResultDTO result = customerService.getCustomerId(customerId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @GetMapping("/searchAllCustomerByParams")
    @ResponseBody
    public ResponseEntity findAllCustomerByParams(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam("sort") String sort, @RequestParam("customerListId") Long customerListId, @RequestParam("companySiteId") Long companySiteId, @RequestParam("name") String name, @RequestParam("mobileNumber") String mobileNumber, @RequestParam("email") String email) {
        ResultDTO result = customerService.searchAllCustomer(page, pageSize, sort, customerListId, companySiteId, name, mobileNumber, email);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @PostMapping(value = "/create")
    @ResponseBody
    public ResultDTO createCustomer(@RequestBody @Valid CustomerDTO customerDTO) {
        ResultDTO result = new ResultDTO();
        result = customerService.createCustomer(customerDTO);
        return result;
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResultDTO deleteCustomer(@RequestBody @Valid CustomerRequestDTO customerRequestDTO) {
        ResultDTO result = new ResultDTO();
        result = customerService.deleteCustomer(customerRequestDTO);
        return result;
    }

    @PostMapping("/deleteIds")
    @ResponseBody
    public ResultDTO deleteIds(@RequestBody @Valid CustomerRequestDTO customerRequestDTO) {
        ResultDTO result = new ResultDTO();
        result = customerService.deleteIds(customerRequestDTO);
        return result;
    }

    @GetMapping("/getInformation")
    @ResponseBody
    public ResponseEntity getAllInformation(@RequestParam("companySiteId") Long companySiteId, @RequestParam("customerListId") Long customerListId, @RequestParam("customerId") Long customerId) {
        ResultDTO result = customerService.getCustomerDetailById(companySiteId, customerListId, customerId);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


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

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

    @PostMapping("/createCustomerList")
    @ResponseBody
    public ResultDTO createCustomerList(@RequestBody @Valid CustomerListDTO customerListDTO, HttpServletRequest request) {
        ResultDTO result = new ResultDTO();

        String xAuthToken = request.getHeader("X-Auth-Token");
        UserSession userSession = (UserSession) RedisUtil.getInstance().get(xAuthToken);

        result = customerService.createCustomerList(customerListDTO, userSession.getUserName());
        return result;
    }

    @PostMapping("/updateCustomerList")
    @ResponseBody
    public ResultDTO updateCustomerList(@RequestBody @Valid CustomerListDTO customerListDTO) {
        ResultDTO result = new ResultDTO();
        result = customerService.updateCustomerList(customerListDTO);
        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 CustomerRequestDTO customerRequestDTO) {
        ResultDTO result = new ResultDTO();
        result = customerService.deleteCustomerListIds(customerRequestDTO);
        return result;
    }

    @RequestMapping(value = "/searchCustomerList", method = RequestMethod.POST)
    public ResponseEntity searchCustomerList(@RequestBody SearchCustomerRequestDTO searchCustomerRequestDTO) {
        ResultDTO result = customerService.searchCustomerList(searchCustomerRequestDTO);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @GetMapping("/latestCreated")
    @ResponseBody
    public ResponseEntity getLatestCreated(@RequestParam("companySiteId") Long companySiteId) {
        ResultDTO result = new ResultDTO();
        result.setData(customerService.getLatestCreated(companySiteId));
        return new ResponseEntity<>(result, HttpStatus.OK);
    }

    @PostMapping("/findCustomerContact")
    @ResponseBody
    public ResponseEntity<ResultDTO> findAllCustomerContact(@RequestBody CustomerContactDTO customerContactDTO) {
        ResultDTO result = customerService.getCustomerContact(customerContactDTO);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @PostMapping("/getCustomerRecall")
    @ResponseBody
    public ResponseEntity<ResultDTO> getCustomerRecall(@RequestParam("campaignId") Long campaignId, @RequestParam("customerId") Long customerId) {
        ResultDTO result = customerService.getCustomerRecall(campaignId, customerId);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    //<editor-fold desc="Download and import excel" defaultstate="collapsed">
    @GetMapping(value = "/downloadFileTemplate")
    public ResponseEntity<byte[]> downloadFileTemplate(@RequestParam("companySiteId") Long companySiteId) {
        LOGGER.info("--------DOWNLOAD FILE TEMPLATE---------");
        try {
            byte[] content = customerService.buildTemplate(companySiteId);
            return ResponseEntity.ok()
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template_import_customer.xlsx")
                    .header("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX)
                    .body(content);
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }

    @PostMapping(value = "/importFile")
    public ResponseEntity<?> importFile(@RequestParam("file") MultipartFile file,
                                        @RequestParam("customerListId") Long customerListId,
                                        @RequestHeader("X-Auth-Token") String authToken) {
        LOGGER.info("------------IMPORT FILE TEMPLATE--------------");
        Locale locale = new Locale("vi", "VN");
        try {
            UserSession userSession = (UserSession) RedisUtil.getInstance().get(authToken);
            if (file.isEmpty()) {
                return new ResponseEntity<>(BundleUtils.getLangString("customer.fileNotSelected"), HttpStatus.OK);
            }
            if (!Objects.equals(FilenameUtils.getExtension(file.getOriginalFilename()), Constants.FileType.xlsx)) {
                return new ResponseEntity<>(BundleUtils.getLangString("customer.invalidTemplate", locale), HttpStatus.OK);
            }
            String path = saveUploadFile(file);
            List<CustomizeFields> dynamicHeaders = customerService.getDynamicHeader(userSession.getCompanySiteId());
            Map<String, Object> map = customerService.readAndValidateCustomer(path, dynamicHeaders, userSession, customerListId);
            File fileExport = (File) map.get("file");
            String message = (String) map.get("message");
            return ResponseEntity.ok()
                    .header("Content-Type", Constants.MIME_TYPE.EXCEL_XLSX)
                    .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=template_import_customer.xlsx")
                    .header("Message", message)
                    .body(Files.readAllBytes(fileExport.toPath()));
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
    }
    //</editor-fold>

    @PostMapping("/getCustomerListInfo")
    public ResponseEntity<?> getCustomerListInfo(@RequestBody CampaignCustomerDTO dto) {
        List<CustomerListDTO> customers = customerService.getCustomerListInfo(dto);
        return new ResponseEntity<>(customers, HttpStatus.OK);
    }

    @PostMapping("/getIndividualCustomerInfo")
    public ResponseEntity<?> getIndividualCustomerInfo(@RequestBody CampaignCustomerDTO dto) {
        List<CustomerDTO> customers = customerService.getIndividualCustomerInfo(dto);
        return new ResponseEntity<>(customers, HttpStatus.OK);
    }

    @PostMapping("/getCustomizeField")
    public ResponseEntity<?> getCustomizeField(@RequestBody Long customerId) {
        List<CustomizeFieldObject> data = customerService.getCustomizeField(customerId);
        return new ResponseEntity<>(data, HttpStatus.OK);
    }

    private String saveUploadFile(MultipartFile file) {
        try {
            String currentTime = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss").format(new Date());
            String fileName = FilenameUtils.getBaseName(file.getOriginalFilename()) + "_" + currentTime + "." + FilenameUtils.getExtension(file.getOriginalFilename());
            byte[] content = file.getBytes();
            File uploadFolder = new File(BundleUtils.getGlobalConfig("import.uploadFolder"));
            if (!uploadFolder.exists()) {
                uploadFolder.mkdir();
            }
            Path path = Paths.get(BundleUtils.getGlobalConfig("import.uploadFolder"), fileName);
            Files.write(path, content);
            return path.toString();
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
        return null;
    }
    @GetMapping(path = "", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<List<Customer>> query(@RequestParam(value = "search") String query) {
        List<Customer> result = null;
        try {
            result= customerService.searchByQuery(query);
        }catch (IllegalArgumentException iae){
            return  ResponseEntity.status(HttpStatus.BAD_REQUEST)
                    .body(result);
        }
        return  ResponseEntity.status(HttpStatus.OK)
                .body(result);
    }
    @PostMapping("/getCustomizeFields")
    @ResponseBody
    public ResponseEntity<?> getListFieldsToShow(@RequestBody CustomizeRequestDTo customizeRequestDTo) {
        ResultDTO resultDTO = customerService.searchCustomizeFields(customizeRequestDTo);
        return new ResponseEntity<>(resultDTO, HttpStatus.OK);
    }
}
