{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

{-

This file is part of the Haskell package playlists. It is subject to
the license terms in the LICENSE file found in the top-level directory
of this distribution and at git://pmade.com/playlists/LICENSE. No part
of playlists package, including this file, may be copied, modified,
propagated, or distributed except according to the terms contained in
the LICENSE file.

-}

--------------------------------------------------------------------------------
module Text.Playlist.M3U.Writer (writePlaylist) where

--------------------------------------------------------------------------------
import Data.ByteString.Lazy.Builder (Builder)
import qualified Data.ByteString.Lazy.Builder as B
import Data.Text (Text)
import Data.Text.Encoding (encodeUtf8)
import Text.Playlist.Types

--------------------------------------------------------------------------------
writePlaylist :: Playlist -> Builder
writePlaylist :: Playlist -> Builder
writePlaylist Playlist
x = ByteString -> Builder
B.byteString ByteString
"#EXTM3U\n" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ((Track -> Builder) -> Playlist -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map Track -> Builder
writeTrack Playlist
x)

--------------------------------------------------------------------------------
writeTrack :: Track -> Builder
writeTrack :: Track -> Builder
writeTrack Track
x =
  Track -> Builder
writeTitleAndLength Track
x Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
  ByteString -> Builder
B.byteString (Text -> ByteString
encodeUtf8 (Text -> ByteString) -> Text -> ByteString
forall a b. (a -> b) -> a -> b
$ Track -> Text
trackURL Track
x) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
  Char -> Builder
B.charUtf8 Char
'\n'

--------------------------------------------------------------------------------
writeTitleAndLength :: Track -> Builder
writeTitleAndLength :: Track -> Builder
writeTitleAndLength (Track Text
_ Maybe Text
Nothing Maybe Float
Nothing) = Builder
forall a. Monoid a => a
mempty
writeTitleAndLength Track{Maybe Float
Maybe Text
Text
trackDuration :: Track -> Maybe Float
trackTitle :: Track -> Maybe Text
trackDuration :: Maybe Float
trackTitle :: Maybe Text
trackURL :: Text
trackURL :: Track -> Text
..} =
  ByteString -> Builder
B.byteString ByteString
"#EXTINF:"   Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
  Maybe Float -> Builder
writeLength Maybe Float
trackDuration Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
  ByteString -> Builder
B.byteString ByteString
","          Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
  Maybe Text -> Builder
writeTitle Maybe Text
trackTitle     Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
  Char -> Builder
B.charUtf8 Char
'\n'

--------------------------------------------------------------------------------
writeLength :: Maybe Float -> Builder
writeLength :: Maybe Float -> Builder
writeLength Maybe Float
Nothing = Builder
forall a. Monoid a => a
mempty
writeLength (Just Float
l) = String -> Builder
B.stringUtf8 (Float -> String
forall a. Show a => a -> String
show Float
l)

--------------------------------------------------------------------------------
writeTitle :: Maybe Text -> Builder
writeTitle :: Maybe Text -> Builder
writeTitle Maybe Text
Nothing = Builder
forall a. Monoid a => a
mempty
writeTitle (Just Text
x) = ByteString -> Builder
B.byteString (Text -> ByteString
encodeUtf8 Text
x)