{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE CPP                 #-}

module Distribution.Fedora.Release
  ( Release(releaseProductVersionId,releaseVersion,releaseProduct),
    readRelease
  )
where

import Data.Aeson(Object)
import Data.Text (Text)
import Fedora.PDC (lookupKey)

data Release = Release {
    Release -> Text
releaseProductVersionId :: Text,
--    releaseReleases :: [Text],
--    releaseAllowedPushTargets :: [[Maybe Value]],
--    releaseActive :: Bool,
--    releaseName :: Text,
    Release -> Text
releaseVersion :: Text,
--    releaseShort :: Text,
    Release -> Text
releaseProduct :: Text
  } deriving (Int -> Release -> ShowS
[Release] -> ShowS
Release -> String
(Int -> Release -> ShowS)
-> (Release -> String) -> ([Release] -> ShowS) -> Show Release
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Release] -> ShowS
$cshowList :: [Release] -> ShowS
show :: Release -> String
$cshow :: Release -> String
showsPrec :: Int -> Release -> ShowS
$cshowsPrec :: Int -> Release -> ShowS
Show,Release -> Release -> Bool
(Release -> Release -> Bool)
-> (Release -> Release -> Bool) -> Eq Release
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Release -> Release -> Bool
$c/= :: Release -> Release -> Bool
== :: Release -> Release -> Bool
$c== :: Release -> Release -> Bool
Eq)

instance Ord Release where
  compare :: Release -> Release -> Ordering
compare Release
r1 Release
r2 =
    Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Release -> Text
releaseProductVersionId Release
r1) (Release -> Text
releaseProductVersionId Release
r2)

readRelease :: Object -> Maybe Release
readRelease :: Object -> Maybe Release
readRelease Object
obj = do
  Text
pvid <- Text -> Object -> Maybe Text
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"product_version_id" Object
obj
  Text
ver <- Text -> Object -> Maybe Text
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"version" Object
obj
  Text
prod <- Text -> Object -> Maybe Text
forall a. FromJSON a => Text -> Object -> Maybe a
lookupKey Text
"product" Object
obj
  Release -> Maybe Release
forall (m :: * -> *) a. Monad m => a -> m a
return (Release -> Maybe Release) -> Release -> Maybe Release
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text -> Release
Release Text
pvid Text
ver Text
prod