package com.mesc.service.controller;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mesc.service.entity.Invoice;
import com.mesc.service.entity.Tax;
import com.mesc.service.services.InvoiceService;

@RestController
public class InvoiceController {
	/* Path for Invoice related services */
	private static final String INVOICE_PATH = "/invoice";

	private static Gson gson = new GsonBuilder().create();
	private final InvoiceService invService;

	@Autowired
	InvoiceController(InvoiceService invService) {
		this.invService = invService;
	}

	/**
	 * Create new invoice
	 * 
	 * @return
	 */
	@RequestMapping(value = INVOICE_PATH, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public String createInvoice() {
		return gson.toJson(invService.create(), Invoice.class);
	}

	/**
	 * Preview invoice, before creation
	 * 
	 * @return
	 */
	@RequestMapping(value = INVOICE_PATH, method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
	public String invoicePreview() {
		return gson.toJson(new Invoice(), Invoice.class);
	}

	/**
	 * Get the latest 100 invoices
	 * 
	 * @return
	 */
	@RequestMapping(value = INVOICE_PATH, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public String getLatestInvoice() {
		return gson.toJson(new Invoice(), Invoice.class);
	}

	/**
	 * Work in progress These functions is entry point for other filters For
	 * example - invoices for a particular date / containing particular phone,
	 * etc
	 * 
	 * @param orderId
	 * @return
	 */
	@RequestMapping(value = INVOICE_PATH
			+ "/{orderId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public String findOrder(@PathVariable("orderId") String orderId) {
		// return gson.toJson(new Order(orderId), Order.class);
		BigDecimal invAmt = new BigDecimal(orderId);
		invAmt = invAmt.divide(new BigDecimal("100"));
		BigDecimal taxPct = new BigDecimal("0.06");
		return gson.toJson(new Tax(invAmt, taxPct), Tax.class);
	}
}

@@ -421,73 +421,8 @@
 r;%0A%0A
-import com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0A
 impo
@@ -457,16 +457,16 @@
 nvoice;%0A
+
 import c
@@ -541,16 +541,55 @@
 Service;
+%0Aimport com.mesc.service.util.GsonUtil;
 %0A%0A@RestC
@@ -733,64 +733,8 @@
 %22;%0A%0A
-%09private static Gson gson = new GsonBuilder().create();%0A
 %09pri
@@ -1069,30 +1069,37 @@
  %7B%0A%09%09return 
-gson.t
+GsonUtil.objT
 oJson(invSer
@@ -1344,38 +1344,45 @@
 ew() %7B%0A%09%09return 
-gson.t
+GsonUtil.objT
 oJson(new Invoic
@@ -1614,38 +1614,45 @@
 ce() %7B%0A%09%09return 
-gson.t
+GsonUtil.objT
 oJson(new Invoic
@@ -2283,30 +2283,37 @@
 );%0A%09%09return 
-gson.t
+GsonUtil.objT
 oJson(new Ta

Update to make use of GsonUtil