package com.prime.sony.ecommerce.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.BatchStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.prime.sony.ecommerce.exceptions.StoreServiceException;
import com.prime.sony.ecommerce.service.StoreService;
import com.prime.sony.ecommerce.util.MailService;

@RestController
@RequestMapping(value = "/store", produces = { MediaType.APPLICATION_JSON_VALUE })
public class StoreController {

	private static final Logger log = LoggerFactory.getLogger(StoreController.class);

	@Autowired
	private StoreService storeService;
	
	@Autowired
	private MailService mailService;

	@GetMapping("/save")
	public Map<String, String> saveStoreData() throws StoreServiceException {

		log.info("==>StoreController.saveStoreData()");
		Map<String, String> resultMap = null;
		try {

			resultMap = storeService.saveStoreData();
		} catch (Exception e) {
			log.error("==>Exception at StoreController.saveStoreData()", e);
			throw new StoreServiceException(e.getMessage(), e.getCause(), null);
		}

		log.info("<==StoreController.saveStoreData()");
		return resultMap;

	}
	
	@GetMapping("/load/{fromDate}/{toDate}")
	public Map<String, String> loadStoreData(@PathVariable("fromDate") String fromDate,@PathVariable("toDate") String toDate) throws StoreServiceException {

		log.info("==>StoreController.loadStoreData()");
		Map<String, String> resultMap = null;
		try {
			Date startDate=new Date(); 
			long startDateTime=new Date().getTime();
			resultMap=new HashMap<String, String>();
			resultMap.put("isLoadService", "true");
			mailService.sentJobExecutionMail(resultMap,BatchStatus.STARTED,startDate+"",null);
			
			resultMap = storeService.loadStoreData(fromDate,toDate);
			
			long endDateTime=new Date().getTime(); 
			long millis = endDateTime - startDateTime;
			String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
    	            TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
    	            TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));

			resultMap.put("isLoadService", "true");
			mailService.sentJobExecutionMail(resultMap,BatchStatus.COMPLETED,startDate+"",hms);
			
		} catch (Exception e) {
			log.error("==>Exception at StoreController.loadStoreData()", e);
			throw new StoreServiceException(e.getMessage(), e.getCause(), null);
		}

		log.info("==>StoreController.loadStoreData()");
		return resultMap;

	}

}
