/*-
 * 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.unmarshal.citygml.ade.ADEUnmarshallerHelper;
import org.citygml4j.model.citygml.ade.binding.ADEModelObject;
import org.citygml4j.model.citygml.ade.binding.ADEUnmarshaller;
import org.citygml4j.util.mapper.CheckedTypeMapper;
import org.citygml4j.xml.io.reader.MissingADESchemaException;

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.WaterSurveyType;

public class FoodWaterEnergyLandUseADEUnMarshaller implements ADEUnmarshaller {	
	private final CheckedTypeMapper<ADEModelObject> typeMapper;
	private ADEUnmarshallerHelper helper;
	
	public FoodWaterEnergyLandUseADEUnMarshaller() {
		typeMapper = CheckedTypeMapper.<ADEModelObject>create()
				.with(FoodWaterEnergyType.class, this::unmarshalFoodWaterEnergyType)
				.with(ConsistsOfFWESurveyType.class, this::unmarshalConsistsOfFWESurveyType)
				.with(LandUseSurveyType.class, this::unmarshalLandUseSurveyType)
				.with(FoodSurveyType.class, this::unmarshalFoodSurveyType)
				.with(WaterSurveyType.class, this::unmarshalWaterSurveyType)
				.with(EnergySurveyType.class, this::unmarshalEnergySurveyType);
	}
	
	private FoodWaterEnergy unmarshalFoodWaterEnergyType(FoodWaterEnergyType src) throws MissingADESchemaException {
		FoodWaterEnergy dest = new FoodWaterEnergy();
		/*
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.getAreaName() != null) {
			dest.setAreaName(src.getAreaName());
		}
		if(src.getAreaName() != null) {
			dest.setSystemName(src.getSystemName());
		}
		if(src.getConsistsOfFWESurveyType() != null) {
			dest.setConsistsOfFWESurvey(unmarshalConsistsOfFWESurveyType(src.getConsistsOfFWESurveyType()));	
		}
	
		helper.getGMLUnmarshaller().unmarshalAbstractGML(src, dest);
		return dest;
	}
	
	private ConsistsOfFWESurvey unmarshalConsistsOfFWESurveyType(ConsistsOfFWESurveyType src) throws MissingADESchemaException {
		ConsistsOfFWESurvey dest = new ConsistsOfFWESurvey();
		/*
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.getLandUseSurveyType() !=null) {
			for(LandUseSurveyType lust : src.getLandUseSurveyType()) {
				dest.getLandUseSurvey().add(unmarshalLandUseSurveyType(lust));
			}	
		}
		
		helper.getGMLUnmarshaller().unmarshalAbstractGML(src, dest);
		return dest;
	}
	
	private LandUseSurvey unmarshalLandUseSurveyType(LandUseSurveyType src) throws MissingADESchemaException {
		LandUseSurvey dest = new LandUseSurvey();
		/*
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.getBuildingFootprintArea() != null) {
			dest.setBuildingFootprintArea(helper.getGMLUnmarshaller().unmarshalArea(src.getBuildingFootprintArea()));
		}
		if(src.getPopulation() != null) {
			dest.setPopulation(src.getPopulation());
		}
		if(src.getSurfaceArea() != null) {
			dest.setSurfaceArea(helper.getGMLUnmarshaller().unmarshalArea(src.getSurfaceArea()));
		}
		if(src.getSurveyDescription()!=null) {
			dest.setSurveyDescription(src.getSurveyDescription());	
		}
		if(src.getSurveyYear()!=null) {
			dest.setSurveyYear(src.getSurveyYear());
		}
		if(src.getSurveyYearVersion()!=null) {
			dest.setSurveyYearVersion(src.getSurveyYearVersion());	
		}
		if(src.getFoodSurveyType() != null) {
			dest.setFoodsurvey(unmarshalFoodSurveyType(src.getFoodSurveyType()));	
		}
		if(src.getWaterSurveyType() !=null) {
			dest.setWatersurvey(unmarshalWaterSurveyType(src.getWaterSurveyType()));	
		}
		if(src.getEnergySurveyType() !=null) {
			dest.setEnergySurvey(unmarshalEnergySurveyType(src.getEnergySurveyType()));	
		}
		
		helper.getGMLUnmarshaller().unmarshalAbstractGML(src, dest);
		return dest;
	}
	
	private FoodSurvey unmarshalFoodSurveyType(FoodSurveyType src) throws MissingADESchemaException {
		FoodSurvey dest = new FoodSurvey();
		/*
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.getFoodCategory()!=null) {
			dest.setFoodCategory(helper.getGMLUnmarshaller().unmarshalCode(src.getFoodCategory()));
		}
		if(src.getFoodConsumption()!=null) {
			dest.setFoodConsumption(helper.getGMLUnmarshaller().unmarshalScale(src.getFoodConsumption()));
		}
		if(src.getFoodDemand()!=null) {
			dest.setFoodDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getFoodDemand()));
		}
		if(src.getFoodProduction()!=null) {
			dest.setFoodProduction(helper.getGMLUnmarshaller().unmarshalScale(src.getFoodProduction()));	
		}
		if(src.getFoodProductionEnergyDemand()!=null) {
			dest.setFoodProductionEnergyDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getFoodProductionEnergyDemand()));
		}
		if(src.getFoodProductionWaterDemand()!=null) {
			dest.setFoodProductionWaterDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getFoodProductionWaterDemand()));
		}
		
		helper.getGMLUnmarshaller().unmarshalAbstractGML(src, dest);
		return dest;
	}
	
	private WaterSurvey unmarshalWaterSurveyType(WaterSurveyType src) throws MissingADESchemaException {
		WaterSurvey dest = new WaterSurvey();
		/*
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.getDomesticHotWaterDemand()!=null) {
			dest.setDomesticHotWaterDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getDomesticHotWaterDemand()));	
		}
		if(src.getDomesticHotWaterEnergyDemand()!=null) {
			dest.setDomesticHotWaterEnergyDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getDomesticHotWaterEnergyDemand()));	
		}
		if(src.getDomesticSolidWaste()!=null) {
			dest.setDomesticSolidWaste(helper.getGMLUnmarshaller().unmarshalScale(src.getDomesticSolidWaste()));	
		}
		if(src.getDomesticWaterDemand()!=null) {
			dest.setDomesticWaterDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getDomesticWaterDemand()));	
		}
		if(src.getDomesticWaterWaste()!=null) {
			dest.setDomesticWaterWaste(helper.getGMLUnmarshaller().unmarshalScale(src.getDomesticWaterWaste()));	
		}
		if(src.getTotalDomesticWaste()!=null) {
			dest.setTotalDomesticWaste(helper.getGMLUnmarshaller().unmarshalScale(src.getTotalDomesticWaste()));	
		}
		
		
		helper.getGMLUnmarshaller().unmarshalAbstractGML(src, dest);
		return dest;
	}
	
	private EnergySurvey unmarshalEnergySurveyType(EnergySurveyType src) throws MissingADESchemaException {
		EnergySurvey dest = new EnergySurvey();
		/*
		if(src.isSetId()) {
			dest.setId(src.getId());
		}
		*/
		if(src.getBiomassPrimaryEnergyPotential()!=null) {
			dest.setBiomassPrimaryEnergyPotential(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassPrimaryEnergyPotential()));	
		}
		if(src.getBiomassToBioethanol()!=null) {
			dest.setBiomassToBioethanol(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassToBioethanol()));	
		}
		if(src.getBiomassToBiogas()!=null) {
			dest.setBiomassToBiogas(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassToBiogas()));	
		}
		if(src.getBiomassToElectricityPotential()!=null) {
			dest.setBiomassToElectricityPotential(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassToElectricityPotential()));	
		}
		if(src.getBiomassToEnergyWoods() != null) {
			dest.setBiomassToEnergyWoods(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassToEnergyWoods()));	
		}
		if(src.getBiomassToPlantOil()!=null) {
			dest.setBiomassToPlantOil(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassToPlantOil()));
		}
		if(src.getBiomassToResidualSolidFuel()!=null) {
			dest.setBiomassToResidualSolidFuel(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassToResidualSolidFuel()));
		}
		if(src.getBiomassToSolidFuel()!=null) {
			dest.setBiomassToSolidFuel(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassToSolidFuel()));
		}
		if(src.getBiomassToThermalPotential()!=null) {
			dest.setBiomassToThermalPotential(helper.getGMLUnmarshaller().unmarshalScale(src.getBiomassToThermalPotential()));
		}
		if(src.getElectricityPotentialFromPVBuildings()!=null) {
			dest.setElectricityPotentialFromPVBuildings(helper.getGMLUnmarshaller().unmarshalScale(src.getElectricityPotentialFromPVBuildings()));
		}
		if(src.getResidentialElectricityDemand()!=null) {
			dest.setResidentialElectricityDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getResidentialElectricityDemand()));
		}
		if(src.getSpaceCoolingDemand()!=null) {
			dest.setSpaceCoolingDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getSpaceCoolingDemand()));
		}
		if(src.getSpaceCoolingDemand()!=null) {
			dest.setSpaceCoolingDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getSpaceCoolingDemand()));
		}
		if(src.getSpaceCoolingDemand()!=null) {
			dest.setSpaceCoolingDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getSpaceCoolingDemand()));
		}
		if(src.getSpaceCoolingDemand()!=null) {
			dest.setSpaceCoolingDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getSpaceCoolingDemand()));
		}
		if(src.getSpaceHeatingDemand()!=null) {
			dest.setSpaceHeatingDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getSpaceHeatingDemand()));
		}
		if(src.getResidentialElectricityDemand()!=null) {
			dest.setResidentialElectricityDemand(helper.getGMLUnmarshaller().unmarshalScale(src.getResidentialElectricityDemand()));
		}
		
		if(src.getVegetationCoverCategory()!=null) {
			dest.setVegetationCoverCategory(helper.getGMLUnmarshaller().unmarshalCode(src.getVegetationCoverCategory()));
		}
		
		helper.getGMLUnmarshaller().unmarshalAbstractGML(src, dest);	
		return dest;
	}
	
	@Override
	public void setADEUnmarshallerHelper(ADEUnmarshallerHelper helper) {
		this.helper = helper;
	}

	@Override
	public ADEModelObject unmarshal(JAXBElement<?> src) throws MissingADESchemaException {
		final Object value = src.getValue();
		
		return unmarshal(value);
	}

	@Override
	public ADEModelObject unmarshal(Object src) throws MissingADESchemaException {
		return typeMapper.apply(src);
	}
	

}
