{-# LANGUAGE OverloadedStrings #-}
module Hasmin.Types.BgSize
( BgSize(..)
, Auto(..)
) where
import Control.Monad.Reader (Reader)
import Data.Monoid ((<>))
import Data.Text.Lazy.Builder (singleton)
import Hasmin.Class
import Hasmin.Config
import Hasmin.Types.PercentageLength
data Auto = Auto
deriving (Eq, Show)
instance ToText Auto where
toBuilder Auto = "auto"
data BgSize = Cover
| Contain
| BgSize1 (Either PercentageLength Auto)
| BgSize2 (Either PercentageLength Auto) (Either PercentageLength Auto)
deriving Show
instance Eq BgSize where
BgSize1 x1 == BgSize1 x2 = x1 `bgsizeArgEq` x2
BgSize2 x1 y == BgSize1 x2 = x1 `bgsizeArgEq` x2 && y == Right Auto
x@BgSize1{} == y@BgSize2{} = y == x
BgSize2 x1 y1 == BgSize2 x2 y2 = x1 `bgsizeArgEq` x2 && y1 `bgsizeArgEq` y2
Cover == Cover = True
Contain == Contain = True
_ == _ = False
bgsizeArgEq :: Either PercentageLength Auto -> Either PercentageLength Auto -> Bool
bgsizeArgEq (Left x) (Left y) = isZero x && isZero y || x == y
bgsizeArgEq x y = x == y
instance ToText BgSize where
toBuilder Cover = "cover"
toBuilder Contain = "contain"
toBuilder (BgSize1 x) = toBuilder x
toBuilder (BgSize2 x y) = toBuilder x <> singleton ' ' <> toBuilder y
instance Minifiable BgSize where
minify (BgSize1 x) = BgSize1 <$> minifyBgSizeArg x
minify (BgSize2 x y) = do
nx <- minifyBgSizeArg x
ny <- minifyBgSizeArg y
let b = BgSize2 nx ny
pure $ if True
then minifyBgSize b
else b
where minifyBgSize (BgSize2 l (Right Auto)) = BgSize1 l
minifyBgSize z = z
minify x = pure x
minifyBgSizeArg :: Either PercentageLength Auto
-> Reader Config (Either PercentageLength Auto)
minifyBgSizeArg (Left a) = Left <$> minifyPL a
minifyBgSizeArg (Right Auto) = pure $ Right Auto