package com.viettel.campaign.job;

import com.viettel.campaign.model.ccms_full.Campaign;
import com.viettel.campaign.model.ccms_full.ProcessConfig;
import com.viettel.campaign.service.CampaignService;
import com.viettel.campaign.service.ProcessConfigService;
import com.viettel.campaign.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author hanv_itsol
 * @project campaign
 */

@Slf4j
public class CampaignJob {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
    private static final String CUSTOMER_INACTIVE_DUARATION =  "CUSTOMER_INACTIVE_DUARATION";
    private static final String CRON_EXPRESSION_CHECK_START =  "CRON_EXPRESSION_CHECK_START";
    private static final String CRON_EXPRESSION_CHECK_END =  "CRON_EXPRESSION_CHECK_END";

    @Autowired
    private ProcessConfigService processConfigService;

    @Autowired
    private CampaignService campaignService;

//    @Scheduled(fixedRate = 5000)
    @Transactional( propagation = Propagation.REQUIRED)
    public void process() {
        log.info(Thread.currentThread().getName() + " The Task executed at " + dateFormat.format(new Date()));
        List<ProcessConfig> list = processConfigService.findAll();

        list.parallelStream().forEach(p -> {
            boolean isExecute = DateTimeUtil.isRun(p.getConfigValue(), p.getLastProcess());
            switch (p.getConfigCode()){
                case CUSTOMER_INACTIVE_DUARATION:
                    if(isExecute){

                    }
                    break;
                case CRON_EXPRESSION_CHECK_START:
                    // process
                    if(isExecute){
                        // chuyen trang thai Du thao sang Trien khai
                        log.info("thay doi trang thai");
                        List<Long> status = new ArrayList<>();
                        status.add(1L);
                        status.add(1L);
                        List<Campaign> campaigns = campaignService.findCampaignByCompanySiteIdAndStartTimeIsLessThanEqualAndStatusIn(p.getSiteId(), new Date(), status);
                        campaigns.parallelStream().forEach(campaign -> {
                            campaign.setProcessStatus(1);
                            campaign.setStatus(2L);
                            campaign.setCampaignStart(new Date());
                            campaignService.updateProcess(campaign);
                        });

                        // update last check
                        p.setLastProcess(new Date());
                        processConfigService.update(p);
                    }
                    break;
                case CRON_EXPRESSION_CHECK_END:
                    // process
                    if(isExecute){
                        // chuyen trang thai sang ket thuc
                        List<Long> status = new ArrayList<>();
                        status.add(2L);
                        status.add(3L);
                        List<Campaign> campaigns = campaignService.findCampaignByCompanySiteIdAndEndTimeIsLessThanEqualAndStatusIn(p.getSiteId(), new Date(), status);
                        campaigns.parallelStream().forEach(campaign -> {
                            campaign.setStatus(4L);
                            campaign.setCampaignEnd(new Date());
                            campaignService.updateProcess(campaign);
                        });
                    }
                    break;
                default:

            }
        });


    }
//

}
