package com.bankaudit.rest.process.service;

import java.sql.Timestamp;
import java.util.List;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.dto.DataTableResponse;
import com.bankaudit.process.model.AuditDocumentDetails;
import com.bankaudit.rest.process.controller.AuditOffsetController;
import com.bankaudit.rest.process.dao.AuditDocumentDetailsDao;
import com.bankaudit.rest.process.dao.AuditOffsetDao;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.util.BankAuditUtil;

@Service
@Transactional("transactionManager")
public class AuditOffsetServiceImpl implements AuditOffsetService{
	@Autowired
	AuditDocumentDetailsDao auditDocumentDetailsDao;
	
	@Autowired
	AuditOffsetDao auditOffsetDao;
	
	@Autowired
	BusinessDateService businessDateService;
	
	static final Logger logger = Logger.getLogger(AuditOffsetController.class);
	
	@Override
	public DataTableResponse getAuditsforOffset(int legalEntityCode, String userId, String search, Integer orderColumn,
			String orderDirection, Integer page, Integer size) {
		DataTableResponse ds=auditOffsetDao.getAuditsforOffset(legalEntityCode,userId,search,orderColumn,
				orderDirection,page,size);
		return ds;
	}	
	
	/*@SuppressWarnings("unchecked")
	@Override
	public List<AuditDocumentDetails> getAuditDocumentDetails(Integer legalEntityCode, String auditTypeCode, String planId,
			String auditId, String userId) {
		
		return auditDocumentDetailsDao.getAuditDocumentDetails( legalEntityCode,  auditTypeCode,  planId,
				 auditId,  userId);
	}*/
	
	/*@Override
	public List<AuditDocumentDetails> uploadDocument(Integer legalEntityCode, String auditTypeCode, String planId,
			String auditId, String userId, CommonsMultipartFile[] files, String documentName) throws Exception {
		List<AuditDocumentDetails> auditDocumentDetailsLst = null;
		AuditDocumentDetails auditDocumentDetails =null;
		logger.info("AuditOffsetServiceImpl uploadDocument():: LE .. "+legalEntityCode+" planId:: "+planId +" auditId:: "+auditId );
		String referencePath="";
		try {
			for (CommonsMultipartFile commonsMultipartFile : files) {
	    	  auditDocumentDetails=new AuditDocumentDetails();
				auditDocumentDetails.setLegalEntityCode(legalEntityCode);
				auditDocumentDetails.setAuditTypeCode(auditTypeCode);
				auditDocumentDetails.setPlanId(planId);
				auditDocumentDetails.setAuditId(auditId);
				auditDocumentDetails.setObservationId("AUDIT");
				auditDocumentDetails.setDocumentType("AUDIT_OFFSET");
				auditDocumentDetails.setDocumentName(documentName);
				auditDocumentDetails.setEntityStatus("A");
				auditDocumentDetails.setStatus("A");
				auditDocumentDetails.setMaker(userId);
				//auditDocumentDetails.setChecker(auditObservationDto.getAuditObservations().get(0).getChecker());
				auditDocumentDetails.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(legalEntityCode));
				auditDocumentDetails.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
				auditDocumentDetails.setMakerTimestamp(auditDocumentDetails.getBusinessdateTimestamp());
				auditDocumentDetails.setReferencePath("");
							
				auditDocumentDetailsDao.save(auditDocumentDetails);
	    	    referencePath=BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD,auditDocumentDetails.getId().toString());
	    		logger.info("referencePath :: "+referencePath);
	    		auditDocumentDetails.setReferencePath(referencePath);
	    		auditDocumentDetails.setFileName(commonsMultipartFile.getOriginalFilename());
	    		auditDocumentDetailsDao.update(auditDocumentDetails);
	    	}	
			logger.info("documents uploaded succesfully :: ");
			auditDocumentDetailsLst=  getAuditDocumentDetails(legalEntityCode, auditTypeCode, planId, auditId, userId);
			logger.info("auditDocumentDetailsLst :: " + auditDocumentDetailsLst);
		}catch(Exception e) {
			e.printStackTrace();
		}
		return auditDocumentDetailsLst;
   		
	}*/
	
	@Override
	public AuditDocumentDetails getAuditDocumentDetailsById(Integer id) {
		return(AuditDocumentDetails)auditDocumentDetailsDao.get(AuditDocumentDetails.class, id);
	}
	
	/*@Override
	public String deleteAuditOffsetDocumentDetails(Integer legalEntityCode,Integer id, String auditTypeCode, String planId, String auditId ){
		return 	auditDocumentDetailsDao.deleteAuditOffsetDocumentDetails(legalEntityCode, id, auditTypeCode, planId, auditId);
	}*/
	 
}
