{-# LANGUAGE OverloadedStrings #-}

module Messages (challenge1Msg, challenge2Msg, challenge3Msg, challenge2FlagMsg, challenge3FlagMsg) where

import qualified Data.ByteString as Bs
import Data.Word
import System.Random
import Data.Char

flags :: [Bs.ByteString]
flags = ["IGCTF{challenge_1_did_you_write_them_all_down!?}", "IGCTF{challenge2_in_case_it_wasnt_clear_I_dont_like_meetings}", "IGCTF{challenge_3_I_forgot_to_take_my_meds}"]

bsVerbs = [
    "aggregate",
    "architect",
    "benchmark",
    "brand",
    "cultivate",
    "deliver",
    "deploy",
    "disintermediate",
    "drive",
    "e-enable",
    "embrace",
    "empower",
    "enable",
    "engage",
    "engineer",
    "enhance",
    "envisioneer",
    "evolve",
    "expedite",
    "exploit",
    "extend",
    "facilitate",
    "generate",
    "grow",
    "harness",
    "implement",
    "incentivize",
    "incubate",
    "innovate",
    "integrate",
    "iterate",
    "leverage",
    "matrix",
    "maximize",
    "mesh",
    "monetize",
    "morph",
    "optimize",
    "orchestrate",
    "productize",
    "recontextualize",
    "redefine",
    "reintermediate",
    "reinvent",
    "repurpose",
    "revolutionize",
    "scale",
    "seize",
    "strategize",
    "streamline",
    "syndicate",
    "synergize",
    "synthesize",
    "target",
    "transform",
    "transition",
    "unleash",
    "utilize",
    "visualize",
    "whiteboard"]

bsAdjectives = [
    "24/365",
    "24/7",
    "B2B",
    "B2C",
    "back-end",
    "best-of-breed",
    "bleeding-edge",
    "bricks-and-clicks",
    "clicks-and-mortar",
    "collaborative",
    "compelling",
    "cross-platform",
    "cross-media",
    "customized",
    "cutting-edge",
    "distributed",
    "dot-com",
    "dynamic",
    "e-business",
    "efficient",
    "end-to-end",
    "enterprise",
    "extensible",
    "frictionless",
    "front-end",
    "global",
    "granular",
    "holistic",
    "impactful",
    "innovative",
    "integrated",
    "interactive",
    "intuitive",
    "killer",
    "leading-edge",
    "magnetic",
    "mission-critical",
    "next-generation",
    "one-to-one",
    "open-source",
    "out-of-the-box",
    "plug-and-play",
    "proactive",
    "real-time",
    "revolutionary",
    "rich",
    "robust",
    "scalable",
    "seamless",
    "sexy",
    "sticky",
    "strategic",
    "synergistic",
    "transparent",
    "turn-key",
    "ubiquitous",
    "user-centric",
    "value-added",
    "vertical",
    "viral",
    "virtual",
    "visionary",
    "web-enabled",
    "wireless",
    "world-class"]

bsNouns = [
    "action-items",
    "applications",
    "architectures",
    "bandwidth",
    "channels",
    "communities",
    "content",
    "convergence",
    "deliverables",
    "e-business",
    "e-commerce",
    "e-markets",
    "e-services",
    "e-tailers",
    "experiences",
    "eyeballs",
    "functionalities",
    "infomediaries",
    "infrastructures",
    "initiatives",
    "interfaces",
    "markets",
    "methodologies",
    "metrics",
    "mindshare",
    "models",
    "networks",
    "niches",
    "paradigms",
    "partnerships",
    "platforms",
    "portals",
    "relationships",
    "ROI",
    "synergies",
    "web-readiness",
    "schemas",
    "solutions",
    "supply-chains",
    "systems",
    "technologies",
    "users",
    "vortals",
    "web services"]

atRandIndex :: [a] -> IO a
atRandIndex l = do
    i <- randomRIO (0, length l - 1)
    return $ l !! i

asJson :: [Bs.ByteString] -> [Bs.ByteString] -> Bs.ByteString
asJson keys values = Bs.concat ["{", inner', "}"]
    where inner = Bs.concat $ zipWith (\k v -> Bs.concat ["\"", k, "\": \"", v, "\","]) keys values
          inner' = Bs.take (Bs.length inner - 1) inner

challenge1Msg :: IO Bs.ByteString
challenge1Msg = do
    bsVerb <- atRandIndex bsVerbs
    bsAdjective <- atRandIndex bsAdjectives
    bsNoun <- atRandIndex bsNouns
    return $ asJson ["verb", "adjective", "noun", "flag"] [bsVerb, bsAdjective, bsNoun, flags !! 0]

challenge2Msg :: Bs.ByteString
challenge2Msg = asJson ["morb", "flag"] ["GET ME OUT OF HERE", "<flag already sent>"]

challenge2FlagMsg :: Bs.ByteString
challenge2FlagMsg = asJson ["morb", "flag"] ["I'M GONNA MORB", flags !! 1]

uwufy :: Word8 -> Word8
uwufy w = if fromIntegral (ord 'l') == w || fromIntegral (ord 'r') == w then fromIntegral (ord 'w') else w

challenge3Msg :: IO Bs.ByteString
challenge3Msg = do
    bsVerb <- atRandIndex bsVerbs
    bsAdjective <- atRandIndex bsAdjectives
    bsNoun <- atRandIndex bsNouns
    let out = asJson ["verb", "adjective", "noun", "uwu"] [bsVerb, bsAdjective, bsNoun, "UwU"]
    return $ Bs.map uwufy out

challenge3FlagMsg :: Bs.ByteString
challenge3FlagMsg = Bs.concat ["The flag is: ", flags !! 2]
