package meerkat.bulletinboard.webapp;

import com.google.protobuf.ByteString;
import com.google.protobuf.Message;
import meerkat.bulletinboard.service.HelloProtoBuf;
import meerkat.protobuf.Crypto.*;
import meerkat.protobuf.BulletinBoardApi.*;
import meerkat.rest.Constants;

import javax.annotation.PostConstruct;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("/proto")
public class HelloProtoWebApp {
	private HelloProtoBuf helloProtoBuf;

	@PostConstruct
	public void init() {
		helloProtoBuf = new HelloProtoBuf();
	}

	@GET
	@Produces(Constants.MEDIATYPE_PROTOBUF)
	public Message hello() {
		byte[] b1 = { (byte) 1, (byte) 2, (byte) 3, (byte) 4 };
		byte[] b2 = { (byte) 11, (byte) 12, (byte) 13, (byte) 14 };
		byte[] b3 = {(byte) 21, (byte)22, (byte) 23, (byte) 24};
		
		Message msg;

		if (helloProtoBuf != null) {
			msg = helloProtoBuf.sayHello();
		} else {
			msg = BulletinBoardMessage.newBuilder()
					.setMsg(UnsignedBulletinBoardMessage.newBuilder()
							.addTag("Signature")
							.addTag("Trustee")
							.setData(ByteString.copyFrom(b1)).build())
					.addSig(Signature.newBuilder()
							.setType(SignatureType.DSA)
							.setData(ByteString.copyFrom(b2))
							.setSignerId(ByteString.copyFrom(b3)).build())
					.build();
		}

		return msg;
	}
}
