module Pages.Journal.Digitalprivacy exposing (Model, Msg, page)

import Config.Data.Identity exposing (pageNames)
import Config.Helpers.Articles.Article exposing (contentList)
import Config.Helpers.Articles.Markdown as Markdown
import Config.Helpers.Cards.Outer.Helpers exposing (cardMaker)
import Config.Helpers.Cards.Outer.Types as C exposing (Cardable(..))
import Config.Helpers.Headers.Types exposing (Header)
import Config.Helpers.Response
    exposing
        ( pageList
        , topLevelContainer
        )
import Config.Helpers.Viewport exposing (resetViewport)
import Config.Pages.Blogs.Journal.DigitalPrivacy exposing (articleDigitalPrivacy)
import Effect exposing (Effect)
import Element as E
    exposing
        ( Device
        , Element
        , column
        )
import Layouts
import Page exposing (Page)
import Ports
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
    | MarkdownMsg Markdown.Msg


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

        MarkdownMsg (Markdown.ScrollToElement elementId) ->
            ( model
            , Effect.sendCmd (Ports.scrollToElement elementId)
            )



-- SUBSCRIPTIONS


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



-- VIEW


view : Shared.Model -> Model -> View Msg
view shared model =
    { title = pageNames.pageJournal ++ " (digitalPrivacy)"
    , attributes = []
    , element = articleContainer shared.device
    }


articleContainer : Device -> Element Msg
articleContainer device =
    topLevelContainer (articleList device)


articleList : Device -> Element Msg
articleList device =
    column
        (case ( device.class, device.orientation ) of
            _ ->
                pageList device
        )
    <|
        List.concat
            [ List.map
                (\article ->
                    cardMaker device (C.BlogArticle article) (contentList article [] MarkdownMsg)
                )
                [ articleDigitalPrivacy ]
            ]
