package com.viettel.campaign.service;

import com.viettel.campaign.web.dto.CampaignCompleteCodeDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.mapper.CampaignCompleteCodeMapper;
import com.viettel.campaign.model.CampaignCompleteCode;
import com.viettel.campaign.repository.CampaignCompleteCodeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CampaignCompleteCodeServiceImpl implements CampaignCompleteCodeService {

    @Autowired
    CampaignCompleteCodeRepository completeCodeRepository;

    @Override
    public Map listAllCompleteCode(int page, int pageSize, String sort) {
        Map result = new HashMap();
        Page<CampaignCompleteCode> pg = completeCodeRepository.findAll(PageRequest.of(page, pageSize, Sort.by(sort)));
        result.put("totalItems", pg.getTotalElements());
        result.put("data", pg.iterator());
        return result;
    }

    @Override
    public Map listCompleteCodeByName(int page, int pageSize, String sort, String name) {
        Map result = new HashMap();
        List<CampaignCompleteCode> lst = new ArrayList<>();

        lst = completeCodeRepository.findByCompleteNameContains(name, PageRequest.of(page, pageSize, Sort.by(sort)));

        result.put("totalItem", lst.size());
        result.put("customers", lst);

        return result;
    }

    @Override
    public ResultDTO createCompleteCode(CampaignCompleteCodeDTO completeCodeDTO) {
        ResultDTO resultDTO = new ResultDTO();
        CampaignCompleteCodeMapper compCodeMapper = new CampaignCompleteCodeMapper();
        Date today = new Date();
        CampaignCompleteCode compCode = new CampaignCompleteCode();

        try {
            if (completeCodeDTO != null) {
                // insert
                compCode = compCodeMapper.toPersistenceBean(completeCodeDTO);
                compCode = completeCodeRepository.save(compCode);

                resultDTO.setErrorCode("0");
                resultDTO.setDescription("Complete Code: " + compCode.getCampaignCompleteCodeId() + " created!");
            } else {
                resultDTO.setErrorCode("-2");
                resultDTO.setDescription("CompleteCodeDTO null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Override
    public ResultDTO updateCompleteCode(CampaignCompleteCodeDTO completeCodeDTO) {
        ResultDTO resultDTO = new ResultDTO();
        CampaignCompleteCodeMapper compCodeMapper = new CampaignCompleteCodeMapper();
        Date today = new Date();
        CampaignCompleteCode compCode = new CampaignCompleteCode();

        try {
            if (completeCodeDTO != null) {
                // update
                compCode = compCodeMapper.toPersistenceBean(completeCodeDTO);
                compCode = completeCodeRepository.save(compCode);

                resultDTO.setErrorCode("0");
                resultDTO.setDescription("Complete Code: " + compCode.getCampaignCompleteCodeId() + " updated!");
            } else {
                resultDTO.setErrorCode("-2");
                resultDTO.setDescription("CompleteCodeDTO null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }

    @Override
    public ResultDTO deleteCompleteCode(CampaignCompleteCodeDTO completeCodeDTO) {
        ResultDTO resultDTO = new ResultDTO();
        CampaignCompleteCodeMapper compCodeMapper = new CampaignCompleteCodeMapper();
        Date today = new Date();
        CampaignCompleteCode compCode = new CampaignCompleteCode();

        try {
            if (completeCodeDTO != null) {
                // delete
                completeCodeRepository.deleteById(completeCodeDTO.getCampaignCompleteCodeId());

                resultDTO.setErrorCode("0");
                resultDTO.setDescription("Complete Code: " + compCode.getCampaignCompleteCodeId() + " deleted!");
            } else {
                resultDTO.setErrorCode("-2");
                resultDTO.setDescription("CompleteCodeDTO null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultDTO;
    }
}
