module Pages.Contact.Criticism exposing (Model, Msg, page)

import Config.Data.Identity exposing (pageNames)
import Config.Helpers.Cards.Inner.Text
    exposing
        ( detailBodyMaker
        , detailFormat
        , detailHeader
        , detailTitleMaker
        , divider
        , numberedListItem
        )
import Config.Helpers.Cards.Outer.Helpers exposing (cardMaker)
import Config.Helpers.Cards.Outer.Types as C exposing (Cardable(..))
import Config.Helpers.Response
    exposing
        ( pageList
        , topLevelContainer
        )
import Config.Helpers.Viewport exposing (resetViewport)
import Config.Pages.Contact.Records exposing (contactCriticism)
import Config.Pages.Contact.Types exposing (Contact)
import Config.Style.Colour.Helpers
    exposing
        ( ThemeColor(..)
        , colourTheme
        )
import Config.Style.Fonts exposing (defaultFontSize)
import Config.Style.Transitions
    exposing
        ( hoverFontDarkOrange
        , transitionStyleFast
        )
import Effect exposing (Effect)
import Element as E
    exposing
        ( Attr
        , Attribute
        , Device
        , DeviceClass(..)
        , Element
        , Orientation(..)
        , alignRight
        , alignTop
        , centerX
        , column
        , el
        , fill
        , newTabLink
        , paddingEach
        , paragraph
        , px
        , row
        , spacing
        , text
        , width
        )
import Element.Background as B exposing (color)
import Element.Border as D exposing (rounded)
import Element.Font as F
    exposing
        ( alignLeft
        , color
        )
import Html.Attributes as H exposing (style)
import Layouts
import Page exposing (Page)
import Route exposing (Route)
import Shared exposing (Model)
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.map
        (\_ -> NoOp)
        (Effect.sendCmd resetViewport)
    )



-- 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 = pageNames.pageContact ++ " (criticism)"
    , attributes = []
    , element = contactContainer shared.device
    }


contactContainer : Device -> Element msg
contactContainer device =
    topLevelContainer (contactList device)


contactList : Device -> Element msg
contactList device =
    column
        (case ( device.class, device.orientation ) of
            _ ->
                pageList device
        )
    <|
        List.concat
            [ List.map
                (\contact ->
                    cardMaker device (C.ContactPage contact) (contentList device contact)
                )
                [ contactCriticism ]
            ]


contentList : Device -> Contact -> List (Element msg)
contentList device contact =
    [ instructionBody device ]


instructionBody : Device -> Element msg
instructionBody device =
    column
        [ spacing 20
        , paddingEach
            { top = 10
            , bottom = 0
            , left = 0
            , right = 0
            }
        ]
        [ detailFormat paragraph
            [ detailBodyMaker TextLightGrey (text "The following terms may seem unreasonable to some, but after years on a large platform, I've learned the importance of filtering the criticisms I receive. Most feedback I receive is just vague gesturing and lacks substance, making some sort of quality filter essential. Thank you for your patience and understanding.") ]
        , detailFormat paragraph
            [ detailBodyMaker TextLightGrey (text "Please keep in mind that any failure to comply with the following terms and conditions will forfeit your access to my time and attention. I ask that you respect my time and read these terms carefully. You are the one requesting an audience with me, and my time is mine to donate as I see fit. If you wish to submit your criticisms, you must do so on my terms, following the rules and conditions that streamline the process for me.") ]
        , el
            [ centerX
            , width fill
            ]
          <|
            detailHeader (String.toUpper "Terms and Conditions")
        , column [ spacing 10 ] <|
            List.indexedMap
                (\index term ->
                    row
                        [ spacing 10
                        , width fill
                        ]
                        [ column
                            [ width <| px 15
                            , alignTop
                            ]
                            [ numberedListItem TextLightGrey (index + 1) ]
                        , el
                            [ spacing 10
                            , width fill
                            , alignRight
                            ]
                          <|
                            paragraph
                                [ width fill
                                , defaultFontSize
                                , F.alignLeft
                                ]
                                term
                        ]
                )
                (termsAndConditions device)
        , el [ centerX, width fill ] <|
            detailHeader (String.toUpper "Additional Clarifications")
        , detailFormat paragraph
            [ detailBodyMaker TextLightGrey (text "• ")
            , detailBodyMaker TextLightGrey (text "You are only allowed to post one criticism at a time in the ")
            , newTabLink []
                { url = "https://discord.com/channels/692563032546476062/1301247050796634182"
                , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "🔎┃criticism")
                }
            , detailBodyMaker TextLightGrey (text " channel. You may post an additional criticism only after the previous one has been addressed and resolved to my satisfaction. This policy aims to reduce spamming, rambling, and Gish galloping, and to encourage linear discourse.")
            ]
        , detailFormat paragraph
            [ detailBodyMaker TextLightGrey
                (text "• ")
            , detailBodyMaker TextLightGrey
                (text "You may or may not be asked to voice chat about your criticism. While your willingness to engage in voice chat is a necessary condition for submitting your criticism, it does not guarantee that a voice chat will be requested. If your initial criticism is clear and I agree with it, then no voice chat will be required.")
            ]
        , detailFormat paragraph
            [ detailBodyMaker TextLightGrey (text "• ")
            , detailBodyMaker TextLightGrey (text "You may or may not be asked to have your criticism formalized. While your willingness to have your criticism formalized is a necessary condition for submitting your criticism, it does not guarantee that a formalization will be requested. If your initial criticism is clear and I agree with it, then no formalization will be required.")
            ]
        , detailFormat paragraph
            [ detailBodyMaker TextLightGrey (text "• ")
            , detailBodyMaker TextLightGrey (text "If I find it necessary to access a text-based channel (for simple clarifying questions, for example), then either I or a moderator will open a new thread in the ")
            , newTabLink []
                { url = "https://discord.com/channels/692563032546476062/1301247050796634182"
                , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "🔎┃criticism")
                }
            , detailBodyMaker TextLightGrey (text " channel. There we can then engage in a text-based discussion and/or ping other users if needed.")
            ]
        , detailFormat paragraph
            [ detailBodyMaker TextLightGrey (text "• ")
            , detailBodyMaker TextLightGrey (text "I will only request that your criticism be formalized if I do not understand it and we have exhausted all other reasonable means of clarification. If formalization is requested, you will not need to do it yourself, as I recognize that not everyone understands formal logic. If formalization is requested and I am unavailable to assist you, you may ping the @Logic role, and another user may help you.")
            ]
        ]


darkFormat : Attr decorative msg
darkFormat =
    F.color colourTheme.textDarkGrey


orangeFormat : List (Attr () msg)
orangeFormat =
    [ F.color colourTheme.textLightOrange
    , hoverFontDarkOrange
    , transitionStyleFast
    ]


rounding : Attribute msg
rounding =
    D.rounded 10


termsAndConditions : Device -> List (List (Element msg))
termsAndConditions device =
    let
        backGroundCase : Attr decorative msg
        backGroundCase =
            case ( device.class, device.orientation ) of
                ( Phone, Portrait ) ->
                    B.color colourTheme.backgroundDarkGrey

                ( Tablet, Portrait ) ->
                    B.color colourTheme.backgroundDarkGrey

                _ ->
                    B.color colourTheme.backgroundLightGrey
    in
    [ [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You will register an account with ")
                , newTabLink []
                    { url = "https://discord.com/login"
                    , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "Discord")
                    }
                , detailBodyMaker TextLightGrey (text " (if you haven't already).")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You will join the ")
                , newTabLink []
                    { url = "https://discord.com/invite/VzU8yCvYX8"
                    , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "upRootNutrition")
                    }
                , detailBodyMaker TextLightGrey (text " Discord Server (if you haven't already).")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You will click the \"😃\" emoji to enter the server (if you haven't already).")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You will locate the ")
                , newTabLink []
                    { url = "https://discord.com/channels/692563032546476062/826225570219687956"
                    , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "💻┃general")
                    }
                , detailBodyMaker TextLightGrey (text " channel in General category.")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ paragraph [ F.alignLeft ]
                    [ detailBodyMaker TextLightGrey (text "You will post your criticism in the ")
                    , newTabLink []
                        { url = "https://discord.com/channels/692563032546476062/826225570219687956"
                        , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "💻┃general")
                        }
                    , detailBodyMaker TextLightGrey (text " channel with this exact format:")
                    ]
                ]
            ]
      , column
            [ paddingEach
                { top = 10
                , bottom = 10
                , left = 0
                , right = 0
                }
            , alignLeft
            ]
            [ paragraph
                [ paddingEach
                    { top = 15
                    , bottom = 15
                    , left = 20
                    , right = 20
                    }
                , backGroundCase
                , rounding
                , width fill
                , spacing 8
                ]
                [ detailBodyMaker TextLightGrey (text "\"@Moderators I have a criticism for Nick.\"")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You will then receive the @Critic role.")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You will locate the ")
                , newTabLink []
                    { url = "https://discord.com/channels/692563032546476062/1301247050796634182"
                    , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "🔎┃criticism")
                    }
                , detailBodyMaker TextLightGrey (text " channel in the General category.")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ paragraph [ F.alignLeft ]
                    [ detailBodyMaker TextLightGrey (text "You will post your criticism in the ")
                    , newTabLink []
                        { url = "https://discord.com/channels/692563032546476062/1301247050796634182"
                        , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "🔎┃criticism")
                        }
                    , detailBodyMaker TextLightGrey (text " channel with this exact format:")
                    ]
                ]
            ]
      , column
            [ paddingEach
                { top = 10
                , bottom = 10
                , left = 0
                , right = 0
                }
            , alignLeft
            ]
            [ column
                [ paddingEach
                    { top = 15
                    , bottom = 15
                    , left = 20
                    , right = 20
                    }
                , backGroundCase
                , rounding
                , width fill
                , spacing 8
                ]
                [ detailBodyMaker TextLightGrey (text "\"Hello, <@191027366640877568>. I have a criticism for you.")
                , row [ alignLeft ]
                    [ paragraph []
                        [ detailBodyMaker TextLightGrey (text "Proposition: ")
                        , el [ darkFormat ] <| detailBodyMaker TextDarkGrey (text "specify the exact proposition you are addressing.")
                        ]
                    ]
                , row [ alignLeft ]
                    [ paragraph []
                        [ detailBodyMaker TextLightGrey (text "Link: ")
                        , el [ darkFormat ] <| detailBodyMaker TextDarkGrey (text "provide a url to the claim, with a timestamp if applicable.")
                        ]
                    ]
                , row [ alignLeft ]
                    [ paragraph []
                        [ detailBodyMaker TextLightGrey (text "Reason for Error: ")
                        , el [ darkFormat ] <| detailBodyMaker TextDarkGrey (text "explain exactly why you believe this claim is in error.")
                        ]
                    ]
                , row [ alignLeft ]
                    [ paragraph []
                        [ detailBodyMaker TextLightGrey (text "Suggested Correction (if any): ")
                        , el [ darkFormat ] <| detailBodyMaker TextDarkGrey (text "provide the corrected information or perspective.")
                        ]
                    ]
                , row [ alignLeft ]
                    [ paragraph []
                        [ detailBodyMaker TextLightGrey (text "Additional Comments: ")
                        , el [ darkFormat ] <| detailBodyMaker TextDarkGrey (text "any other relevant thoughts or context.")
                        , detailBodyMaker TextLightGrey (text "\"")
                        ]
                    ]
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph <|
                [ detailBodyMaker TextLightGrey (text "You will not post additional criticisms until the last one has been resolved.")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You will not post anything other than criticisms in the ")
                , newTabLink []
                    { url = "https://discord.com/channels/692563032546476062/1301247050796634182"
                    , label = el orangeFormat <| detailBodyMaker TextLightOrange (text "🔎┃criticism")
                    }
                , detailBodyMaker TextLightGrey (text " channel.")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You must be willing to converse over voice chat.")
                ]
            ]
      ]
    , [ row []
            [ detailFormat paragraph
                [ detailBodyMaker TextLightGrey (text "You must be willing to have your criticism formalized.")
                ]
            ]
      ]
    ]
