package com.viettel.campaign.service.impl;

import com.viettel.campaign.model.Campaign;
import com.viettel.campaign.model.TimeZoneDialMode;
import com.viettel.campaign.repository.CampaignRepository;
import com.viettel.campaign.repository.TimeZoneDialModeRepository;
import com.viettel.campaign.repository.impl.CampaignRepositoryImpl;
import com.viettel.campaign.service.CampaignService;
import com.viettel.campaign.utils.Constants;
import com.viettel.campaign.web.dto.CampaignDTO;
import com.viettel.campaign.web.dto.ResultDTO;
import com.viettel.campaign.web.dto.request_dto.CampaignRequestDTO;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class CampaignServiceImpl implements CampaignService {
    private static final Logger logger = LogManager.getLogger(CampaignRepositoryImpl.class);

    @Autowired
    CampaignRepository campaignRepository;

    @Autowired
    TimeZoneDialModeRepository timeZoneDialModeRepository;

    @Autowired
    ModelMapper modelMapper;

    @Override
    public ResultDTO searchCampaignExecute(CampaignRequestDTO requestDto) {
        return campaignRepository.searchCampaignExecute(requestDto);
    }

    @Override
    public ResultDTO search(CampaignRequestDTO requestDto) {
        return campaignRepository.search(requestDto);
    }

    @Override
    public ResultDTO findByCampaignCode(CampaignRequestDTO requestDTO) {
        return campaignRepository.findByCampaignCode(requestDTO);
    }

    @Override
    public ResultDTO addNewCampaign(CampaignDTO campaignDTO) {
        ResultDTO resultDTO = new ResultDTO();
        Campaign campaign = modelMapper.map(campaignDTO, Campaign.class);
        TimeZoneDialMode timeZoneDialMode = new TimeZoneDialMode();
        try {
            campaign = campaignRepository.save(campaign);
            //insert sub tables
            if(campaignDTO.getCurrentTimeMode().equals(1)){
                timeZoneDialMode.setCompanySiteId(campaignDTO.getCompanySiteId());
                timeZoneDialMode.setCampaignId(campaign.getCampaignId());
//                timeZoneDialMode.setHour(campaignDTO.getT);

                timeZoneDialModeRepository.save(timeZoneDialMode);
            }
            if(campaignDTO.getCurrentTimeMode().equals(2)) {

            }
            resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
            resultDTO.setErrorCode(Constants.ApiErrorDesc.SUCCESS);
            resultDTO.setData(campaign);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
            resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
            resultDTO.setErrorCode(Constants.ApiErrorDesc.ERROR);
        }
        return resultDTO;
    }

    public Map countRecallCustomer(Long companySiteId, Long agentId) {
        Map result = new HashMap();
        Long count = campaignRepository.countRecallCustomer(companySiteId, agentId);
        ResultDTO resultDTO = new ResultDTO();
        if (count != null) {
            resultDTO.setErrorCode(Constants.ApiErrorCode.SUCCESS);
            resultDTO.setDescription(Constants.ApiErrorDesc.SUCCESS);
            result.put("info", resultDTO);
            result.put("result", count);
        } else {
            resultDTO.setErrorCode(Constants.ApiErrorCode.ERROR);
            resultDTO.setDescription(Constants.ApiErrorDesc.ERROR);
            result.put("info", resultDTO);
            result.put("result", 0);
        }

        return result;
    }
}
