package com.viettel.campaign.service.impl;

import com.viettel.campaign.mapper.CustomerListMapper;
import com.viettel.campaign.model.CustomerList;
import com.viettel.campaign.repository.CampaignCustomerListRepository;
import com.viettel.campaign.repository.CustomerListRepository;
import com.viettel.campaign.service.CustomerListService;
import com.viettel.campaign.utils.Constants;
import com.viettel.campaign.utils.DataUtil;
import com.viettel.campaign.web.dto.CustomerListDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.*;

@Service
public class CustomerListServiceImpl implements CustomerListService {

    @Autowired
    EntityManager entityManager;

    @Autowired
    CustomerListRepository customerListRepository;

    @Autowired
    CampaignCustomerListRepository campaignCustomerListRepository;

    private Logger logger = LogManager.getLogger(CustomerListServiceImpl.class);

    @Override
    public Map getCustomerList(int page, int pageSize, String sort) {
        Map result = new HashMap();
        List<CustomerList> list = new ArrayList<>();

        Pageable pageable = PageRequest.of(page, pageSize, Sort.by(sort));
        Page<CustomerList> pc = customerListRepository.findAll(pageable);

        result.put("totalItem", pc.getTotalElements());
        result.put("customerList", pc.iterator());

        return result;
    }

    @Override
    public ResultDTO createCustomerList(CustomerListDTO customerListDTO) {
        ResultDTO resultDTO = new ResultDTO();
        CustomerListMapper customerListMapper = new CustomerListMapper();
        Date today = new Date();
        CustomerList customerList = new CustomerList();
        try {
            if (customerListDTO != null) {
                // insert
                CustomerList findCustomer = customerListRepository.findByCustomerListCode(customerListDTO.getCustomerListCode());
                if (findCustomer == null) {
                    customerList = customerListMapper.toPersistenceBean(customerListDTO);
                    customerListRepository.save(customerList);

                    resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
                    resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
                } else {
                    resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
                    resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
                }
            } else {
                resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
                resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Override
    public ResultDTO updateCustomerList(CustomerListDTO customerListDTO) {
        ResultDTO resultDTO = new ResultDTO();
        CustomerListMapper customerListMapper = new CustomerListMapper();
        Date today = new Date();
        CustomerList customerList = customerListRepository.findCustomerListByCustomerListId(customerListDTO.getCustomerListId());

        try {
            if (customerListDTO != null) {
                // update
                customerList = customerListMapper.toPersistenceBean(customerListDTO);
                customerList = customerListRepository.save(customerList);

                resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
                resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
            } else {
                resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
                resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Override
    public ResultDTO deleteCustomerList(CustomerListDTO customerListDTO) {
        ResultDTO resultDTO = new ResultDTO();
        CustomerListMapper customerListMapper = new CustomerListMapper();
        Date today = new Date();
        CustomerList customerList = new CustomerList();
        try {
            if (customerListDTO != null) {
                // delete
                if (DataUtil.isNullOrZero(campaignCustomerListRepository.campaignCount(customerListDTO.getCustomerListId()))) {
                    customerListRepository.deleteById(customerListDTO.getCustomerListId());

                    resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
                    resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
                } else {
                    resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
                    resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
                }

            } else {
                resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
                resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Transactional
    @Override
    public ResultDTO deleteIds(List<Long> ids) {
        ResultDTO resultDTO = new ResultDTO();
        try {
            if (ids != null) {
                if (DataUtil.isNullOrZero(campaignCustomerListRepository.campaignIdsCount(ids))) {
                    customerListRepository.deleteIds(ids);
                    resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
                    resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
                } else {
                    resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
                    resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
                }
            } else {
                resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
                resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultDTO;
    }

    @Override
    public Map searchCustomerList(String customerListCode, String customerListName, Date dateFrom, Date dateTo, int page, int pageSize, String sort) {
        Map result = new HashMap();
        List<CustomerList> list = new ArrayList<>();

        Pageable pageable = PageRequest.of(page, pageSize, Sort.by(sort));
        Page<CustomerList> pc = customerListRepository.searchCustomerList(customerListCode, customerListName, dateFrom, dateTo, pageable);

        result.put("totalItem", pc.getTotalElements());
        result.put("searchList", pc.iterator());

        return result;
    }
}
