/*
 * Copyright (C) 2016 - 2024 Free Software Foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.mxchange.pizzaapplication.converter.country;

import java.util.List;
import java.util.Objects;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import javax.faces.validator.ValidatorException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.mxchange.jcountry.model.data.Country;
import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;

/**
 * Converter for country instance
 * <p>
 * @author Roland Häder<roland@mxchange.org>
 */
@FacesConverter ("CountryConverter")
public class PizzaCountryConverter implements Converter<Country> {

	/**
	 * Country bean
	 */
	private static CountrySingletonBeanRemote COUNTRY_BEAN;

	@Override
	public Country getAsObject (final FacesContext context, final UIComponent component, final String submittedValue) {
		// Is the instance there?
		if (COUNTRY_BEAN == null) {
			try {
				// Not yet, attempt lookup
				final Context initial = new InitialContext();

				// Lookup EJB
				COUNTRY_BEAN = (CountrySingletonBeanRemote) initial.lookup("java:global/pizzaservice-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote");
			} catch (final NamingException ex) {
				// Throw it again
				throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
			}
		}

		// Is the value null or empty?
		if ((null == submittedValue) || (submittedValue.trim().isEmpty())) {
			// Warning message
			// @TODO Not working with JNDI (no remote interface) this.loggerBeanLocal.logWarning(MessageFormat.format("{0}.getAsObject(): submittedValue is null or empty - EXIT!", this.getClass().getSimpleName())); //NOI18N

			// Return null
			return null;
		}

		// Get full list
		final List<Country> countryList = COUNTRY_BEAN.allCountries();

		// Init value
		Country country = null;

		// Try this better
		try {
			// Convert it to long
			final Long countryId = Long.valueOf(submittedValue);

			// Category id should not be below 1
			assert (countryId > 0) : "countryId is smaller than one: " + countryId; //NOI18N

			// Try to find it
			for (final Country cntry : countryList) {
				// Is the id the same? (null-safe)
				if (Objects.equals(cntry.getCountryId(), countryId)) {
					// Found it
					country = cntry;
					break;
				}
			}
		} catch (final NumberFormatException ex) {
			// Throw again
			throw new ConverterException(ex);
		}

		// Return it
		return country;
	}

	@Override
	public String getAsString (final FacesContext context, final UIComponent component, final Country value) {
		// Is the object null?
		if ((null == value) || (String.valueOf(value).isEmpty())) {
			// Is null
			return ""; //NOI18N
		}

		// Return id number
		return String.valueOf(value.getCountryId());
	}

}
