package com.mx.dla.dda.contrato.transaccion.terminos.actions;

import java.io.IOException;
import java.sql.SQLException;

import org.apache.ibatis.executor.result.ResultMapException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;

import com.mx.dla.dda.contrato.common.actions.BaseContratoAction;
import com.mx.dla.dda.contrato.transaccion.exceptions.dtos.TransaccionException;
import com.mx.dla.dda.contrato.transaccion.terminos.bos.TerminosMovie;
import com.mx.dla.dda.contrato.transaccion.terminos.bos.TerminosTransaccionBO;
import com.mx.dla.dda.contrato.transaccion.terminos.dtos.MoviesDTO;
import com.opensymphony.xwork2.Action;

public class TerminosMoviesAction extends BaseContratoAction {

	private static final long serialVersionUID = 1L;

	@Autowired
	private TerminosMovie service;

	@Autowired
	private TerminosTransaccionBO comun;

	private MoviesDTO movies;
	private String mensaje;
	private String error;
	private Long idContrato;

	public String obtener() {
		try {
			movies = service.obtenerMovies((Long) this.getSession().get("idContrato"));

		} catch (TransaccionException e) {
			error = e.getMessage();
		} catch (ResultMapException e) {
			error = e.getMessage();
		} catch (SQLException e) {
			error = e.getMessage();
		}
		return Action.SUCCESS;
	}

	public String guardar() {
		try {
			logger.debug("par {}", movies.toString());
			comun.guardarDatosComun(movies.getComun(), this.idContrato);
			service.guardarPremium(movies, this.idContrato);
			comun.calculaPagoMinGarantizado(this.idContrato);
		} catch (TransaccionException e) {
			error = e.getMessage();
		} catch (JsonParseException e) {
			error = "Error al guardar los Terminos Premium";
			logger.error("Error {}", e);
		} catch (JsonMappingException e) {
			error = "Error al guardar los Terminos Premium";
			logger.error("Error {}", e);
		} catch (IOException e) {
			error = "Error al guardar los Terminos Premium";
			logger.error("Error {}", e);
		}
		return Action.SUCCESS;
	}

	public String getMensaje() {
		return mensaje;
	}

	public String getError() {
		return error;
	}

	public MoviesDTO getMovies() {
		return movies;
	}

	public void setMovies(MoviesDTO movies) {
		this.movies = movies;
	}

	public void setIdContrato(Long idContrato) {
		this.idContrato = idContrato;
	}

}