module Pages.Debate.Gibberish exposing (Model, Msg, page)

import Config.Data.Identity exposing (pageNames)
import Config.Data.ImageFolders as M exposing (ImageFolder(..), imagePathMaker)
import Config.Helpers.Cards.Inner.StrengthBar
    exposing
        ( barMaker
        , barPadding
        )
import Config.Helpers.Cards.Inner.Text exposing (detailBodyMaker, detailFormat, detailTitleMaker, listMaker, listMaker2, numberedListItem)
import Config.Helpers.Cards.Inner.ToolTip
    exposing
        ( ToolTipPosition(..)
        , tooltip
        )
import Config.Helpers.Cards.Outer.Helpers exposing (cardMaker)
import Config.Helpers.Cards.Outer.Types as C exposing (Cardable(..))
import Config.Helpers.Converters exposing (toTitleCase)
import Config.Helpers.Headers.Helpers exposing (headerMaker)
import Config.Helpers.Headers.Records exposing (gibberishHeader)
import Config.Helpers.Headers.Types as R exposing (Header)
import Config.Helpers.Response
    exposing
        ( pageList
        , topLevelContainer
        )
import Config.Helpers.Viewport exposing (resetViewport)
import Config.Pages.Debate.Arguments.Types exposing (Argument)
import Config.Pages.Debate.Gibberish.List exposing (gibberishList)
import Config.Pages.Debate.Gibberish.Records.Epistemology exposing (epistemologyGibberish)
import Config.Pages.Debate.Gibberish.Records.Metaphysics exposing (metaphysicsGibberish)
import Config.Pages.Debate.Gibberish.Records.Normativity exposing (normativityGibberish)
import Config.Pages.Debate.Gibberish.Records.Ontology exposing (ontologyGibberish)
import Config.Pages.Debate.Gibberish.Records.PhilOfLanguage exposing (philOfLanguageGibberish)
import Config.Pages.Debate.Gibberish.Records.PhilOfMind exposing (philOfMindGibberish)
import Config.Pages.Debate.Gibberish.Records.Theology exposing (theologyGibberish)
import Config.Pages.Debate.Gibberish.Types
    exposing
        ( Explanation(..)
        , Gibberish
        , Terms
        )
import Config.Style.Colour.Helpers
    exposing
        ( ThemeColor(..)
        , getThemeColor
        )
import Config.Style.Fonts
    exposing
        ( defaultFontSize
        , paragraphSpacing
        )
import Config.Style.Images exposing (ElementSize(..), imageSquareMaker)
import Effect exposing (Effect)
import Element as E
    exposing
        ( Device
        , DeviceClass(..)
        , Element
        , Orientation(..)
        , alignTop
        , centerX
        , column
        , el
        , fill
        , height
        , none
        , paddingEach
        , paragraph
        , row
        , spacing
        , text
        , width
        )
import Element.Font as F
    exposing
        ( alignLeft
        , bold
        , center
        , color
        , regular
        )
import Layouts
import Page exposing (Page)
import Route exposing (Route)
import Shared
import View exposing (View)


page : Shared.Model -> Route () -> Page Model Msg
page shared route =
    Page.new
        { init = init
        , update = update
        , subscriptions = subscriptions
        , view = view shared
        }
        |> Page.withLayout toLayout


toLayout : Model -> Layouts.Layout Msg
toLayout model =
    Layouts.Navbar {}



-- INIT


type alias Model =
    {}


init : () -> ( Model, Effect Msg )
init () =
    ( {}
    , Effect.batch
        [ Effect.map
            (\_ -> NoOp)
            (Effect.sendCmd resetViewport)
        , Effect.none
        ]
    )



-- UPDATE


type Msg
    = NoOp


update : Msg -> Model -> ( Model, Effect Msg )
update msg model =
    case msg of
        NoOp ->
            ( model
            , Effect.none
            )



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none



-- VIEW


view : Shared.Model -> Model -> View Msg
view shared model =
    { title = "debate (" ++ pageNames.pageGibberish ++ ")"
    , attributes = []
    , element = gibberishContainer shared
    }


gibberishContainer : Shared.Model -> Element msg
gibberishContainer shared =
    topLevelContainer (gibberishList shared)


gibberishList : Shared.Model -> Element msg
gibberishList shared =
    column
        (case ( shared.device.class, shared.device.orientation ) of
            _ ->
                pageList shared.device
        )
    <|
        List.concat
            [ [ headerMaker (R.Gibberish gibberishHeader) ]
            , List.map
                (\gibberish ->
                    cardMaker shared.device (C.Gibberish gibberish) (contentList shared gibberish)
                )
                [ epistemologyGibberish
                , theologyGibberish
                , metaphysicsGibberish
                , normativityGibberish
                , ontologyGibberish
                , philOfMindGibberish
                , philOfLanguageGibberish
                ]
            ]


contentList : Shared.Model -> Gibberish -> List (Element msg)
contentList shared gibberish =
    let
        image : ElementSize -> Element msg
        image size =
            el
                [ E.alignLeft
                , paddingEach
                    { top = 0
                    , right = 10
                    , bottom = 0
                    , left = 0
                    }
                ]
            <|
                imageSquareMaker shared.device (imagePathMaker M.Gibberish gibberish.gibberishImage) True size
    in
    [ detailFormat paragraph
        [ case ( shared.device.class, shared.device.orientation ) of
            ( Phone, Portrait ) ->
                none

            ( Tablet, Portrait ) ->
                none

            _ ->
                image Medium
        , detailBodyMaker TextLightGrey (text gibberish.gibberishDescription)
        ]
    , gibberishMakerBody shared gibberish
    ]


gibberishMakerBody : Shared.Model -> Gibberish -> Element msg
gibberishMakerBody shared gibberish =
    detailFormat column
        [ listMaker2 (makeTerms shared) gibberish.gibberishTerms
        ]


makeTerms : Shared.Model -> Terms -> Int -> Element msg
makeTerms shared terms index =
    detailFormat column
        [ detailFormat row
            [ detailTitleMaker TextLightGrey
                (String.fromInt index ++ ". ")
            , el
                [ alignLeft
                , F.alignLeft
                , F.bold
                , F.color (getThemeColor TextLightOrange)
                , width fill
                ]
              <|
                text
                    ("\"" ++ String.toLower terms.term ++ "\"")
            ]
        , detailFormat column
            [ intelligibilityMaker shared terms
            , detailBodyMaker TextLightGrey <|
                text
                    (case terms.explanation of
                        NoClue ->
                            "I have no fucking clue what this means."

                        SpecificExplanation str ->
                            str
                    )
            ]
        ]


intelligibilityMaker : Shared.Model -> Terms -> Element msg
intelligibilityMaker shared terms =
    detailFormat row
        [ el
            (case ( shared.device.class, shared.device.orientation ) of
                ( Phone, Portrait ) ->
                    []

                ( Tablet, Portrait ) ->
                    []

                _ ->
                    if not shared.isNavbarExpanded then
                        [ tooltip IsLeft
                            "This represents my confidence that the term can be understood from at least one viewpoint."
                        ]

                    else
                        []
            )
          <|
            detailTitleMaker TextLightGrey "Intelligibility:"
        , barPadding
            [ barMaker shared getIntelligibilityTooltip terms.strength ]
        ]


getIntelligibilityTooltip : Int -> String
getIntelligibilityTooltip num =
    case num of
        0 ->
            "Total fucking gibberish."

        1 ->
            "Extremely unclear, speaking in tongues."

        2 ->
            "Mostly unclear, hard to make any sense of."

        3 ->
            "Somewhat unclear, difficult to understand."

        4 ->
            "Slightly unclear, understandable with effort."

        5 ->
            "Neutral, not sure what to make of it."

        6 ->
            "Slightly clear, with unanswered questions."

        7 ->
            "Somewhat clear, kinda get the idea."

        8 ->
            "Very clear, usefulness questionable."

        9 ->
            "Extremely clear, usefulness dubious."

        10 ->
            "Perfectly clear, but also useless."

        _ ->
            "Intelligibility rating is out of bounds."
