package com.rjconsultores.ventaboletos.rest.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.service.PricingAsientoService;
import com.rjconsultores.ventaboletos.service.PricingCategoriaService;
import com.rjconsultores.ventaboletos.service.PricingClaseService;
import com.rjconsultores.ventaboletos.service.PricingCorridaService;
import com.rjconsultores.ventaboletos.service.PricingDiaService;
import com.rjconsultores.ventaboletos.service.PricingMarcaService;
import com.rjconsultores.ventaboletos.service.PricingMercadoService;
import com.rjconsultores.ventaboletos.service.PricingOcupaAntecipaService;
import com.rjconsultores.ventaboletos.service.PricingPuntoVentaService;
import com.rjconsultores.ventaboletos.service.PricingRutaService;
import com.rjconsultores.ventaboletos.service.PricingService;
import com.rjconsultores.ventaboletos.service.PricingTipoPtoVtaService;
import com.rjconsultores.ventaboletos.service.PricingTipoServicioService;
import com.rjconsultores.ventaboletos.service.PricingVigenciaService;
import com.rjconsultores.ventaboletos.utilerias.PricingConverterVOToEntidad;
import com.rjconsultores.ventaboletos.vo.pricing.Pricing;

@Service("alteraPricingService")
public class AlteraPricingService {
	
	private static Logger log = LogManager.getLogger(AlteraPricingService.class);
	
	@Autowired
	PricingService pricingService;
	
	@Autowired
	PricingAsientoService pricingAsientoService;
	
	@Autowired
	PricingTipoPtoVtaService pricingTipoPtoVtaService;
	
	@Autowired
	PricingCategoriaService pricingCategoriaService;
	
	@Autowired
	PricingClaseService pricingClaseService;
	
	@Autowired
	PricingCorridaService pricingCorridaService;
	
	@Autowired
	PricingDiaService pricingDiaService;
	
	@Autowired
	PricingOcupaAntecipaService pricingOcupaAntecipaService;
	
	@Autowired
	PricingPuntoVentaService pricingPuntoVentaService;
	
	@Autowired
	PricingRutaService pricingRutaService;
	
	@Autowired
	PricingTipoServicioService pricingTipoServicioService;
	
	@Autowired
	PricingMercadoService pricingMercadoService;
	
	@Autowired
	PricingVigenciaService pricingVigenciaService;
	
	@Autowired
	PricingMarcaService pricingMarcaService;
	
	public List<Pricing> buscar(Integer pricingId, String nomepricing, Integer empresaId, Date dataViagemInicio, Date dataViagemFim, Integer origemId, Integer destinoId) {
		try {
			List<com.rjconsultores.ventaboletos.entidad.Pricing> lsPricings = pricingService.buscar(pricingId, nomepricing, empresaId,
																								dataViagemInicio, dataViagemFim, origemId, destinoId);
			List<Pricing> lsRetorno = new ArrayList<Pricing>();
			
			for (com.rjconsultores.ventaboletos.entidad.Pricing pricing : lsPricings) {
				Pricing pricingVO = new Pricing(pricing);
				lsRetorno.add(pricingVO);
			}
			return lsRetorno;
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}
	
	@Transactional
	public String incluir(Pricing pricingVO) {
		try {
			com.rjconsultores.ventaboletos.entidad.Pricing pricing = PricingConverterVOToEntidad.getInstance().convertVOToEntidad(pricingVO);
			pricing.setPricingId(null);
			pricing = pricingService.suscribir(pricing);
			
			return "OK";
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		
		return "ERROR";		
	}
	
	@Transactional
	public String alterar(Pricing pricingVO) {
		try {
			com.rjconsultores.ventaboletos.entidad.Pricing pricing = pricingService.obtenerID(pricingVO.getPricingId());
			pricing.setPricingId(pricingVO.getPricingId());
			pricing.setNombPricing(pricingVO.getNomePricing());
			pricing.setCantboleto(pricingVO.getQtdePoltronas() != null ? pricingVO.getQtdePoltronas().shortValue() : null);
			pricing.setDescuentoporcentaje(pricingVO.getPorcentagemIda());
			pricing.setDescuentoporcredondo(pricingVO.getPorcentagemIdaVolta());
			pricing.setIndtransferible(pricingVO.getPodeTransferir() != null ? pricingVO.getPodeTransferir().equals(1) : true);
			pricing.setIndreservable(pricingVO.getPodeReservar() != null ? pricingVO.getPodeReservar().equals(1) : true);
			pricing.setExibeVenda(pricingVO.getExibirVenda() != null ? pricingVO.getExibirVenda().equals(1) : true);
			pricing.setIndcancelable(pricingVO.getPodeCancelar() != null ? pricingVO.getPodeCancelar().equals(1) : true);
			pricing.setActivo(com.rjconsultores.ventaboletos.entidad.Pricing.ATIVO);
			pricing.setFecmodif(new Date());
			pricing.setCantdiasanticipacion(pricingVO.getDiasAtencipacao());
			pricing.setEmpresa(new Empresa(pricingVO.getEmpresaId()));
			pricing.setIndGeneraFeriadoViaje(StringUtils.isNotBlank(pricingVO.getAplicaFeriadoDtViagem()) ? pricingVO.getAplicaFeriadoDtViagem() : "S");
			pricing.setIndGeneraFeriadoVenta(StringUtils.isNotBlank(pricingVO.getAplicaFeriadoDtVenda()) ? pricingVO.getAplicaFeriadoDtVenda() : "S");
			
			if(pricingVO.getPoltronas() != null && !pricingVO.getPoltronas().isEmpty()) {
				pricingAsientoService.borrar(pricing.getPricingAsientoList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadPoltronas(pricing, pricingVO);
			}
	
			if(pricingVO.getCanalvendas() != null && !pricingVO.getCanalvendas().isEmpty()) {
				pricingTipoPtoVtaService.borrar(pricing.getPricingTipoptovtaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadCanalVendas(pricing, pricingVO);
			}
	
			if(pricingVO.getCategorias() != null && !pricingVO.getCategorias().isEmpty()) {
				pricingCategoriaService.borrar(pricing.getPricingCategoriaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadCategorias(pricing, pricingVO);
			}
			
			if(pricingVO.getClasses() != null && !pricingVO.getClasses().isEmpty()) {
				pricingClaseService.borrar(pricing.getPricingClaseList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadClasses(pricing, pricingVO);
			}
			
			if(pricingVO.getServicos() != null && !pricingVO.getServicos().isEmpty()) {
				pricingCorridaService.borrar(pricing.getPricingCorridaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadServicos(pricing, pricingVO);
			}
			
			if(pricingVO.getDias() != null && !pricingVO.getDias().isEmpty()) {
				pricingDiaService.borrar(pricing.getPricingDiaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadDias(pricing, pricingVO);
			}
			
			if(pricingVO.getOcupacoes() != null && !pricingVO.getOcupacoes().isEmpty()) {
				pricingOcupaAntecipaService.borrar(pricing.getPricingOcupaAntecipaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadOcupacoes(pricing, pricingVO);
			}
			
			if(pricingVO.getPuntoventas() != null && !pricingVO.getPuntoventas().isEmpty()) {
				pricingPuntoVentaService.borrar(pricing.getPricingPuntoventaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadPuntoventas(pricing, pricingVO);
			}
			
			if(pricingVO.getLinhas() != null && !pricingVO.getLinhas().isEmpty()) {
				pricingRutaService.borrar(pricing.getPricingRutaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadLinhas(pricing, pricingVO);
			}
			
			if(pricingVO.getTiposervicos() != null && !pricingVO.getTiposervicos().isEmpty()) {
				pricingTipoServicioService.borrar(pricing.getPricingTipoServicioList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadTiposervicos(pricing, pricingVO);
			}
			
			if(pricingVO.getMercados() != null && !pricingVO.getMercados().isEmpty()) {
				pricingMercadoService.borrar(pricing.getPricingMercadoList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadMercados(pricing, pricingVO);
			}
			
			if(pricingVO.getVigencias() != null && !pricingVO.getVigencias().isEmpty()) {
				pricingVigenciaService.borrar(pricing.getPricingVigenciaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadVigencias(pricing, pricingVO);
			}
			
			if(pricingVO.getMarcas() != null && !pricingVO.getMarcas().isEmpty()) {
				pricingMarcaService.borrar(pricing.getPricingMarcaList());
				PricingConverterVOToEntidad.getInstance().convertVOToEntidadMarcas(pricing, pricingVO);
			}
			
			pricingService.actualizacion(pricing);
			
			return "OK";
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		
		return "ERROR";
	}
	
	@Transactional
	public String excluir(Pricing pricingVO) {
		try {
			com.rjconsultores.ventaboletos.entidad.Pricing pricing = pricingService.obtenerID(pricingVO.getPricingId());
			pricingService.borrar(pricing);
			return "OK";
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return "ERROR";
	}
	
	@Transactional
	public String excluir(Integer diasFimViagem) {
		try {
			pricingService.excluirPricing(diasFimViagem);
			return "OK";
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
		
		return "ERROR";
	}

	public String inativar(Pricing pricingVO) {
		try {
			com.rjconsultores.ventaboletos.entidad.Pricing pricing = pricingService.obtenerID(pricingVO.getPricingId());
			pricingService.inativar(pricing);
			return "OK";
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		return "ERROR";
	}

}
