module Pages.Services exposing (Model, Msg, page)

import Browser
import Config.Data.Identity exposing (pageNames)
import Config.Data.ImageFolders as M
    exposing
        ( ImageFolder(..)
        , imagePathMaker
        )
import Config.Helpers.Cards.Inner.Text
    exposing
        ( detailBodyMaker
        , detailFormat
        , detailTitleMaker
        , listItem
        , listMaker
        )
import Config.Helpers.Cards.Outer.Helpers exposing (cardMaker)
import Config.Helpers.Cards.Outer.Types as C exposing (Cardable(..))
import Config.Helpers.Headers.Helpers exposing (headerMaker)
import Config.Helpers.Headers.Records exposing (servicesHeader)
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.Services.Records.DebateAnalysis exposing (servicesDebateAnalysis)
import Config.Pages.Services.Records.DebateCoaching exposing (servicesDebateCoaching)
import Config.Pages.Services.Records.ElmBuilds exposing (servicesElmBuilds)
import Config.Pages.Services.Records.NixBuilds exposing (servicesNixBuilds)
import Config.Pages.Services.Records.NutritionScience exposing (servicesNutritionScience)
import Config.Pages.Services.Types
    exposing
        ( Description
        , Service
        )
import Config.Style.Colour.Helpers as T
    exposing
        ( ThemeColor(..)
        , getThemeColor
        )
import Config.Style.Images
    exposing
        ( ElementSize(..)
        , imageSquareMaker
        )
import Effect exposing (Effect)
import Element as E
    exposing
        ( Device
        , DeviceClass(..)
        , Element
        , Orientation(..)
        , alignLeft
        , column
        , el
        , fill
        , none
        , paddingEach
        , row
        , text
        , width
        )
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.pageServices
    , attributes = []
    , element = servicesContainer shared.device
    }


servicesContainer : Device -> Element msg
servicesContainer device =
    topLevelContainer (servicesList device)


servicesList : Device -> Element msg
servicesList device =
    column
        (case ( device.class, device.orientation ) of
            _ ->
                pageList device
        )
    <|
        List.concat
            [ [ headerMaker (R.Services servicesHeader)
              ]
            , List.map
                (\service ->
                    cardMaker device (C.Service service) (contentList device service)
                )
                [ servicesDebateAnalysis
                , servicesDebateCoaching
                , servicesNutritionScience
                , servicesNixBuilds
                , servicesElmBuilds
                ]
            ]


contentList : Device -> Service -> List (Element msg)
contentList device service =
    let
        image : ElementSize -> Element msg
        image size =
            el
                [ alignLeft
                , paddingEach
                    { top = 0
                    , right = 10
                    , bottom = 0
                    , left = 0
                    }
                ]
            <|
                imageSquareMaker device (imagePathMaker M.ServicePage service.serviceImage) True size
    in
    [ detailFormat row
        [ case ( device.class, device.orientation ) of
            ( Phone, Portrait ) ->
                none

            ( Tablet, Portrait ) ->
                none

            _ ->
                image Big
        , detailFormat column
            [ rateMaker service
            , descriptionMaker
            , offeringMaker service
            ]
        ]
    ]


rateMaker : Service -> Element msg
rateMaker service =
    detailFormat row
        [ detailTitleMaker TextLightOrange "Rate:"
        , el [ width fill ] <| detailBodyMaker TextLightGrey (text service.serviceRate)
        ]


descriptionMaker : Element msg
descriptionMaker =
    detailTitleMaker TextLightOrange "Offerings:"


offeringMaker : Service -> Element msg
offeringMaker service =
    listMaker makeDescription service.serviceDescription


makeDescription : Description -> Element msg
makeDescription description =
    listItem
        TextLightGrey
        description.point
