package com.viettel.campaign.service;

import com.viettel.campaign.utils.Constants;
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.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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<>();
        List<CampaignCompleteCode> count = new ArrayList<>();

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

        result.put("totalItem", count.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;
    }
    @Transactional
    @Override
    public ResultDTO deleteList(List<Long> completeCodeDtos) {
        ResultDTO resultDTO = new ResultDTO();

//        for (int i = 0; i < completeCodeDtos.size(); i++) {
            try {
                completeCodeRepository.deleteds(completeCodeDtos);
                resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
                resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);

            } catch (Exception e) {
                resultDTO.setErrorCode("-1");
                return resultDTO;
            }
//        }
        resultDTO.setErrorCode("0");

        return resultDTO;
    }

    @Override
    public ResultDTO deleteById(Long id) {
        ResultDTO result = new ResultDTO();

        try {
            completeCodeRepository.deleteById(id);
            result.setErrorCode(Constants.Status.ACTIVE_STR);
            result.setDescription(Constants.Status.ACTIVE_STR);

        } catch (Exception e) {
            result.setErrorCode("-1");
            return result;
        }
        result.setErrorCode("0");
        return  result;
    }
}
