{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RecordWildCards     #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE TypeOperators       #-}
{-# LANGUAGE DeriveGeneric       #-}

-- | Generated from json response, now it's type safe, see the tests
module Speechmatics.JSON.PostJob where

import           System.Exit        (exitFailure, exitSuccess)
import           System.IO          (stderr, hPutStrLn)
import qualified Data.ByteString.Lazy.Char8 as BSL
import           System.Environment (getArgs)
import           Control.Monad      (forM_, mzero, join)
import           Control.Applicative
import           Data.Aeson.AutoType.Alternative
import           Data.Aeson(decode, Value(..), FromJSON(..), ToJSON(..),
                            pairs, eitherDecode,
                            (.:), (.:?), object)
import           Data.Monoid
import           Data.Text (Text)
import qualified GHC.Generics

-- | Workaround for https://github.com/bos/aeson/issues/287.
o .:?? val = fmap join (o .:? val)


data PostJob = PostJob { 
    checkWait :: (Maybe Value),
    cost :: Double,
    balance :: Double,
    postId :: Integer
  } deriving (Show,Eq,GHC.Generics.Generic)


instance FromJSON PostJob where
  parseJSON (Object v) = PostJob <$> v .:?? "check_wait" <*> v .:   "cost" <*> v .:   "balance" <*> v .:   "id"
  parseJSON _          = mzero

parse :: BSL.ByteString -> Either String PostJob
parse = eitherDecode