package com.bankaudit.rest.process.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder.In;

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.AuditAdhocDocument;
import com.bankaudit.process.model.AuditAdhocReport;
import com.bankaudit.process.model.AuditAdhocReportHst;
import com.bankaudit.process.model.AuditDocumentDetails;
import com.bankaudit.process.model.AuditDocumentDetailsTemp;
import com.bankaudit.process.model.AuditObservation;
import com.bankaudit.process.model.AuditSchedule;
import com.bankaudit.rest.process.dao.AuditAdhocReportDao;
import com.bankaudit.service.BusinessDateService;
import com.bankaudit.util.BankAuditUtil;

@Service
@Transactional("transactionManager")
public class AuditAdhocReportServiceImpl  implements AuditAdhocReportService {

	@Autowired
	AuditAdhocReportDao auditAdhocReportDao;
	
	@Autowired
	BusinessDateService businessDateService;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AuditAdhocDocument> getAuditAdhocDocument(Integer legalEntityCode, String auditTypeCode, String planId,
			String auditId, String reportedBy) {
		

		
		return auditAdhocReportDao.getAuditAdhocDocument( legalEntityCode,  auditTypeCode,  planId,
				 auditId,  reportedBy);
	}
	@Override
	public List<AuditAdhocDocument> uploadDocument(Integer legalEntityCode, String auditTypeCode, String planId,
			String auditId, String reportedBy, CommonsMultipartFile[] files) throws Exception {
		AuditAdhocDocument auditAdhocDocument=null;
		String referencePath="";
		//List<AuditAdhocDocument> auditAdhocDocuments=new ArrayList<>();
      for (CommonsMultipartFile commonsMultipartFile : files) {
    	  auditAdhocDocument = new AuditAdhocDocument();
    	    auditAdhocDocument.setLegalEntityCode(legalEntityCode);
    	    auditAdhocDocument.setAuditTypeCode(auditTypeCode);
    	    auditAdhocDocument.setPlanId(planId);
    	    auditAdhocDocument.setAuditId(auditId);
    	    auditAdhocDocument.setReportedBy(reportedBy);
    	    auditAdhocDocument.setStatus("A");
    	    auditAdhocDocument.setEntityStatus("A");
    	    auditAdhocDocument.setMakerTimestamp(new Timestamp(System.currentTimeMillis()));
    	    auditAdhocDocument.setReferencePath("");
    	    auditAdhocReportDao.save(auditAdhocDocument);
    		referencePath=BankAuditUtil.uploadMaterial(commonsMultipartFile, BankAuditConstant.DIR_DOC_UPLOAD_AD_HOC,auditAdhocDocument.getId().toString());
    		auditAdhocDocument.setReferencePath(referencePath);
    		auditAdhocReportDao.update(auditAdhocDocument);
    	//	auditAdhocDocuments.add(auditAdhocDocument);
	}	
   
      return  getAuditAdhocDocument(legalEntityCode, auditTypeCode, planId, auditId, reportedBy);
   //   return auditAdhocDocuments;
		
	}
	@Override
	public void deleteauditAdhocDocument(Integer id,Integer legalEntityCode) {
		auditAdhocReportDao.deleteauditAdhocDocument(id,legalEntityCode);
		
	}
	/*@Override
	public void createAuditAdhocReport(List<AuditAdhocReport> auditAdhocReports) {
		for (AuditAdhocReport auditAdhocReport : auditAdhocReports) {
			auditAdhocReport.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditAdhocReport.getLegalEntityCode()));
			auditAdhocReport.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
			auditAdhocReport.setMakerTimestamp(auditAdhocReport.getBusinessdateTimestamp());
			auditAdhocReportDao.save(auditAdhocReport);
		}
	
	}*/
	@Override
	public void updateAuditAdhocReport(List<AuditAdhocReport> auditAdhocReports) {
		
		for (AuditAdhocReport auditAdhocReport : auditAdhocReports) {
			auditAdhocReport.setBusinessdateTimestamp(businessDateService.getBusinessDateTimestampByLegalEntityCode(auditAdhocReport.getLegalEntityCode()));
			auditAdhocReport.setSysdateTimestamp(new Timestamp(System.currentTimeMillis()));
			if(auditAdhocReport.getId()==null){
				auditAdhocReport.setMakerTimestamp(auditAdhocReport.getBusinessdateTimestamp());
				auditAdhocReport.setReportedDate(auditAdhocReport.getBusinessdateTimestamp());
			}else {
				//put it into history
				AuditAdhocReportHst auditAdhocReportHst=new AuditAdhocReportHst();
				AuditAdhocReport auditAdhocReportDb=(AuditAdhocReport)auditAdhocReportDao.get(AuditAdhocReport.class, auditAdhocReport.getId());
                BeanUtils.copyProperties(auditAdhocReportDb, auditAdhocReportHst);
                auditAdhocReportDao.save(auditAdhocReportHst);
			}
			auditAdhocReportDao.flushSession();
			auditAdhocReportDao.saveOrUpdate(auditAdhocReport);
		}
	}
	@Override
	public List<AuditAdhocReport> getAuditAdhocReports(int legalEntityCode, String auditTypeCode, String planId,
			String auditId, String reportedBy) {
		return auditAdhocReportDao.getAuditAdhocReports( legalEntityCode,  auditTypeCode,  planId,
				 auditId,  reportedBy);
	}
	@Override
	public List<AuditAdhocReport> getAuditAdhocReportsTargetUser(int legalEntityCode, String auditTypeCode, String planId,
			String auditId, String targetUser) {
		return auditAdhocReportDao.getAuditAdhocReportsTargetUser( legalEntityCode,  auditTypeCode,  planId,
				 auditId,  targetUser);
	}
	@Override
	public AuditAdhocDocument getAuditAdhocDocumentById(Integer id) {
		return(AuditAdhocDocument)auditAdhocReportDao.get(AuditAdhocDocument.class, id);
	}
	

}
