module Debate.Arguments.Helpers exposing (..)

import Config.CardFormat exposing (..)
import Config.Colour as T exposing (..)
import Config.Format as O exposing (..)
import Config.StrengthBar exposing (..)
import Config.ToolTip exposing (..)
import Debate.Arguments.Inferences.Abortion exposing (..)
import Debate.Arguments.Inferences.Agnosticism exposing (..)
import Debate.Arguments.Inferences.AgriculturalPredation exposing (..)
import Debate.Arguments.Inferences.AnabolicKeto exposing (..)
import Debate.Arguments.Inferences.AnimalRights exposing (..)
import Debate.Arguments.Inferences.AntagonisticPleiotropy exposing (..)
import Debate.Arguments.Inferences.AntiRewilding exposing (..)
import Debate.Arguments.Inferences.AntiVandalism exposing (..)
import Debate.Arguments.Inferences.ApoBCVD exposing (..)
import Debate.Arguments.Inferences.BoobyTrapPagers exposing (..)
import Debate.Arguments.Inferences.CarbsObesity exposing (..)
import Debate.Arguments.Inferences.ColonizingNature exposing (..)
import Debate.Arguments.Inferences.CropDeaths exposing (..)
import Debate.Arguments.Inferences.DairyCowRape exposing (..)
import Debate.Arguments.Inferences.DietaryCholesterol exposing (..)
import Debate.Arguments.Inferences.EfilismPatrolSquad exposing (..)
import Debate.Arguments.Inferences.EpidemiologyCausality exposing (..)
import Debate.Arguments.Inferences.EthicalSlurs exposing (..)
import Debate.Arguments.Inferences.FineTuning exposing (..)
import Debate.Arguments.Inferences.FlatEarthDebunk exposing (..)
import Debate.Arguments.Inferences.FructoseNAFLD exposing (..)
import Debate.Arguments.Inferences.HealthPromotingFoods exposing (..)
import Debate.Arguments.Inferences.HealthSeeker exposing (..)
import Debate.Arguments.Inferences.HealthyChocolate exposing (..)
import Debate.Arguments.Inferences.HealthyDairy exposing (..)
import Debate.Arguments.Inferences.HealthyFattyFish exposing (..)
import Debate.Arguments.Inferences.HealthyFibre exposing (..)
import Debate.Arguments.Inferences.HealthyFood exposing (..)
import Debate.Arguments.Inferences.HealthyPlantFoods exposing (..)
import Debate.Arguments.Inferences.HealthySeedOils exposing (..)
import Debate.Arguments.Inferences.HealthySoy exposing (..)
import Debate.Arguments.Inferences.ImmortalityReductio exposing (..)
import Debate.Arguments.Inferences.Malondialdehyde exposing (..)
import Debate.Arguments.Inferences.OddOrderPredators exposing (..)
import Debate.Arguments.Inferences.Omega3Omega6Ratio exposing (..)
import Debate.Arguments.Inferences.Ostroveganism exposing (..)
import Debate.Arguments.Inferences.PlantBasedCVDReversal exposing (..)
import Debate.Arguments.Inferences.PollinationReductio exposing (..)
import Debate.Arguments.Inferences.PolyphenolReductio exposing (..)
import Debate.Arguments.Inferences.ScratcherPioneers exposing (..)
import Debate.Arguments.Inferences.SodiumCVD exposing (..)
import Debate.Arguments.Inferences.TMAOCausality exposing (..)
import Debate.Arguments.Inferences.Template exposing (argument)
import Debate.Arguments.Inferences.TransPeople exposing (..)
import Debate.Arguments.Inferences.TruncatedMeta exposing (..)
import Debate.Arguments.Inferences.UnhealthyCoconutOil exposing (..)
import Debate.Arguments.Inferences.UnhealthyProcessedMeat exposing (..)
import Debate.Arguments.Inferences.UnhealthyRedMeat exposing (..)
import Debate.Arguments.Inferences.UnhealthySaturatedFat exposing (..)
import Debate.Arguments.Inferences.VeganSocietyReductio exposing (..)
import Debate.Arguments.Types exposing (..)
import Effect exposing (Effect)
import Element as E exposing (..)
import Element.Background as B exposing (..)
import Element.Border as D exposing (..)
import Element.Events as V exposing (..)
import Element.Font as F exposing (..)
import Html exposing (div, hr)
import Html.Attributes as H exposing (style, title, wrap)
import Json.Decode exposing (field)
import Layouts
import Page exposing (Page)
import Route exposing (Route)
import Shared
import View exposing (View)


argumentMaker : Argument -> Element msg
argumentMaker argument =
    row
        topLevelBox
        [ cardImageMaker (argumentImage argument) ""
        , cardMaker
            [ cardTitleMaker argument.argumentTitle
            , cardFormatter
                [ cardContentSpacing
                    [ column
                        fieldSpacer
                        [ propositionMakerDesktop argument
                        , reductioMakerDesktop argument
                        , summaryMakerDesktop argument
                        , strengthMakerDesktop argument
                        , tableMaker argument
                        , desktopFormalizationMaker argument
                        , proofTreeMaker argument
                        ]
                    ]
                ]
            ]
        ]


argumentMakerMobile : Argument -> Element msg
argumentMakerMobile argument =
    row
        topLevelBox
        [ column [] []
        , cardMaker
            [ cardTitleMaker argument.argumentTitle
            , cardFormatter
                [ cardContentSpacing
                    [ column
                        fieldSpacer
                        [ row [ spacing 10, E.width fill ]
                            [ cardImageMakerMobile (argumentImage argument) ""
                            , column [ E.width fill ]
                                [ propositionMakerMobile argument
                                ]
                            ]
                        , reductioMakerMobile argument
                        , summaryMakerMobile argument
                        , strengthMakerMobile argument
                        , tableMaker argument
                        , mobileFormalizationMaker argument
                        , proofTreeMaker argument
                        ]
                    ]
                ]
            ]
        ]


argumentImage : Argument -> { src : String, description : String }
argumentImage argument =
    { src = "arguments/" ++ argument.argumentImage ++ ".png"
    , description = argument.argumentTitle
    }


infoSpacing =
    E.width <| px 100


propositionMakerDesktop : Argument -> Element msg
propositionMakerDesktop argument =
    row []
        [ propositionMaker
        , propositionTitleMaker argument
        ]


propositionMakerMobile : Argument -> Element msg
propositionMakerMobile argument =
    column []
        [ propositionMaker
        , propositionTitleMaker argument
        ]


propositionMaker : Element msg
propositionMaker =
    column
        [ E.alignTop, E.alignLeft ]
        [ paragraph
            (paragraphBoldFormat
                ++ [ F.size 18
                   , infoSpacing
                   ]
            )
            [ el
                [ tooltip
                    "A proposition is a declarative statement that can be evaluated as either true or false, and which serves as the basis for debate."
                ]
                (text "Proposition:")
                |> el [ F.color colourTheme.textLightOrange ]
            ]
        ]


propositionTitleMaker : Argument -> Element msg
propositionTitleMaker argument =
    column
        [ E.width fill, E.alignLeft ]
        [ paragraph (paragraphBoldFormat ++ [ F.size 18 ])
            [ text argument.propositionTitle
                |> el
                    [ F.color colourTheme.textLightGrey
                    , F.regular
                    , F.size 16
                    ]
            ]
        ]


reductioMakerDesktop : Argument -> Element msg
reductioMakerDesktop argument =
    case argument.propositionReductio of
        "" ->
            none

        reductio ->
            row
                []
                [ reductioMaker
                , reductioMakerTitle reductio
                ]


reductioMakerMobile : Argument -> Element msg
reductioMakerMobile argument =
    case argument.propositionReductio of
        "" ->
            none

        reductio ->
            row
                []
                [ reductioMaker
                , reductioMakerTitle reductio
                ]


reductioMaker : Element msg
reductioMaker =
    column
        [ E.alignTop, E.alignLeft ]
        [ paragraph
            (paragraphBoldFormat
                ++ [ F.size 18
                   , infoSpacing
                   ]
            )
            [ el
                [ tooltip
                    "This is the position from which the reductio ad absurdum is derived."
                ]
                (text "Reductio:")
                |> el [ F.color colourTheme.textLightOrange ]
            ]
        ]


reductioMakerTitle : String -> Element msg
reductioMakerTitle reductio =
    column [ E.width fill, E.alignLeft ]
        [ paragraph
            (paragraphFormat
                ++ [ F.size 16
                   , spacing 3
                   ]
            )
            [ text reductio ]
        ]


summaryMakerDesktop : Argument -> Element msg
summaryMakerDesktop argument =
    row []
        [ summaryMaker
        , summaryMakerTitle argument
        ]


summaryMakerMobile : Argument -> Element msg
summaryMakerMobile argument =
    column []
        [ summaryMaker
        , summaryMakerTitle argument
        ]


summaryMaker : Element msg
summaryMaker =
    column
        [ E.alignTop
        , E.alignLeft
        ]
        [ paragraph
            (paragraphBoldFormat
                ++ [ F.size 18
                   , infoSpacing
                   ]
            )
            [ el
                [ tooltip
                    "The following information provides additional context and insight into the reasoning behind the argument."
                ]
                (text "Summary:")
                |> el [ F.color colourTheme.textLightOrange ]
            ]
        ]


summaryMakerTitle : Argument -> Element msg
summaryMakerTitle argument =
    column
        [ E.width fill
        , E.alignLeft
        ]
        [ paragraph
            (paragraphBoldFormat
                ++ [ F.size 18
                   , spacing 3
                   ]
            )
            [ text argument.propositionSummary
                |> el
                    [ F.color colourTheme.textLightGrey
                    , F.regular
                    , F.size 16
                    ]
            ]
        ]


strengthMakerDesktop : Argument -> Element msg
strengthMakerDesktop argument =
    row [ E.width fill ]
        [ strengthMaker
        , strengthMakerBar argument
        ]


strengthMakerMobile : Argument -> Element msg
strengthMakerMobile argument =
    column [ E.width fill ]
        [ strengthMaker
        , strengthMakerBar argument
        ]


strengthMaker : Element msg
strengthMaker =
    column
        [ E.alignTop
        , E.alignLeft
        ]
        [ paragraph
            (paragraphBoldFormat
                ++ [ F.size 18
                   , E.width fill
                   ]
            )
            [ el
                [ tooltip
                    "This represents my confidence in the soundness of the argument."
                ]
                (el
                    [ paddingEach
                        { top = 0
                        , right = 10
                        , bottom = 0
                        , left = 0
                        }
                    ]
                 <|
                    text "Confidence:"
                )
                |> el [ F.color colourTheme.textLightOrange ]
            ]
        ]


strengthMakerBar : Argument -> Element msg
strengthMakerBar argument =
    barPadding
        [ barMaker getConfidenceTooltip argument.argumentCertainty ]


getConfidenceTooltip : Int -> String
getConfidenceTooltip num =
    case num of
        0 ->
            "Extremely low. Speculative reasoning."

        1 ->
            "Very low. Extremely weak reasoning."

        2 ->
            "Low. Weak reasoning."

        3 ->
            "Kinda low. Somewhat weak reasoning."

        4 ->
            "Below average. More weak than strong."

        5 ->
            "Moderate. OK reasoning."

        6 ->
            "Above average. More strong than weak."

        7 ->
            "Kinda high. Somewhat strong reasoning."

        8 ->
            "High. Robust reasoning."

        9 ->
            "Very high. Extremely robust reasoning."

        10 ->
            "Extremely high. Air tight reasoning."

        _ ->
            "Confidence level out of expected range."


tableMaker : Argument -> Element msg
tableMaker argument =
    column
        [ centerX
        , E.width fill
        ]
        [ wrappedRow
            (paragraphBoldFormat
                ++ [ E.alignLeft
                   , E.width fill
                   , htmlAttribute <| H.style "position" "relative"
                   ]
            )
            [ E.table
                [ spacing 0
                , D.rounded 10
                , D.width 2
                , D.color colourTheme.textDarkGrey
                , clip
                ]
                { data = argument.definitionTable
                , columns =
                    [ { header =
                            el
                                [ F.bold
                                , D.widthEach
                                    { bottom = 1
                                    , top = 1
                                    , left = 1
                                    , right = 1
                                    }
                                , D.color colourTheme.textDarkGrey
                                , padding 8
                                , E.width fill
                                ]
                                (text "Definiendum")
                                |> el [ F.color colourTheme.textLightOrange ]
                      , width = fill |> maximum 50
                      , view =
                            \definition ->
                                row
                                    [ F.color colourTheme.textLightOrange
                                    , F.bold
                                    , D.widthEach
                                        { bottom = 1
                                        , top = 0
                                        , left = 1
                                        , right = 1
                                        }
                                    , D.color colourTheme.textDarkGrey
                                    , padding 8
                                    , E.height fill
                                    ]
                                    [ row [ centerX ]
                                        [ paragraph [] [ text definition.definiendum ]
                                        ]
                                    ]
                      }
                    , { header =
                            el
                                [ F.bold
                                , D.widthEach
                                    { bottom = 1
                                    , top = 1
                                    , left = 0
                                    , right = 1
                                    }
                                , D.color colourTheme.textDarkGrey
                                , padding 8
                                , E.width fill
                                ]
                                (text "Definiens")
                                |> el [ F.color colourTheme.textLightOrange ]
                      , width = fill
                      , view =
                            \definition ->
                                paragraph
                                    [ F.color colourTheme.textLightGrey
                                    , F.regular
                                    , D.widthEach
                                        { bottom = 1
                                        , top = 0
                                        , left = 0
                                        , right = 1
                                        }
                                    , D.color colourTheme.textDarkGrey
                                    , padding 8
                                    , E.height fill
                                    ]
                                    [ row []
                                        [ paragraph [] [ text definition.definiens ]
                                        ]
                                    ]
                      }
                    ]
                }
            ]
        ]


desktopFormalizationMaker : Argument -> Element msg
desktopFormalizationMaker argument =
    let
        argumentFormatting : List (Attribute msg)
        argumentFormatting =
            [ centerX
            , F.center
            , spacing 3
            , E.width fill
            ]
    in
    column
        [ centerX
        , E.width fill
        , spacing 10
        ]
        (List.indexedMap
            (\index argumentEntry ->
                column
                    (paragraphFormat
                        ++ [ spacing 3
                           , centerX
                           , E.width fill
                           , paddingXY 40 3
                           ]
                    )
                    (List.indexedMap
                        (\premiseIndex premiseWithNotation ->
                            column
                                (argumentFormatting ++ [ E.width fill ])
                                [ paragraph paragraphHightlightedBoldText
                                    [ text ("P" ++ String.fromInt (premiseIndex + 1) ++ ")")
                                    , text premiseWithNotation.premise
                                        |> el
                                            [ F.color colourTheme.textLightGrey
                                            , F.regular
                                            , E.width fill
                                            , paddingEach
                                                { top = 0
                                                , right = 0
                                                , bottom = 0
                                                , left = 5
                                                }
                                            ]
                                    ]
                                , paragraph
                                    (argumentFormatting
                                        ++ [ F.color colourTheme.textLightOrange
                                           , F.bold
                                           ]
                                    )
                                    [ text ("(" ++ premiseWithNotation.notation ++ ")")
                                    ]
                                ]
                        )
                        argumentEntry.premises
                        ++ [ column
                                (argumentFormatting ++ [ E.width fill ])
                                [ paragraph paragraphHightlightedBoldText
                                    [ text "C)"
                                    , text argumentEntry.conclusion
                                        |> el
                                            [ F.color colourTheme.textLightGrey
                                            , F.regular
                                            , E.width fill
                                            , paddingEach
                                                { top = 0
                                                , right = 0
                                                , bottom = 0
                                                , left = 5
                                                }
                                            ]
                                    ]
                                , paragraph
                                    (argumentFormatting
                                        ++ [ F.color colourTheme.textLightOrange
                                           , F.bold
                                           ]
                                    )
                                    [ text ("(∴" ++ argumentEntry.conclusionNotation ++ ")")
                                    ]
                                ]
                           ]
                    )
            )
            argument.argumentFormalization
        )


mobileFormalizationMaker : Argument -> Element msg
mobileFormalizationMaker argument =
    let
        argumentFormatting : List (Attribute msg)
        argumentFormatting =
            [ centerX
            , F.center
            , spacing 3
            , E.width fill
            ]
    in
    column
        [ centerX
        , E.width fill
        , spacing 10
        ]
        (List.indexedMap
            (\index argumentEntry ->
                column
                    (paragraphFormat
                        ++ [ spacing 3
                           , centerX
                           , E.width fill
                           , paddingXY 0 3
                           ]
                    )
                    (List.indexedMap
                        (\premiseIndex premiseWithNotation ->
                            column
                                (argumentFormatting ++ [ E.width fill ])
                                [ paragraph paragraphHightlightedBoldText
                                    [ text ("P" ++ String.fromInt (premiseIndex + 1) ++ ")")
                                    , text premiseWithNotation.premise
                                        |> el
                                            [ F.color colourTheme.textLightGrey
                                            , F.regular
                                            , E.width fill
                                            , paddingEach
                                                { top = 0
                                                , right = 0
                                                , bottom = 0
                                                , left = 5
                                                }
                                            ]
                                    ]
                                , paragraph
                                    (argumentFormatting
                                        ++ [ F.color colourTheme.textLightOrange
                                           , F.bold
                                           ]
                                    )
                                    [ text ("(" ++ premiseWithNotation.notation ++ ")")
                                    ]
                                ]
                        )
                        argumentEntry.premises
                        ++ [ column
                                (argumentFormatting ++ [ E.width fill ])
                                [ paragraph paragraphHightlightedBoldText
                                    [ text "C)"
                                    , text argumentEntry.conclusion
                                        |> el
                                            [ F.color colourTheme.textLightGrey
                                            , F.regular
                                            , E.width fill
                                            , paddingEach
                                                { top = 0
                                                , right = 0
                                                , bottom = 0
                                                , left = 5
                                                }
                                            ]
                                    ]
                                , paragraph
                                    (argumentFormatting
                                        ++ [ F.color colourTheme.textLightOrange
                                           , F.bold
                                           ]
                                    )
                                    [ text ("(∴" ++ argumentEntry.conclusionNotation ++ ")")
                                    ]
                                ]
                           ]
                    )
            )
            argument.argumentFormalization
        )


proofTreeMaker : Argument -> Element msg
proofTreeMaker argument =
    row
        [ paddingEach
            { top = 10
            , right = 0
            , bottom = 10
            , left = 0
            }
        , centerX
        , E.width fill
        ]
        [ column [ E.alignRight ]
            [ newTabLink
                (paragraphBoldFormat
                    ++ [ F.size 18
                       , F.color colourTheme.textLightGrey
                       , B.color colourTheme.textDarkOrange
                       , paddingEach
                            { top = 6
                            , bottom = 2
                            , left = 12
                            , right = 12
                            }
                       , D.rounded 10
                       , F.center
                       , E.alignRight
                       , transitionStyle
                       , O.glow
                       , mouseOver
                            [ B.color colourTheme.textDeepDarkOrange
                            , F.color colourTheme.textDarkGrey
                            ]
                       ]
                )
                { url = argument.proofLink
                , label = text "Proof Tree"
                }
            ]
        ]


argumentList : List Argument
argumentList =
    [ argumentApoBCVD
    , argumentAnabolicKeto
    , argumentAntagonisticPleiotropy
    , argumentCarbsObesity
    , argumentDietaryCholesterol
    , argumentFructoseNAFLD
    , argumentHealthPromotingFoods
    , argumentHealthSeeker
    , argumentHealthyChocolate
    , argumentHealthyDairy
    , argumentHealthyFattyFish
    , argumentHealthyFibre
    , argumentHealthyFood
    , argumentHealthyPlantFoods
    , argumentHealthySeedOils
    , argumentHealthySoy
    , argumentMalondialdehyde
    , argumentOmega3Omega6Ratio
    , argumentPlantBasedCVDReversal
    , argumentPolyphenolReductio
    , argumentSodiumCVD
    , argumentTMAOCausality
    , argumentUnhealthyCoconutOil
    , argumentUnhealthyProcessedMeat
    , argumentUnhealthyRedMeat
    , argumentUnhealthySaturatedFat
    , argumentAbortion
    , argumentAgnosticism
    , argumentAgriculturalPredation
    , argumentAnimalRights
    , argumentAntiRewilding
    , argumentAntiVandalism
    , argumentColonizingNature
    , argumentCropDeaths
    , argumentDairyCowRape
    , argumentEfilismPatrolSquad
    , argumentEthicalSlurs
    , argumentFineTuning
    , argumentImmortalityReductio
    , argumentOddOrderPredators
    , argumentOstroveganism
    , argumentPollinationReductio
    , argumentScratcherPioneers
    , argumentTransPeople
    , argumentVeganSocietyReductio
    , argumentBoobyTrapPagers
    , argumentEpidemiologyCausality
    , argumentFlatEarthDebunk
    , argumentTruncatedMeta
    ]


argumentListNumber : Int
argumentListNumber =
    List.length argumentList
