{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE StandaloneDeriving    #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeOperators         #-}
{-# LANGUAGE UndecidableInstances  #-}

-- | Module that deals with handling config options
module LintConfig where

import           Control.Monad.Identity (Identity)
import           Data.Aeson             (FromJSON (parseJSON), Options (..),
                                         defaultOptions, eitherDecode)
import           Data.Aeson.Types       (genericParseJSON)
import qualified Data.ByteString.Char8  as C8
import qualified Data.ByteString.Lazy   as LB
import qualified Data.Map.Strict        as M
import           Data.Text              (Text)
import           GHC.Generics           (Generic (Rep, from, to), K1 (..),
                                         M1 (..), (:*:) (..))
import           Types                  (Level)
import           Uris                   (SchemaSet,
                                         Substitution (DomainSubstitution))
import           WithCli                (Proxy (..))
import           WithCli.Pure           (Argument (argumentType, parseArgument))

type family HKD f a where
  HKD Identity a = a
  HKD f a = f a

data LintConfig f = LintConfig
  { configScriptInject   :: HKD f (Maybe Text)
  -- ^ Link to Script that should be injected
  , configAssemblyTag    :: HKD f Text
  -- ^ Assembly name (used for jitsiRoomAdminTag)
  , configAssemblies     :: HKD f [Text]
  -- ^ list of all assembly slugs (used to lint e.g. world:// links)
  , configMaxLintLevel   :: HKD f Level
  -- ^ Maximum warn level allowed before the lint fails
  , configDontCopyAssets :: HKD f Bool
  -- ^ Don't copy map assets (mostly useful for development)
  , configAllowScripts   :: HKD f Bool
  -- ^ Allow defining custom scripts in maps
  , configUriSchemas     :: HKD f SchemaSet
  } deriving (Generic)

type LintConfig' = LintConfig Identity

-- TODO: should probably find a way to write these constraints nicer ...
deriving instance
  ( Show (HKD a (Maybe Text))
  , Show (HKD a Text)
  , Show (HKD a Level)
  , Show (HKD a [Text])
  , Show (HKD a Bool)
  , Show (HKD a SchemaSet)
  )
  => Show (LintConfig a)

aesonOptions :: Options
aesonOptions = defaultOptions
  { omitNothingFields = True
  , rejectUnknownFields = True
  , fieldLabelModifier = drop 6
  }

instance
    ( FromJSON (HKD a (Maybe Text))
    , FromJSON (HKD a [Text])
    , FromJSON (HKD a Text)
    , FromJSON (HKD a Level)
    , FromJSON (HKD a Bool)
    , FromJSON (HKD a SchemaSet)
    )
    => FromJSON (LintConfig a)
  where
    parseJSON = genericParseJSON aesonOptions

-- need to define this one extra, since Aeson will not make
-- Maybe fields optional if the type isn't given explicitly.
--
-- Whoever said instances had confusing semantics?
instance {-# Overlapping #-} FromJSON (LintConfig Maybe) where
  parseJSON = genericParseJSON aesonOptions



-- | generic typeclass for things that are "patchable"
class GPatch i m where
  gappend :: i p -> m p -> i p

-- generic instances. It's category theory, but with confusing names!
instance GPatch (K1 a k) (K1 a (Maybe k)) where
  gappend _ (K1 (Just k'))    = K1 k'
  gappend (K1 k) (K1 Nothing) = K1 k
  {-# INLINE gappend #-}

instance (GPatch i o, GPatch i' o')
    => GPatch (i :*: i') (o :*: o') where
  gappend (l :*: r) (l' :*: r') = gappend l l' :*: gappend r r'
  {-# INLINE gappend #-}

instance GPatch i o
    => GPatch (M1 _a _b i) (M1 _a' _b' o) where
  gappend (M1 x) (M1 y) = M1 (gappend x y)
  {-# INLINE gappend #-}


-- | A patch function. For (almost) and a :: * -> *,
-- take an a Identity and an a Maybe, then replace all appropriate
-- values in the former with those in the latter.
--
-- There isn't actually any useful reason for this function to be this
-- abstract, I just wanted to play around with higher kinded types for
-- a bit.
patch ::
  ( Generic (f Maybe)
  , Generic (f Identity)
  , GPatch (Rep (f Identity))
    (Rep (f Maybe))
  )
  => f Identity
  -> f Maybe
  -> f Identity
patch x y = to (gappend (from x) (from y))

patchConfig :: LintConfig Identity -> Maybe (LintConfig Maybe) -> LintConfig Identity
patchConfig config p = config'
  { configUriSchemas = ("world", assemblysubsts) : configUriSchemas config'}
  where config' = case p of
          Just p  -> patch config p
          Nothing -> config
        assemblysubsts =
          DomainSubstitution (M.fromList generated) scope
          where generated = (\slug -> (slug, "/@/rc3_21/"<>slug)) <$> configAssemblies config'
        scope = (\(DomainSubstitution _ s) -> s)
         . snd . head
         . filter ((==) "world" . fst)
         $ configUriSchemas config'


instance (FromJSON (LintConfig a)) => Argument (LintConfig a) where
  parseArgument str =
    case eitherDecode (LB.fromStrict $ C8.pack str) of
      Left _    -> Nothing
      Right res -> Just res

  argumentType Proxy = "LintConfig"