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.ProductServiceException;
import com.prime.sony.ecommerce.service.ProductService;
import com.prime.sony.ecommerce.util.MailService;

@RestController
@RequestMapping(value = "/product", produces = { MediaType.APPLICATION_JSON_VALUE })
public class ProductController {

	private static final Logger log = LoggerFactory.getLogger(ProductController.class);

	@Autowired
	private ProductService productService;

	@Autowired
	private MailService mailService;

	@GetMapping("/save")
	public Map<String, String> saveProductData() throws ProductServiceException {

		log.info("==>ProductController.saveProductData()");
		Map<String, String> resultMap = null;
		try {

			resultMap = productService.saveProductData();
		} catch (Exception e) {
			log.error("==>Exception at ProductController.saveProductData()", e);
			throw new ProductServiceException(e.getMessage(), e.getCause(), null);
		}

		log.info("<==ProductController.saveProductData()");
		return resultMap;

	}

	@GetMapping("/load/{storeId}/{fromDate}/{toDate}")
	public Map<String, String> loadProductData(@PathVariable("storeId") String storeId,
			@PathVariable("fromDate") String fromDate, @PathVariable("toDate") String toDate)
			throws ProductServiceException {

		log.info("==>ProductController.loadProductData()");
		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 = productService.loadProductData(storeId, 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 ProductController.loadProductData()", e);
			throw new ProductServiceException(e.getMessage(), e.getCause(), null);
		}

		log.info("==>ProductController.loadProductData()");
		return resultMap;

	}

}
