import Combine from "../UI/Base/Combine";
import BaseUIElement from "../UI/BaseUIElement";
import Translations from "../UI/i18n/Translations";
import {writeFileSync} from "fs";
import {AllKnownLayouts} from "../Customizations/AllKnownLayouts";
import TableOfContents from "../UI/Base/TableOfContents";
import SimpleMetaTaggers, {SimpleMetaTagger} from "../Logic/SimpleMetaTagger";
import ValidatedTextField from "../UI/Input/ValidatedTextField";
import LayoutConfig from "../Models/ThemeConfig/LayoutConfig";
import SpecialVisualizations from "../UI/SpecialVisualizations";
import FeatureSwitchState from "../Logic/State/FeatureSwitchState";
import {ExtraFunctions} from "../Logic/ExtraFunctions";
import Title from "../UI/Base/Title";
import Minimap from "../UI/Base/Minimap";
import {QueryParameters} from "../Logic/Web/QueryParameters";
import QueryParameterDocumentation from "../UI/QueryParameterDocumentation";

function WriteFile(filename, html: BaseUIElement, autogenSource: string[]): void {

    if (html instanceof Combine) {
        const toc = new TableOfContents(html);
        const els = html.getElements();
        html = new Combine(
            [els.shift(),
                toc,
                ...els
            ]
        ).SetClass("flex flex-col")
    }

    let md = new Combine([Translations.W(html),
        "\n\nThis document is autogenerated from " + autogenSource.join(", ")
    ]).AsMarkdown()

    md.replace(/\n\n\n+/g, "\n\n");
    
    writeFileSync(filename, md);
}

console.log("Starting documentation generation...")
AllKnownLayouts.GenOverviewsForSingleLayer((layer, element) => {
    console.log("Exporting ", layer.id)
    WriteFile("./Docs/Layers/" + layer.id + ".md", element, [`assets/layers/${layer.id}/${layer.id}.json`])

})
WriteFile("./Docs/SpecialRenderings.md", SpecialVisualizations.HelpMessage(), ["UI/SpecialVisualisations.ts"])
WriteFile("./Docs/CalculatedTags.md", new Combine([new Title("Metatags", 1),
        SimpleMetaTaggers.HelpText(), ExtraFunctions.HelpText()]).SetClass("flex-col"),
    ["SimpleMetaTagger", "ExtraFunction"])
WriteFile("./Docs/SpecialInputElements.md", ValidatedTextField.HelpText(), ["ValidatedTextField.ts"]);
WriteFile("./Docs/BuiltinLayers.md", AllKnownLayouts.GenLayerOverviewText(), ["AllKnownLayers.ts"])


Minimap.createMiniMap = _ => {
    console.log("Not creating a minimap, it is disabled");
    return undefined
}


const dummyLayout = new LayoutConfig({
    language: ["en"],
    id: "&gt;theme&lt;",
    maintainer: "pietervdvn",
    version: "0",
    title: "<theme>",
    description: "A theme to generate docs with",
    startLat: 0,
    startLon: 0,
    startZoom: 0,
    icon: undefined,
    layers: [
        {
            name: "<layer>",
            id: "&lt;layer&gt;",
            source: {
                osmTags: "id~*"
            },
            mapRendering: null,
        }
    ]

})

new FeatureSwitchState(dummyLayout)

QueryParameters.GetQueryParameter("layer-&lt;layer-id&gt;", "true", "Wether or not the layer with id <layer-id> is shown")

WriteFile("./Docs/URL_Parameters.md", QueryParameterDocumentation.GenerateQueryParameterDocs(), ["QueryParameters"])

console.log("Generated docs")

