// This file is part of JrUtil and is licenced under the GNU AGPLv3 or later
// (c) 2023 David Koňařík

module GeoReport.HtmlPage

open System.IO
open System.Text.Json
open System.Text.Json.Serialization
open Giraffe.ViewEngine

open JrUtil.GeoData.Common
open GeoReport.Processing

let globalStyle = File.ReadAllText(__SOURCE_DIRECTORY__ + "/style.css")
let globalScript = File.ReadAllText(__SOURCE_DIRECTORY__ + "/script.js")
let vueJsScript = File.ReadAllText(__SOURCE_DIRECTORY__ + "/../thirdparty/vue.global.prod.js")
let leafletStyle = File.ReadAllText(__SOURCE_DIRECTORY__ + "/../thirdparty/leaflet.css")
let leafletScript = File.ReadAllText(__SOURCE_DIRECTORY__ + "/../thirdparty/leaflet.js")

let processForJs stopMatches =
    stopMatches
    |> Array.map (fun (name, matches) -> {|
            name = name
            matches =
                matches
                |> Seq.sortBy (fun m -> matchPriority m.matchType)
                |> Seq.map (fun m ->
                    let wgs84Point = pointEtrs89ExToWgs84 m.point
                    {|
                        matchType = m.matchType
                        lat = wgs84Point.Y
                        lon = wgs84Point.X
                        x = m.point.X
                        y = m.point.Y
                    |})
        |})

let resultPage railMatches otherMatches =
    let serializerOpts =
        JsonFSharpOptions.Default().ToJsonSerializerOptions()
    html [] [
        head [] [
            title [] [str "JrUtil GeoReport results"]
            meta [_charset "UTF-8"]
            style [] [rawText globalStyle]
            // See https://stackoverflow.com/questions/14574471/how-do-browsers-parse-a-script-tag-exactly
            // for some fun HTML reading :)
            script [] [rawText <| vueJsScript.Replace("<script", "< script")]
            style [] [rawText <| leafletStyle]
            script [] [rawText <| leafletScript]
            script [] [
                rawText
                    (sprintf
                        "const railMatches = %s;\nconst otherMatches = %s;"
                        (JsonSerializer.Serialize(processForJs railMatches,
                                                  serializerOpts))
                        (JsonSerializer.Serialize(processForJs otherMatches,
                                                  serializerOpts)))
            ]
            script [] [rawText globalScript]
        ]
        body [] [
            main [] [
                div [_class "tables"] [
                    h1 [] [str "Railway stops"]
                    tag "stops-table" [attr ":stops" "railMatches"] []

                    h1 [] [str "Other stops"]
                    tag "stops-table" [attr ":stops" "otherMatches"] []

                    footer [] [
                        str "Generated by "
                        a [_href "https://gitlab.com/dvdkon/jrutil/tree/master/georeport"]
                          [str "JrUtil GeoReport"]
                    ]
                ]
                div [_id "map"] []
            ]
        ]
    ]
