{-# LANGUAGE NoImplicitPrelude #-}

module Panda.Model.Album where

-- env
import Panda.Helper.Env hiding (title, body, size, path, meta, get)
import qualified Panda.Config.Global as G
import Panda.Helper.StateHelper
import Panda.Helper.ThumbHelper
import List (sort)

data AlbumType = Galleria | Fade | SlideViewer | Popeye deriving (Eq, Show, Read)

instance Default AlbumType where
  def = Fade

data Album = Album
  { uid    :: String -- album/08-06-10
  , prefix :: String
  , show_description :: Bool
  , album_type :: AlbumType
  , width :: Int
  , pictures :: [String]
  }
  deriving (Show, Eq)

instance Resource Album where
  resource_title = uid > spaced_url
  
-- CRUD
ls_l x = ls x ^ map (x /)
image_path id = G.image_uri / id

get pre desc t w id = do
  id.image_path.convert_if_missing_thumb w
  get_pictures id ^ Album id pre desc t w
  
get_pictures = image_path > ls > (^ filter is_image) > (^ sort)

get_picture_title pre x 
  | pre.empty = x
  | otherwise = x.split pre.last.dropExtension

for_post pre id = get pre def def def (G.album_id / id.id_to_resource)

picture_links x = x.pictures .map ("/" / G.image_id / x.uid /)
picture_thumbs x = x.pictures .map ("/" / G.image_id / x.uid / G.thumb_id /)
picture_titles x = x.pictures .map (get_picture_title (x.prefix))

data_list x = zip3 (x.picture_links) (x.picture_titles) (x.picture_thumbs)

data AlbumData = 
    Prefix
  | Name
  | Pictures
  | ShowDescription
  | Type
  | Width
  deriving (Show)

from_list xs = get prefix show_description t w (G.album_id / at Name) where
  at x = xs.lookup (x.show_data).fromJust
  at' x d = xs.lookup (x .show_data) .fromMaybe d
  prefix = at' Prefix G.picture_prefix
  show_description = at' ShowDescription "y" .parse_boolean
  t = at' Type "fade" .camel_case .read
  w = at' Width "400" .read