package com.bankaudit.rest.process.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.jboss.logging.Logger;
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.dao.MaintEntityDao;
import com.bankaudit.model.MaintEntity;
import com.bankaudit.process.model.IfcEnsureBaasData;
import com.bankaudit.process.model.IfcEnsureBaasIRProcessData;
import com.bankaudit.process.model.IfcRatingData;
import com.bankaudit.rest.process.dao.InterfaceParameterDao;

@Service("interfaceParameterService")
@Transactional("transactionManager")
public class InterfaceParameterServiceImpl implements InterfaceParameterService{

	@Autowired
	InterfaceParameterDao interfaceParameterDao;
	
	@Autowired
	MaintEntityDao maintEntityDao;
	
	static final Logger logger = Logger.getLogger(InterfaceParameterServiceImpl.class);
		 	
	@Override
	public List<IfcEnsureBaasData> getLatestParametersByAuditId(int legalEntityCode, String auditId, String unitId) {
		List<IfcEnsureBaasData> ifcEnsureBaasDataLst = interfaceParameterDao.getLatestParametersByAuditId(legalEntityCode, auditId, unitId);
		return ifcEnsureBaasDataLst; 
	}	
	
	@Override
	public Map<String, String> getProfileDetailsByAuditId(int legalEntityCode, String auditId, String unitId) {
		logger.info("getProfileDetailsByAuditId .. "+ legalEntityCode +" "+auditId+" "+unitId);
		List<IfcEnsureBaasData> ifcEnsureBaasDataLst = interfaceParameterDao.getProfileDetailsByAuditId(legalEntityCode, auditId, unitId);
		Map<String, String> profileMap=new HashMap<String, String>();
		MaintEntity maintEntity =maintEntityDao.getUnitDetails(legalEntityCode, unitId);
		if(maintEntity !=null) {
			profileMap.put("INSTINAME",maintEntity.getUnitName());
			//profileMap.put("BANKTYPE", "");
			profileMap.put("STATE", maintEntity.getState());
		}
		for(IfcEnsureBaasData ifc:ifcEnsureBaasDataLst) {
			profileMap.put("1"+ifc.getParameterNameBaas(), ifc.getSubmittedValueOldest());
			profileMap.put("2"+ifc.getParameterNameBaas(), ifc.getSubmittedValueMiddest());
			profileMap.put("3"+ifc.getParameterNameBaas(), ifc.getSubmittedValueLatest());
			if(ifc.getAuditTypeCode().contains(BankAuditConstant.INSPECTION_TYPE_RRB_CONTAINS)){
				profileMap.put("1"+ifc.getParameterNameNabard(), ifc.getSubmittedValueOldest());
				profileMap.put("2"+ifc.getParameterNameNabard(), ifc.getSubmittedValueMiddest());
				profileMap.put("3"+ifc.getParameterNameNabard(), ifc.getSubmittedValueLatest());
			}
		}
		return profileMap; 
	}
	
	@Override
	public Map<String, String> getDliDpiDetailsByAuditId(int legalEntityCode, String auditId, String unitId) {
		logger.info("getProfileDetailsByAuditId .. "+ legalEntityCode +" "+auditId+" "+unitId);
		List<IfcEnsureBaasIRProcessData> ifcEnsureBaasIRPrcDataLst = interfaceParameterDao.getDliDpiDataByAuditId(legalEntityCode, auditId, unitId);
		Map<String, String> profileMap=new HashMap<String, String>();
		MaintEntity maintEntity =maintEntityDao.getUnitDetails(legalEntityCode, unitId);
		if(maintEntity !=null) {
			profileMap.put("INSTINAME",maintEntity.getUnitName());
			//profileMap.put("BANKTYPE", "");
			profileMap.put("STATE", maintEntity.getState());
		}
		for(IfcEnsureBaasIRProcessData ifc:ifcEnsureBaasIRPrcDataLst) {
			profileMap.put("DPI"+ifc.getParameterNameBaas(), ifc.getSubmittedValueLatest()); // DPI => Present
			profileMap.put("DLI"+ifc.getParameterNameBaas(), ifc.getSubmittedValueOldest()); // DLI => Last
		}
		return profileMap; 
	}
		
	@Override
	public List<IfcRatingData> getInputValuesByAuditId(int legalEntityCode, String auditId) {
		List<IfcRatingData> ifcRatingDataLst = interfaceParameterDao.getInputValuesByAuditId(legalEntityCode, auditId);
		return ifcRatingDataLst; 
	}
		
	private static boolean isRowEmpty(Row row, int columnCount) {
		boolean isEmpty = true;	
    	for( int cellNum=0 ; cellNum<columnCount && isEmpty; cellNum++){
			Cell cell =   row.getCell(cellNum);
			if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK 
					|| (cell.getCellType() == Cell.CELL_TYPE_STRING && cell.getStringCellValue().trim().isEmpty())){ 
				isEmpty = true; 
			}else{isEmpty = false;}
    	} 
		return isEmpty;
	}
	
	private static int getColumnCount(Row row) { 
		int columnCount  = 0; 
    	for( Cell cell: row){ 
			if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK 
					|| (cell.getCellType() == Cell.CELL_TYPE_STRING && cell.getStringCellValue().trim().isEmpty())){  
			}else{
				columnCount++;
			}
    	} 
		return columnCount;
	}
	  
}
