package com.bankaudit.report.v2.service;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bankaudit.constants.BankAuditConstant;
import com.bankaudit.report.v2.dao.AuditBookReportDaoV2;
import com.bankaudit.report.v2.dto.AuditBookReportDataModel;
import com.bankaudit.report.v2.dto.ChapterDto;
import com.bankaudit.report.v2.dto.CheckListDto;
import com.bankaudit.report.v2.dto.PartDto;
import com.bankaudit.report.v2.dto.ProcessDto;
import com.bankaudit.report.v2.dto.ProductDto;
import com.bankaudit.report.v2.dto.SectionDto;
import com.bankaudit.spring.config.BankAuditConfig;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

@Service("auditBookReportServiceV2")
@Transactional("transactionManager")
public class AuditBookReportServiceImplV2 implements AuditBookReportServiceV2 {

	@Autowired
	AuditBookReportDaoV2 auditBookReportDao ;
	
	
	@Override
		public List<AuditBookReportDataModel> generateAuditBookReport(Integer legalEntityCode, String auditId, String reportLanguage){
			
			String[] parts ={"Part A","Part B", "Part C"};
			String[] isComplied ={"N","C"};
			
			int chapterSeq  = 1;
			int sectionSeq  = 1;
			int productSeq  = 1;
			int processSeq  = 1;
			int checkListSeq  = 1;
			int isCompliedIndex = 0;
			
			List<PartDto>  listPart =  new ArrayList<PartDto>();
			
			for (String partName : parts) {
				
				PartDto partDto =new PartDto();
				partDto.setPartName(partName);				
				 
				if(!partName.equalsIgnoreCase("Part C"))
				{
					List<ChapterDto>  listChapterDto = auditBookReportDao.getChapter(legalEntityCode, auditId);		
					List<ChapterDto>  listChapterDtoUpdated = new ArrayList<ChapterDto>();
					
					for (ChapterDto chapterDto : listChapterDto) {
						 
						 sectionSeq  = 1;
						 productSeq  = 1;
						 processSeq  = 1;
						 checkListSeq  = 1;
						String auditGroupCode = chapterDto.getAuditGroupCode();
						//String groupSequence = chapterDto.getSequenceNumber();
						ChapterDto chapterDtoUpdated = chapterDto;
						chapterDtoUpdated.setSequenceNumber(chapterSeq+".");  
						
						if(partName.equalsIgnoreCase("Part B")){
							chapterDtoUpdated.setDerivedStatements(auditBookReportDao.getDerivedStatementForChapter(legalEntityCode, auditGroupCode));
							//chapterDtoUpdated.setStatements(auditBookReportDao.getStaticContentForChapter(legalEntityCode, auditGroupCode, reportLanguage));
						}
						
						List<SectionDto> listSectionDto = auditBookReportDao.getSection(legalEntityCode, auditGroupCode);
						List<SectionDto> listSectionDtoUpdated = new ArrayList<SectionDto>(); 
						for (SectionDto sectionDto : listSectionDto) {
							
							productSeq  = 1;
							processSeq  = 1;
							checkListSeq  = 1;
							//String subGroupSequence = sectionDto.getSequenceNumber();
							String auditSubGroupCode = sectionDto.getAuditSubGroupCode();
							SectionDto sectionDtoUpdate = sectionDto;
							sectionDtoUpdate.setSequenceNumber(chapterSeq+"."+sectionSeq+".");				 
							
							if(partName.equalsIgnoreCase("Part B")){
								sectionDtoUpdate.setDerivedStatements(auditBookReportDao.getDerivedStatementForSection(legalEntityCode, auditGroupCode, auditSubGroupCode));
								//sectionDtoUpdate.setStatements(auditBookReportDao.getStaticContentForSection(legalEntityCode, auditGroupCode, auditSubGroupCode, reportLanguage));
							}
							
							List<ProductDto> listProductDto = auditBookReportDao.getProduct(legalEntityCode, auditGroupCode, auditSubGroupCode);
							List<ProductDto> listProductDtoUpdated = new ArrayList<ProductDto>();
							for (ProductDto productDto : listProductDto) {
								
								processSeq  = 1;
								checkListSeq  = 1;
								//String activitySequence = productDto.getSequenceNumber();
								String activityCode = productDto.getActivityCode();
								ProductDto productDtoUpdate = productDto;
								productDtoUpdate.setSequenceNumber(chapterSeq+"."+sectionSeq+"."+productSeq+".");  
							 
								if(partName.equalsIgnoreCase("Part B")){
									productDtoUpdate.setDerivedStatements(auditBookReportDao.getDerivedStatementForProduct(legalEntityCode, auditGroupCode, auditSubGroupCode, activityCode));
									//productDtoUpdate.setStatements(auditBookReportDao.getStaticContentForProduct(legalEntityCode, auditGroupCode, auditSubGroupCode, activityCode, reportLanguage));				
								}
								
								 List<ProcessDto> listProcessDto = auditBookReportDao.getProcess(legalEntityCode, auditGroupCode, auditSubGroupCode, activityCode);
								 List<ProcessDto> listProcessDtoUpdated = new ArrayList<ProcessDto>();
								 for (ProcessDto processDto : listProcessDto) {	
									 
									 checkListSeq  = 1;
									 String processCode = processDto.getProcessCode();
									 ProcessDto processDtoUpdate = processDto;
									 processDtoUpdate.setSequenceNumber(chapterSeq+"."+sectionSeq+"."+productSeq+"."+processSeq+"."); 
									 
									 processDto.setStatements(auditBookReportDao.getDerivedStatementForProcess(legalEntityCode, auditGroupCode, auditSubGroupCode, activityCode, processCode));						 
									//processDto.setCheckList(auditBookReportDao.getChecklist(legalEntityCode, auditGroupCode, auditSubGroupCode, activityCode, processCode, isComplied[isCompliedIndex]));						 
										 
									 List<CheckListDto>  listCheckListDto = auditBookReportDao.getChecklist(legalEntityCode, auditGroupCode, auditSubGroupCode, activityCode, processCode, isComplied[isCompliedIndex]);
									 List<CheckListDto>  listCheckListDtoUpdated = new ArrayList<CheckListDto>();
									 for (CheckListDto checkListDto : listCheckListDto) {
										 String findingCode = checkListDto.getFindingCode();
										 CheckListDto checkListDtoUpdate = checkListDto;
										 checkListDtoUpdate.setStatements(auditBookReportDao.getDerivedStatementForchecklist(legalEntityCode, auditGroupCode, auditSubGroupCode, activityCode, processCode, findingCode));
										 listCheckListDtoUpdated.add(checkListDtoUpdate);
									 }								 
									 processDtoUpdate.setCheckList(listCheckListDtoUpdated);							 
									 listProcessDtoUpdated.add(processDtoUpdate);
									 processSeq++;
								}				
								productDtoUpdate.setProcessList(listProcessDtoUpdated); 
								listProductDtoUpdated.add(productDtoUpdate);
								productSeq++;
							}
							sectionDtoUpdate.setProductList(listProductDtoUpdated);
							listSectionDtoUpdated.add(sectionDtoUpdate);
							sectionSeq++;
						}
						chapterDtoUpdated.setSectionList(listSectionDtoUpdated);
						listChapterDtoUpdated.add(chapterDtoUpdated);				
						chapterSeq++;
					}
					partDto.setChapterList(listChapterDtoUpdated);
				}
				listPart.add(partDto);
				isCompliedIndex++;
				chapterSeq  = 1;
			}
			
		 AuditBookReportDataModel auditBookReportDataModel = new  AuditBookReportDataModel();
		 auditBookReportDataModel.setPartList(listPart);
		
		List<AuditBookReportDataModel> listAuditBookReportDataModel= new ArrayList<AuditBookReportDataModel>();
		listAuditBookReportDataModel.add(auditBookReportDataModel);
		
		return listAuditBookReportDataModel;
	}
	
	
	public String generatePDFReport(Map<String, Object> params, Integer legalEntityCode, String auditId){
		
		String jasperReportName ="Wave_Book.jrxml";
		List<AuditBookReportDataModel> jasperReportBeanDataSourceList =generateAuditBookReport(legalEntityCode, auditId, "hi");
		 
		InputStream stream = this.getClass().getResourceAsStream("/reports/report_V2/" + jasperReportName);
		 
		JasperReport jasperReport = null;

		try (ByteArrayOutputStream byteArray = new ByteArrayOutputStream()) {

			jasperReport = JasperCompileManager.compileReport(stream);
			params.put("SUB_REPORT_DIR", "reports/report_V2/");
			JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(jasperReportBeanDataSourceList);
			JasperPrint print = JasperFillManager.fillReport(jasperReport, params, dataSource);
			
			//bytes = JasperExportManager.exportReportToPdf(print);
			//JasperExportManager.exportReportToPdfFile(print,"D://gg1.pdf");			
			
			 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			 String uploadFileName = "InspectionReport"+".pdf";    		 
			 String uploadPath = BankAuditConfig.getInstance().getConfigValue(BankAuditConstant.DIR_DOC_UPLOAD)+ "/" +uploadFileName;
			 System.out.println("uploadPath .. "+ uploadPath);
			 
			 JasperExportManager.exportReportToPdfFile(print,uploadPath);
			
			// saveUploadDetails(legalEntityCode, auditId, uploadFileName, uploadPath);
			 
			 return uploadPath;
			 
		} catch (JRException | IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public void saveUploadDetails(Integer legalEntityCode, String inspectionId, String fileName, String uploadPath){
		
		
		//auditBookReportDao.save(entity);
	}
	
	   

}
