package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

// abocost handler
func abocostHandler(response http.ResponseWriter, request *http.Request) {
	body, err := ioutil.ReadAll(request.Body)
	if err != nil {
		panic(err)
	}
	log.Println(string(body))

	var data map[string]int
	err2 := json.Unmarshal([]byte(body), &data)
	if err2 != nil {
		log.Println("Error, invalid json: %v", err)
	}
	response.Header().Set("Content-Type", "text/json; charset=utf-8")
	fmt.Fprintf(response, "{\n")
	fmt.Fprintf(response, "  \"stripe_pk\": \"%s\",\n", getStripePK())
	fmt.Fprintf(response, "  \"data\":  {\n")
	first := true
	for key, value := range data {
		fmt.Println("Key:", key, "Value:", value)
		if first {
			first = false
		} else {
			fmt.Fprintf(response, "  ,")
		}
		fmt.Fprintf(response, "    \"%s\": [ %d, %d, \"%s\", \"%s\" ]\n", key, int(value), int(getYearlyAboCost(key)*int(value)), getActiveUntil(key), getDevAlias(key))
	}
	fmt.Fprintf(response, "              }\n")
	fmt.Fprintf(response, "}\n")
}
