package com.eigengraph.egf2.generator.mappers

import com.eigengraph.egf2.generator.Field
import com.squareup.javapoet.FieldSpec
import com.squareup.javapoet.MethodSpec
import java.util.*
import javax.lang.model.element.Modifier

class IntegerMapper(targetPackage: String) : Mapper(targetPackage) {
	override fun getField(field: Field, custom_schemas: LinkedHashMap<String, LinkedList<Field>>): FieldSpec {
		val fs: FieldSpec.Builder
		fs = FieldSpec.builder(Int::class.java, field.name, Modifier.PUBLIC)
		return fs.build()
	}

	override fun deserialize(field: Field, supername: String, deserialize: MethodSpec.Builder, custom_schemas: LinkedHashMap<String, LinkedList<Field>>) {
		if (field.required) {
			deserialize.addStatement("final int \$L = jsonObject.get(\"\$L\").getAsInt()", field.name, field.name)
			deserialize.addStatement("\$L.\$L = \$L", supername, field.name, field.name)
		} else {
			deserialize.beginControlFlow("if(jsonObject.has(\"\$L\"))", field.name)
			deserialize.addStatement("final int \$L = jsonObject.get(\"\$L\").getAsInt()", field.name, field.name)
			deserialize.addStatement("\$L.\$L = \$L", supername, field.name, field.name)
			deserialize.endControlFlow()
		}
		deserialize.addCode("\n")
	}
}