{-# LANGUAGE OverloadedStrings #-}

module Ebay.Types.Picture
    ( PictureDetails (..)
    , xpPictureDetails

    , GalleryStatusCode (..)
    , xpGalleryStatusCode

    , GalleryTypeCode (..)
    , xpGalleryTypeCode

    , PhotoDisplayCode (..)
    , xpPhotoDisplayCode

    , PictureSourceCode (..)
    , xpPictureSourceCode
    ) where

import           Data.Text                       as T
import           Data.XML.Pickle
import           Data.XML.Types
import           GHC.Read
import           Text.ParserCombinators.ReadPrec
import           Text.Read.Lex                   as L

import           Ebay.Types

data PictureDetails = PictureDetails
        { pictureExternalURL      :: [Text]
        , pictureGalleryDuration  :: Maybe Text
        , pictureGalleryErrorInfo :: Maybe Text
        , pictureGalleryStatus    :: Maybe GalleryStatusCode
        , pictureGalleryType      :: Maybe GalleryTypeCode
        , pictureGalleryURL       :: Maybe Text
        , picturePhotoDisplay     :: Maybe PhotoDisplayCode
        , pictureSource           :: Maybe PictureSourceCode
        , pictureURL              :: [Text]
        } deriving (Eq, Show)

xpPictureDetails :: PU [Node] PictureDetails
xpPictureDetails =
    xpWrap (\((((((((a, b), c), d), e), f), g), h), i) -> PictureDetails a b c d e f g h i)
           (\(PictureDetails a b c d e f g h i) -> ((((((((a, b), c), d), e), f), g), h), i)) $
        (xpList $ xpElemText "{urn:ebay:apis:eBLBaseComponents}ExternalPictureURL")
    <#> (xpOption $ xpElemText "{urn:ebay:apis:eBLBaseComponents}GalleryDuration")
    <#> (xpOption $ xpElemText "{urn:ebay:apis:eBLBaseComponents}GalleryErrorInfo")
    <#> (xpOption $ xpElemNodes "{urn:ebay:apis:eBLBaseComponents}GalleryStatus" $
            xpContent xpGalleryStatusCode)
    <#> (xpOption $ xpElemNodes "{urn:ebay:apis:eBLBaseComponents}GalleryType" $
            xpContent xpGalleryTypeCode)
    <#> (xpOption $ xpElemText "{urn:ebay:apis:eBLBaseComponents}GalleryURL")
    <#> (xpOption $ xpElemNodes "{urn:ebay:apis:eBLBaseComponents}PhotoDisplay" $
            xpContent xpPhotoDisplayCode)
    <#> (xpOption $ xpElemNodes "{urn:ebay:apis:eBLBaseComponents}PictureSource" $
            xpContent xpPictureSourceCode)
    <#> (xpList $ xpElemText "{urn:ebay:apis:eBLBaseComponents}PictureURL")

----

data GalleryStatusCode = GSCustomCode | GSImageNonExistant | GSImageProcessingError
                       | GSImageReadTimeOut | GSInvalidFile | GSInvalidFileFormat
                       | GSInvalidProtocol | GSInvalidURL | GSPending | GSServerDown
                       | GSSuccess
                       deriving (Eq)

instance Show GalleryStatusCode where
    show GSCustomCode               = "CustomCode"
    show GSImageNonExistant         = "ImageNonExistant"
    show GSImageProcessingError     = "ImageProcessingError"
    show GSImageReadTimeOut         = "ImageReadTimeOut"
    show GSInvalidFile              = "InvalidFile"
    show GSInvalidFileFormat        = "InvalidFileFormat"
    show GSInvalidProtocol          = "InvalidProtocol"
    show GSInvalidURL               = "InvalidUrl"
    show GSPending                  = "Pending"
    show GSServerDown               = "ServerDown"
    show GSSuccess                  = "Success"

instance Read GalleryStatusCode where
    readPrec = parens $ do
        L.Ident s <- lexP
        case s of "CustomCode"             -> return GSCustomCode
                  "ImageNonExistant"       -> return GSImageNonExistant
                  "ImageProcessingError"   -> return GSImageProcessingError
                  "ImageReadTimeOut"       -> return GSImageReadTimeOut
                  "InvalidFile"            -> return GSInvalidFile
                  "InvalidFileFormat"      -> return GSInvalidFileFormat
                  "InvalidProtocol"        -> return GSInvalidProtocol
                  "InvalidUrl"             -> return GSInvalidURL
                  "Pending"                -> return GSPending
                  "ServerDown"             -> return GSServerDown
                  "Success"                -> return GSSuccess
                  _                        -> pfail

xpGalleryStatusCode :: PU Text GalleryStatusCode
xpGalleryStatusCode = xpPrim

----

data GalleryTypeCode = GTCustomCode | GTFeatured | GTGallery | GTNone | GTPlus
                     deriving (Eq)

instance Show GalleryTypeCode where
    show GTCustomCode   = "CustomCode"
    show GTFeatured     = "Featured"
    show GTGallery      = "Gallery"
    show GTNone         = "None"
    show GTPlus         = "Plus"

instance Read GalleryTypeCode where
    readPrec = parens $ do
        L.Ident s <- lexP
        case s of "CustomCode"   -> return GTCustomCode
                  "Featured"     -> return GTFeatured
                  "Gallery"      -> return GTGallery
                  "None"         -> return GTNone
                  "Plus"         -> return GTPlus
                  _              -> pfail

xpGalleryTypeCode :: PU Text GalleryTypeCode
xpGalleryTypeCode = xpPrim

----

data PhotoDisplayCode = PDCustomCode | PDNone | PDPicturePack | PDSuperSize
                      | PDSuperSizePictureShow
                      deriving (Eq)

instance Show PhotoDisplayCode where
    show PDCustomCode           = "CustomCode"
    show PDNone                 = "None"
    show PDPicturePack          = "PicturePack"
    show PDSuperSize            = "SuperSize"
    show PDSuperSizePictureShow = "SuperSizePictureShow"

instance Read PhotoDisplayCode where
    readPrec = parens $ do
        L.Ident s <- lexP
        case s of "CustomCode"           -> return PDCustomCode
                  "None"                 -> return PDNone
                  "PicturePack"          -> return PDPicturePack
                  "SuperSize"            -> return PDSuperSize
                  "SuperSizePictureShow" -> return PDSuperSizePictureShow
                  _                      -> pfail

xpPhotoDisplayCode :: PU Text PhotoDisplayCode
xpPhotoDisplayCode = xpPrim

----

data PictureSourceCode = PSCustomCode | PSEPS | PSVendor
                       deriving (Eq)

instance Show PictureSourceCode where
    show PSCustomCode       = "CustomCode"
    show PSEPS              = "EPS"
    show PSVendor           = "Vendor"

instance Read PictureSourceCode where
    readPrec = parens $ do
        L.Ident s <- lexP
        case s of "CustomCode" -> return PSCustomCode
                  "EPS"        -> return PSEPS
                  "Vendor"     -> return PSVendor
                  _            -> pfail

xpPictureSourceCode :: PU Text PictureSourceCode
xpPictureSourceCode = xpPrim
