package com.bankaudit.report.v1.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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.report.v1.dao.AuditBookReportDao;
import com.bankaudit.report.v1.dto.AuditBookReportDataModel;
import com.bankaudit.report.v1.dto.ChapterDto;
import com.bankaudit.report.v1.dto.CheckListDto;
import com.bankaudit.report.v1.dto.ProcessDto;
import com.bankaudit.report.v1.dto.ProductDto;
import com.bankaudit.report.v1.dto.SectionDto;

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("auditBookReportService")
@Transactional("transactionManager")
public class AuditBookReportServiceImpl implements AuditBookReportService {

	@Autowired
	AuditBookReportDao auditBookReportDao ;
	
	
	@Override
	public List<AuditBookReportDataModel> generateAuditBookReport(Integer legalEntityCode, String auditId, String reportLanguage){
		
		int chapterSeq  = 1;
		int sectionSeq  = 1;
		int productSeq  = 1;
		int processSeq  = 1;
		int checkListSeq  = 1;
		
		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+".");  			
			chapterDtoUpdated.setDerivedStatements(auditBookReportDao.getDerivedStatementForChapter(legalEntityCode, groupSequence));
			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+".");				 
				sectionDtoUpdate.setDerivedStatements( auditBookReportDao.getDerivedStatementForSection(legalEntityCode, groupSequence, subGroupSequence));
				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+".");  
				 
					productDtoUpdate.setDerivedStatements(auditBookReportDao.getDerivedStatementForProduct(legalEntityCode, groupSequence, subGroupSequence, activitySequence));
					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(null);						 
						 processDto.setCheckList(auditBookReportDao.getChecklist(legalEntityCode, auditGroupCode, auditSubGroupCode, activityCode, processCode));						 
						 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++;
		}
		
		 AuditBookReportDataModel auditBookReportDataModel = new  AuditBookReportDataModel();
		 auditBookReportDataModel.setChapterList(listChapterDtoUpdated);
		
		List<AuditBookReportDataModel> listAuditBookReportDataModel= new ArrayList<AuditBookReportDataModel>();
		listAuditBookReportDataModel.add(auditBookReportDataModel);
		
		return listAuditBookReportDataModel;
	}
	
	
	public byte[] 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_V1/" + jasperReportName);
		byte[] bytes = null;
		JasperReport jasperReport = null;

		try (ByteArrayOutputStream byteArray = new ByteArrayOutputStream()) {

			jasperReport = JasperCompileManager.compileReport(stream);
			params.put("SUB_REPORT_DIR", "reports/report_V1/");
			JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(jasperReportBeanDataSourceList);
			JasperPrint print = JasperFillManager.fillReport(jasperReport, params, dataSource);
			//bytes = JasperExportManager.exportReportToPdf(print);

			JasperExportManager.exportReportToPdfFile(print,"D://gg.pdf");
		} catch (JRException | IOException e) {
			e.printStackTrace();
		}

		return bytes;
		
	}
	
	   

}
