package com.bankaudit.template.rest.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.jboss.logging.Logger;
import org.springframework.beans.BeanUtils;
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.process.model.AuditDocumentDetails;
import com.bankaudit.process.model.AuditDocumentDetailsWrk;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.template.rest.dao.DocUploadDownloadDao;
import com.bankaudit.util.BankAuditUtil;

@Service("docUploadDownloadService")
@Transactional("transactionManager")
public class DocUploadDownloadServiceImpl implements DocUploadDownloadService{
	
	@Autowired
	BusinessDateService businessDateService;
	
	@Autowired
	DocUploadDownloadDao docUploadDownloadDao;
	
	static final Logger logger = Logger.getLogger(DocUploadDownloadServiceImpl.class);
	
	@Override
	public List<AuditDocumentDetails> getAuditDocumentDetails(Integer legalEntityCode, String auditTypeCode, String planId, String auditId, 
			String observationId, String documentType, String documentSubType, String status, String documentName) {		
		return docUploadDownloadDao.getAuditDocumentDetails( legalEntityCode,  auditTypeCode, planId,  auditId,  observationId,  documentType,
				documentSubType, status, documentName);
	} 
	
	@Override
	public Map<String, Integer>  uploadDocument( AuditDocumentDetails auditDocumentDetails, CommonsMultipartFile[] files,String fileName)  {
		
		Map<String , Integer> uplodedIds = new HashMap<String , Integer>();				
		auditDocumentDetails.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditDocumentDetails.getLegalEntityCode()));
		 
		if(auditDocumentDetails.getEntityStatus().equals(BankAuditConstant.STATUS_AUTH) || 
				(auditDocumentDetails.getStatus().equals(BankAuditConstant.STATUS_AUTH) &&
						auditDocumentDetails.getDocumentType().equalsIgnoreCase(BankAuditConstant.DOCUMENT_TYPE_OBSERVATION)) ){
			for (CommonsMultipartFile commonsMultipartFile : files) {
				AuditDocumentDetails auditDocumentDetailsToSave = new AuditDocumentDetails(); 
				Integer uploadId=0;
				try{ 
					BeanUtils.copyProperties(auditDocumentDetails, auditDocumentDetailsToSave ); 
					String pattern = "yyyyMMddHHmmssSSS";
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
					//String referencePath = BankAuditConstant.DELIMITER_URL+BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD, simpleDateFormat.format(new Date())+"_"+fileName);
					//referencePath=referencePath.substring(3);'
					String referencePath =  BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD, simpleDateFormat.format(new Date())+"_"+fileName);
					auditDocumentDetailsToSave.setReferencePath(referencePath); 
					auditDocumentDetailsToSave.setFileName(commonsMultipartFile.getOriginalFilename());				
					docUploadDownloadDao.save(auditDocumentDetailsToSave);
					uploadId = auditDocumentDetailsToSave.getId();
				}catch (Exception e) {
					logger.error("Error .." + e.getMessage()+" :: "+e.getCause());				 
				}
				uplodedIds.put(commonsMultipartFile.getOriginalFilename(), uploadId);				
			}
		}else{			
			for (CommonsMultipartFile commonsMultipartFile : files) {
				AuditDocumentDetailsWrk auditDocumentDetailsToSave = new AuditDocumentDetailsWrk(); 
				Integer uploadId=0;
				try{ 
					BeanUtils.copyProperties(auditDocumentDetails, auditDocumentDetailsToSave ); 
					String pattern = "yyyyMMddHHmmssSSS";
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
					//String referencePath = BankAuditConstant.DELIMITER_URL+BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD, simpleDateFormat.format(new Date())+"_"+fileName);
					//referencePath=referencePath.substring(3);'
					String referencePath =  BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD, simpleDateFormat.format(new Date())+"_"+fileName);
					auditDocumentDetailsToSave.setReferencePath(referencePath); 
					auditDocumentDetailsToSave.setFileName(commonsMultipartFile.getOriginalFilename());				
					docUploadDownloadDao.save(auditDocumentDetailsToSave);
					uploadId = auditDocumentDetailsToSave.getId();
				}catch (Exception e) {
					logger.error("Error .." + e.getMessage()+" :: "+e.getCause());				 
				}
				uplodedIds.put(commonsMultipartFile.getOriginalFilename(), uploadId);				
			}
		}			  
		
		return uplodedIds;
	}
	
	@Override
	public String deleteDocumentById(Integer legalEntityCode, Integer documentId, String status){		
		return 	docUploadDownloadDao.deleteDocumentById(legalEntityCode, documentId, status);		
	}
	
	public String deleteAllDocumentBy(Integer legalEntityCode, String auditTypeCode, String planId, String auditId, String observationId, 
			String documentType, String documentSubType){		 			
			return 	docUploadDownloadDao.deleteAllDocumentBy(legalEntityCode, auditTypeCode, planId, auditId, observationId, documentType,   documentSubType);				 
	}
	
	@Override
	public String getReferencePathById(Integer legalEntityCode, Integer documentId, String status, String auditId){
		return docUploadDownloadDao.getReferencePathById(legalEntityCode, documentId, status, auditId);
	}	
	
	/*@Override
	public List<AuditDocumentDetails> getAuditDocumentDetailCompliance(Integer legalEntityCode, String auditTypeCode, String planId,
			String auditId, String observationId, String documentType, String documentName, String documentSubType) {		
		return docUploadDownloadDao.getAuditDocumentDetailCompliance( legalEntityCode,  auditTypeCode, planId, 
				auditId,  observationId,  documentType, documentName, documentSubType );
	}	*/
	
	@Override
	public AuditDocumentDetails getAuditTypeDocumentDetailById(Integer legalEntityCode, Integer Id, String status) {
		Map<String,Object> properties=new HashMap<String, Object>();
		properties.put("legalEntityCode", legalEntityCode);
		properties.put("id", Id);
		
		if(!BankAuditConstant.STATUS_AUTH.equalsIgnoreCase(status)){
			AuditDocumentDetails auditTypeDocumentDetails=null;
			AuditDocumentDetailsWrk auditTypeDocumentDetailsWrk =(AuditDocumentDetailsWrk) docUploadDownloadDao.getUniqueEntityByMatchingProperties(AuditDocumentDetailsWrk.class, properties);
			if(auditTypeDocumentDetailsWrk!=null){
				auditTypeDocumentDetails=new AuditDocumentDetails();
				BeanUtils.copyProperties(auditTypeDocumentDetailsWrk, auditTypeDocumentDetails);
			}
			return auditTypeDocumentDetails;
		}else {
			return (AuditDocumentDetails) docUploadDownloadDao.getUniqueEntityByMatchingProperties(AuditDocumentDetails.class, properties);
		}		
	}
	
	@Override
	public String getCountOfDocuments(Integer legalEntityCode, String auditTypeCode, String planId, String auditId, 
			String observationId, String documentType, String documentSubType, String status, String documentName){		
		return docUploadDownloadDao.getCountOfDocuments(legalEntityCode, auditTypeCode, planId, auditId, 
				observationId, documentType, documentSubType, status, documentName);
	}
	
	public String submitObservationDocuments(Integer legalEntityCode, String auditTypeCode, String planId, String auditId,
			 String role, String documentType, String documentSubType, String observationId){
		return docUploadDownloadDao.submitObservationDocuments(legalEntityCode, auditTypeCode, planId, auditId, role, documentType,
				documentSubType, observationId);
	}

}
