{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}

module Octane.Type.Value.GameModeValue
  ( GameModeValue(..)
  ) where

import Data.Aeson ((.=))

import qualified Data.Aeson as Aeson
import qualified Data.Bimap as Bimap
import qualified Data.Default.Class as Default
import qualified Data.OverloadedRecords.TH as OverloadedRecords
import qualified Data.Text as StrictText
import qualified Octane.Data as Data
import qualified Octane.Type.Word8 as Word8

getGameMode :: Word8.Word8 -> Maybe StrictText.Text
getGameMode x = Bimap.lookup (Word8.fromWord8 x) Data.gameModes

newtype GameModeValue = GameModeValue
  { gameModeValueUnpack :: Word8.Word8
  } deriving (Eq, Show)

$(OverloadedRecords.overloadedRecord Default.def ''GameModeValue)

instance Aeson.ToJSON GameModeValue where
  toJSON x =
    Aeson.object
      [ "Type" .= ("GameMode" :: StrictText.Text)
      , "Value" .=
        Aeson.object ["Id" .= #unpack x, "Name" .= getGameMode (#unpack x)]
      ]