{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE TemplateHaskell #-}
module Orbat where

import Data.Aeson
import GHC.Generics (Generic)
import Control.Lens

import Unit
import Color


data Orbat = Orbat
  { _orbatColor :: Color
  , _orbatUnits :: [Unit]
  , _orbatSpottedUnits :: [UnitProjection]
  }
  deriving (Eq, Show, Generic)

makeLenses ''Orbat


instance FromJSON Orbat
instance ToJSON Orbat


orbatFindUnit :: UnitId -> Orbat -> Maybe Unit
orbatFindUnit uid ob =
  let filt = filter (\u -> (u ^. unitId) == uid)
      us = ob ^. orbatUnits in
    case filt us of
      [u] -> Just u
      _ -> Nothing


orbatRemoveUnit :: UnitId -> Orbat -> Orbat
orbatRemoveUnit uid ob =
  ob & orbatUnits %~ filter (\u -> (u ^. unitId) /= uid)


orbatReplaceUnit :: Unit -> Orbat -> Orbat
orbatReplaceUnit u ob =
  let ob' = orbatRemoveUnit (u ^. unitId) ob in
      ob' & orbatUnits %~ (++) [u]

