/**
 * Copyright 2015-2015 Ralph Schaer <ralphschaer@gmail.com>
 *
 * 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 ch.rasc.bsoncodec.codegen.delegate;

import java.time.Instant;
import java.time.Month;
import java.time.Year;
import java.util.Date;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

import javax.lang.model.type.TypeMirror;

import com.squareup.javapoet.MethodSpec;

import ch.rasc.bsoncodec.Util;
import ch.rasc.bsoncodec.codegen.CodeGeneratorContext;

public class CoreClassesDelegate implements CodeGeneratorDelegate {

	@Override
	public void addEncodeStatements(TypeMirror type, CodeGeneratorContext ctx) {
		MethodSpec.Builder builder = ctx.builder();

		if (Util.isSameType(type, String.class)) {
			builder.addStatement("writer.writeString($L)", ctx.getter());
		}
		else if (Util.isSameType(type, Date.class)) {
			builder.addStatement("writer.writeDateTime($L.getTime())", ctx.getter());
		}
		else if (Util.isSameType(type, AtomicBoolean.class)) {
			builder.addStatement("writer.writeBoolean($L.get())", ctx.getter());
		}
		else if (Util.isSameType(type, AtomicInteger.class)) {
			builder.addStatement("writer.writeInt32($L.intValue())", ctx.getter());
		}
		else if (Util.isSameType(type, AtomicLong.class)) {
			builder.addStatement("writer.writeInt64($L.longValue())", ctx.getter());
		}
		else if (Util.isSameType(type, Instant.class)) {
			builder.addStatement("writer.writeInt64($L.toEpochMilli())", ctx.getter());
		}
		else if (Util.isSameType(type, Year.class)) {
			builder.addStatement("writer.writeInt32($L.getValue())", ctx.getter());
		}
		else if (Util.isSameType(type, Month.class)) {
			builder.addStatement("writer.writeInt32($L.getValue())", ctx.getter());
		}

	}

	@Override
	public void addDecodeStatements(TypeMirror type, CodeGeneratorContext ctx) {
		MethodSpec.Builder builder = ctx.builder();

		if (Util.isSameType(type, String.class)) {
			builder.addStatement(ctx.setter("reader.readString()"));
		}
		else if (Util.isSameType(type, Date.class)) {
			builder.addStatement(ctx.setter("new $T(reader.readDateTime())"), Date.class);
		}
		else if (Util.isSameType(type, AtomicBoolean.class)) {
			builder.addStatement(ctx.setter("new $T(reader.readBoolean())"),
					AtomicBoolean.class);
		}
		else if (Util.isSameType(type, AtomicInteger.class)) {
			builder.addStatement(ctx.setter("new $T(reader.readInt32())"),
					AtomicInteger.class);
		}
		else if (Util.isSameType(type, AtomicLong.class)) {
			builder.addStatement(ctx.setter("new $T(reader.readInt64())"),
					AtomicLong.class);
		}
		else if (Util.isSameType(type, Instant.class)) {
			builder.addStatement(ctx.setter("$T.ofEpochMilli(reader.readInt64())"),
					Instant.class);
		}
		else if (Util.isSameType(type, Year.class)) {
			builder.addStatement(ctx.setter("$T.of(reader.readInt32())"), Year.class);
		}
		else if (Util.isSameType(type, Month.class)) {
			builder.addStatement(ctx.setter("$T.of(reader.readInt32())"), Month.class);
		}
	}

	@Override
	public boolean accepts(TypeMirror type) {
		return Util.isAnyType(type, String.class, Date.class, AtomicBoolean.class,
				AtomicInteger.class, AtomicLong.class, Instant.class, Year.class,
				Month.class);
	}

}

@@ -1931,397 +1931,8 @@
 %0A%09%09%7D
-%0A%09%09else if (Util.isSameType(type, Instant.class)) %7B%0A%09%09%09builder.addStatement(%22writer.writeInt64($L.toEpochMilli())%22, ctx.getter());%0A%09%09%7D%0A%09%09else if (Util.isSameType(type, Year.class)) %7B%0A%09%09%09builder.addStatement(%22writer.writeInt32($L.getValue())%22, ctx.getter());%0A%09%09%7D%0A%09%09else if (Util.isSameType(type, Month.class)) %7B%0A%09%09%09builder.addStatement(%22writer.writeInt32($L.getValue())%22, ctx.getter());%0A%09%09%7D
 %0A%0A%09%7D
@@ -2774,421 +2774,8 @@
 %09%09%7D%0A
-%09%09else if (Util.isSameType(type, Instant.class)) %7B%0A%09%09%09builder.addStatement(ctx.setter(%22$T.ofEpochMilli(reader.readInt64())%22),%0A%09%09%09%09%09Instant.class);%0A%09%09%7D%0A%09%09else if (Util.isSameType(type, Year.class)) %7B%0A%09%09%09builder.addStatement(ctx.setter(%22$T.of(reader.readInt32())%22), Year.class);%0A%09%09%7D%0A%09%09else if (Util.isSameType(type, Month.class)) %7B%0A%09%09%09builder.addStatement(ctx.setter(%22$T.of(reader.readInt32())%22), Month.class);%0A%09%09%7D%0A
 %09%7D%0A%0A
@@ -2901,24 +2901,24 @@
 lean.class,%0A
+
 %09%09%09%09AtomicIn
@@ -2950,52 +2950,8 @@
 lass
-, Instant.class, Year.class,%0A%09%09%09%09Month.class
 );%0A%09

Remove Instant, Year and Month