package com.gamaset.sonicbot.collector.schedule;

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

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.gamaset.sonicbot.collector.business.probabilitymatch.ManagerProcessProbabilityMatchSchedule;
import com.gamaset.sonicbot.collector.business.statistic.ManagerProcessMatchStatistic;
import com.gamaset.sonicbot.collector.dto.MatchDataDTO;
import com.gamaset.sonicbot.collector.dto.MatchResumeDTO;
import com.gamaset.sonicbot.collector.dto.MatchSeriesDTO;
import com.gamaset.sonicbot.collector.dto.MatchStatusEnum;
import com.gamaset.sonicbot.collector.dto.statistic.MatchStatisticDTO;
import com.gamaset.sonicbot.collector.infra.utils.DateUtils;
import com.gamaset.sonicbot.collector.repository.CouponMatchRepository;
import com.gamaset.sonicbot.collector.repository.entity.CouponMatch;
import com.gamaset.sonicbot.collector.service.match.MatchService;

@Component
public class ReadMatchesSchedule {

	private final static Logger LOG = LogManager.getLogger(ReadMatchesSchedule.class);
	
	// ler as 03:00:00 
	private static final String CRON_CONFIG_STATS_MATCHES = "00 43 10 * * ?";
	// ler as 02:30:00 
	private static final String CRON_CONFIG_UPDATE_MATCHES = "00 47 10 * * ?";

	private static final String ZONE_CONFIG = "America/Sao_Paulo";
	
	@Autowired
	private MatchService matchService;
	@Autowired
	private ManagerProcessMatchStatistic matchStatistic;
	@Autowired
	private ManagerProcessProbabilityMatchSchedule probabilityMatch;
	@Autowired
	private CouponMatchRepository couponMatchRepository;
	
	@Scheduled(cron = CRON_CONFIG_STATS_MATCHES, zone=ZONE_CONFIG)
	public void executeReadMatchesAndPersist() {
		LOG.info("=== initializing read and save matches today ===" + dateFormat.format(new Date()));
		
		MatchSeriesDTO matchSeries = matchService.listByDate(null);
//		List<MatchDataDTO> datas = new ArrayList<>();
//		for (MatchResumeDTO matchResume : matchSeries.getMatches()) {
//			matchResume.setDate(matchSeries.getDate());
//			MatchStatisticDTO matchStatisticDTO = matchStatistic.generateStatistics(matchResume);
//			datas.add(new MatchDataDTO(matchResume, matchStatisticDTO));
//		}
//		
//		probabilityMatch.save(datas);
		
		LOG.info("=== finished read and save matches today ===" + dateFormat.format(new Date()));
	}
	
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Scheduled(cron = CRON_CONFIG_UPDATE_MATCHES, zone=ZONE_CONFIG)
	@Transactional
	public void executeReadMatchesAndUpdateResults() {
		
		String date = DateUtils.getTodayMinus1DayDateString();
		LOG.info(String.format("=== initializing read and update matches results [%s] in [%s] ===", date, dateFormat.format(new Date())));
		
		MatchSeriesDTO matchSeries = matchService.listByDate(date);
		List<CouponMatch> matches = couponMatchRepository.findByCouponId(matchSeries.getId());
		for (int i = 0; i < matches.size(); i++) {
			CouponMatch couponMatch = matches.get(i);
			for (MatchResumeDTO matchResume : matchSeries.getMatches()	) {
				if(matchResume.getMatchStatus().equals(MatchStatusEnum.TERMINADO)){
					if(matchResume.getMatchId().equals(couponMatch.getId())){
							
						couponMatch.setMatchStatus(matchResume.getMatchStatus());
						couponMatch.setScoreHomeTeam(matchResume.getHomeTeamMatch().getScore());
						couponMatch.setScoreAwayTeam(matchResume.getAwayTeamMatch().getScore());
						if(matchResume.getHomeTeamMatch().getScore() > matchResume.getAwayTeamMatch().getScore()){
							couponMatch.setWinnerTeam(couponMatch.getHomeTeam());
						}else if(matchResume.getHomeTeamMatch().getScore() < matchResume.getAwayTeamMatch().getScore()){
							couponMatch.setWinnerTeam(couponMatch.getAwayTeam());
						}//EMPATE vai NULL
						
						couponMatch.setUpdatedDate(new Date());
						couponMatchRepository.update(couponMatch);

					}
				}
			}
		}
		
		LOG.info(String.format("=== finished read and update matches [%s] in [%s] ===", date, dateFormat.format(new Date())));
	}
}


@@ -1440,13 +1440,13 @@
 %2200 
-43 10
+07 11
  * *
@@ -1537,13 +1537,13 @@
 %2200 
-47 10
+15 11
  * *
@@ -2142,17 +2142,23 @@
 ate(
-null
+%222017-04-15%22
 );%0A
-//
 %09%09Li
@@ -2201,18 +2201,16 @@
 st%3C%3E();%0A
-//
 %09%09for (M
@@ -2265,18 +2265,16 @@
 es()) %7B%0A
-//
 %09%09%09match
@@ -2312,18 +2312,16 @@
 ate());%0A
-//
 %09%09%09Match
@@ -2401,18 +2401,16 @@
 esume);%0A
-//
 %09%09%09datas
@@ -2469,21 +2469,15 @@
 ));%0A
-//
 %09%09%7D%0A
-//
 %09%09%0A
-//
 %09%09pr
@@ -3086,20 +3086,28 @@
 tByDate(
-date
+%222017-04-15%22
 );%0A%09%09Lis

teste 02 - carga jogos sabado