{-# LANGUAGE NoImplicitPrelude #-}
module Bamboo.Model.Static where

import Bamboo.Env hiding (match)
import Bamboo.Model.Helper
import Bamboo.Type.Reader
import qualified Data.ByteString.Char8 as S


data Static = Static 
  { uid :: String
  , body :: S.ByteString
  , reader :: Reader
  }
  deriving (Show, Eq)

instance Resource Static where
  resource_title x = (static_config.static_id / x.uid.get_title) .spaced_url

instance Markable Static where
  markup = (reader &&& body) > splash render_to_html

instance Default Static where
  def = Static def S.empty def

-- CRUD
instance FlatRead Static where
  flat_read x = do
    t <- get_body x
    def {body = t, uid = x} .return

instance Gettable Static where
  get id = flat_read id

get_title :: SC
get_title = id_to_resource > drop_known_extension > split "/" > last

title :: Static -> String
title = uid > get_title