/*-
 * Copyright 2021 Hochschule für Technik Stuttgart
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.bind;


import javax.xml.bind.JAXBElement;

import org.citygml4j.builder.jaxb.marshal.citygml.ade.ADEMarshallerHelper;
import org.citygml4j.model.citygml.ade.binding.ADEMarshaller;
import org.citygml4j.model.citygml.ade.binding.ADEModelObject;
import org.citygml4j.util.mapper.TypeMapper;

import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.ConsistsOfFWESurvey;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.EnergySurvey;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.FoodSurvey;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.FoodWaterEnergy;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.LandUseSurvey;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.WaterSurvey;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.xsd.ConsistsOfFWESurveyType;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.xsd.EnergySurveyType;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.xsd.FoodSurveyType;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.xsd.FoodWaterEnergyType;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.xsd.LandUseSurveyType;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.xsd.ObjectFactory;
import de.hft.stuttgart.ade.foodwaterenergycitygml4j.landuse.model.xsd.WaterSurveyType;


public class FoodWaterEnergyLandUseADEMarshaller implements ADEMarshaller {
	private final ObjectFactory factory = new ObjectFactory();
	private final TypeMapper<JAXBElement<?>> elementMapper;
	private final TypeMapper<Object> typeMapper;
	private ADEMarshallerHelper helper;
	
	public FoodWaterEnergyLandUseADEMarshaller() {
		elementMapper = TypeMapper.<JAXBElement<?>>create()
				.with(FoodWaterEnergy.class, this::createFoodWaterEnergy)
				.with(ConsistsOfFWESurvey.class, this::createConsistsOfFWESurvey)
				.with(LandUseSurvey.class, this::createLandUseSurvey)
				.with(FoodSurvey.class, this::createFoodSurvey)
				.with(WaterSurvey.class, this::createWaterSurvey)
				.with(EnergySurvey.class, this::createEnergySurvey);
		
		typeMapper = TypeMapper.create()
				.with(FoodWaterEnergy.class, this::marshalFoodWaterEnergy)
				.with(ConsistsOfFWESurvey.class, this::marshalConsistsOfFWESurvey)
				.with(LandUseSurvey.class, this::marshalLandUseSurvey)
				.with(FoodSurvey.class, this::marshalFoodSurvey)
				.with(WaterSurvey.class, this::marshalWaterSurvey)
				.with(EnergySurvey.class, this::marshalEnergySurvey);
	}

	@Override
	public void setADEMarshallerHelper(ADEMarshallerHelper helper) {
		this.helper = helper;
	}

	@Override
	public JAXBElement<?> marshalJAXBElement(ADEModelObject src) {
		return elementMapper.apply(src);
	}

	@Override
	public Object marshal(ADEModelObject src) {
		return typeMapper.apply(src);
	}
	
	private JAXBElement<?> createFoodWaterEnergy(FoodWaterEnergy src) {
		return factory.createFoodWaterEnergyType(marshalFoodWaterEnergy(src));
	}
	
	private FoodWaterEnergyType marshalFoodWaterEnergy(FoodWaterEnergy src) {
		FoodWaterEnergyType dest = new FoodWaterEnergyType();
		/*
		helper.getGMLMarshaller().marshalAbstractFeature(src, dest);
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.isAreaName()) {
			dest.setAreaName(src.getAreaName());
		}
		if(src.isSystemName()) {
			dest.setSystemName(src.getSystemName());
		}
		if(src.isConsistsOfFWESurvey()) {
			dest.setConsistsOfFWESurveyType(marshalConsistsOfFWESurvey(src.getConsistsOfFWESurvey()));;
		}
		
		helper.getGMLMarshaller().marshalAbstractGML(src, dest);
		return dest;
	}
	
	private JAXBElement<?> createConsistsOfFWESurvey(ConsistsOfFWESurvey src) {
		return factory.createFConsistsOfFWESurveyType(marshalConsistsOfFWESurvey(src));
	}
	
	
	private ConsistsOfFWESurveyType marshalConsistsOfFWESurvey(ConsistsOfFWESurvey src) {
		ConsistsOfFWESurveyType dest = new ConsistsOfFWESurveyType();
		/*
		helper.getGMLMarshaller().marshalAbstractFeature(src, dest);
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.isLandUseSurvey()) {
			for(LandUseSurvey lus : src.getLandUseSurvey()) {
				dest.getLandUseSurveyType().add(marshalLandUseSurvey(lus));
			}
		}
		
		helper.getGMLMarshaller().marshalAbstractGML(src, dest);
		return dest;
	}
	
	private JAXBElement<?> createLandUseSurvey(LandUseSurvey src) {
		return factory.createLandUseSurveyType(marshalLandUseSurvey(src));
	}
	
	private LandUseSurveyType marshalLandUseSurvey(LandUseSurvey src) {
		LandUseSurveyType dest = new LandUseSurveyType();
		/*
		helper.getGMLMarshaller().marshalAbstractFeature(src, dest);
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.isBuildingFootprintArea()) {
			dest.setBuildingFootprintArea(helper.getGMLMarshaller().marshalArea(src.getBuildingFootprintArea()));
		}
		if(src.isPopulation()) {
			dest.setPopulation(src.getPopulation());
		}
		if(src.isSurfaceArea()) {
			dest.setSurfaceArea(helper.getGMLMarshaller().marshalArea(src.getSurfaceArea()));
		}
		if(src.isSurveyDescription()) {
			dest.setSurveyDescription(src.getSurveyDescription());
		}
		if(src.isSurveyYear()) {
			dest.setSurveyYear(src.getSurveyYear());
		}
		if(src.isSurveyYearVersion()) {
			dest.setSurveyYearVersion(src.getSurveyYearVersion());
		}
		if(src.isFoodsurvey()) {
			dest.setFoodSurveyType(marshalFoodSurvey(src.getFoodsurvey()));
		}
		if(src.isWatersurvey()) {
			dest.setWaterSurveyType(marshalWaterSurvey(src.getWatersurvey()));
		}
		if(src.isEnergySurvey()) {
			dest.setEnergySurveyType(marshalEnergySurvey(src.getEnergySurvey()));
		}
		
		
		helper.getGMLMarshaller().marshalAbstractGML(src, dest);
		
		return dest;
	}
	
	private JAXBElement<?> createFoodSurvey(FoodSurvey src) {
		return factory.createFoodSurveyType(marshalFoodSurvey(src));
	}
	
	private FoodSurveyType marshalFoodSurvey(FoodSurvey src) {
		FoodSurveyType dest = new FoodSurveyType();
		/*
		helper.getGMLMarshaller().marshalAbstractFeature(src, dest);
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.isFoodCategory()) {
			dest.setFoodCategory(helper.getGMLMarshaller().marshalCode(src.getFoodCategory()));
		}
		if(src.isFoodConsumption()) {
			dest.setFoodConsumption(helper.getGMLMarshaller().marshalScale(src.getFoodConsumption()));
		}
		
		if(src.isFoodDemand()) {
			dest.setFoodDemand(helper.getGMLMarshaller().marshalScale(src.getFoodDemand()));
		}
		
		if(src.isFoodProduction()) {
			dest.setFoodProduction(helper.getGMLMarshaller().marshalScale(src.getFoodProduction()));
		}
		
		if(src.isFoodProductionEnergyDemand()) {
			dest.setFoodProductionEnergyDemand(helper.getGMLMarshaller().marshalScale(src.getFoodProductionEnergyDemand()));
		}
		
		if(src.isFoodProductionWaterDemand()) {
			dest.setFoodProductionWaterDemand(helper.getGMLMarshaller().marshalScale(src.getFoodProductionWaterDemand()));
		}
		
		helper.getGMLMarshaller().marshalAbstractGML(src, dest);
		return dest;		
	}
	
	private JAXBElement<?> createWaterSurvey(WaterSurvey src) {
		return factory.createWaterSurveyType(marshalWaterSurvey(src));
	}
	
	private WaterSurveyType marshalWaterSurvey(WaterSurvey src) {
		WaterSurveyType dest = new WaterSurveyType();
		/*
		helper.getGMLMarshaller().marshalAbstractFeature(src, dest);
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		
		if(src.isDomesticHotWaterDemand()) {
			dest.setDomesticHotWaterDemand(helper.getGMLMarshaller().marshalScale(src.getDomesticHotWaterDemand()));
		}
		if(src.isDomesticHotWaterEnergyDemand()) {
			dest.setDomesticHotWaterEnergyDemand(helper.getGMLMarshaller().marshalScale(src.getDomesticHotWaterEnergyDemand()));
		}
		if(src.isDomesticWaterDemand()) {
			dest.setDomesticWaterDemand(helper.getGMLMarshaller().marshalScale(src.getDomesticWaterDemand()));
		}
		if(src.isDomesticSolidWaste()) {
			dest.setDomesticSolidWaste(helper.getGMLMarshaller().marshalScale(src.getDomesticSolidWaste()));
		}
		if(src.isDomesticWaterWaste()) {
			dest.setDomesticWaterWaste(helper.getGMLMarshaller().marshalScale(src.getDomesticWaterWaste()));
		}
		if(src.isTotalDomesticWaste()) {
			dest.setTotalDomesticWaste(helper.getGMLMarshaller().marshalScale(src.getTotalDomesticWaste()));
		}
		
		helper.getGMLMarshaller().marshalAbstractGML(src, dest);
		
		return dest;
	}


	private JAXBElement<?> createEnergySurvey(EnergySurvey src) {
		return factory.createEnergySurveyType(marshalEnergySurvey(src));
	}
	
	private EnergySurveyType marshalEnergySurvey(EnergySurvey src) {
		EnergySurveyType dest = new EnergySurveyType();
		/*
		helper.getGMLMarshaller().marshalAbstractFeature(src, dest);
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		
	
		if(src.isBiomassPrimaryEnergyPotential()) {
			dest.setBiomassPrimaryEnergyPotential(helper.getGMLMarshaller().marshalScale(src.getBiomassPrimaryEnergyPotential()));
		}
		if(src.isBiomassToBioethanol()) {
			dest.setBiomassToBioethanol(helper.getGMLMarshaller().marshalScale(src.getBiomassToBioethanol()));
		}
		if(src.isBiomassToBiogas()) {
			dest.setBiomassToBiogas(helper.getGMLMarshaller().marshalScale(src.getBiomassToBiogas()));
		}
		if(src.isBiomassToElectricityPotential()) {
			dest.setBiomassToElectricityPotential(helper.getGMLMarshaller().marshalScale(src.getBiomassToElectricityPotential()));
		}
		if(src.isBiomassToEnergyWoods()) {
			dest.setBiomassToEnergyWoods(helper.getGMLMarshaller().marshalScale(src.getBiomassToEnergyWoods()));
		}
		if(src.isBiomassToPlantOil()) {
			dest.setBiomassToPlantOil(helper.getGMLMarshaller().marshalScale(src.getBiomassToPlantOil()));
		}
		if(src.isBiomassToResidualSolidFuel()) {
			dest.setBiomassToResidualSolidFuel(helper.getGMLMarshaller().marshalScale(src.getBiomassToResidualSolidFuel()));
		}
		if(src.isBiomassToSolidFuel()) {
			dest.setBiomassToSolidFuel(helper.getGMLMarshaller().marshalScale(src.getBiomassToSolidFuel()));
		}
		if(src.isBiomassToThermalPotential()) {
			dest.setBiomassToThermalPotential(helper.getGMLMarshaller().marshalScale(src.getBiomassToThermalPotential()));
		}
		if(src.isSpaceCoolingDemand()) {
			dest.setSpaceCoolingDemand(helper.getGMLMarshaller().marshalScale(src.getSpaceCoolingDemand()));
		}
		if(src.isSpaceHeatingDemand()) {
			dest.setSpaceHeatingDemand(helper.getGMLMarshaller().marshalScale(src.getSpaceHeatingDemand()));
		}
		if(src.isElectricityPotentialFromPVBuildings()) {
			dest.setElectricityPotentialFromPVBuildings(helper.getGMLMarshaller().marshalScale(src.getElectricityPotentialFromPVBuildings()));
		}
		if(src.isResidentialElectricityDemand()) {
			dest.setResidentialElectricityDemand(helper.getGMLMarshaller().marshalScale(src.getResidentialElectricityDemand()));
		}
		if(src.isVegetationCoverCategory()) {
			dest.setVegetationCoverCategory(helper.getGMLMarshaller().marshalCode(src.getVegetationCoverCategory()));
		}
		
		helper.getGMLMarshaller().marshalAbstractGML(src, dest);
		
		return dest;
	}
	
	
}


