import {TagRenderingOptions} from "../TagRenderingOptions";
import {LayerDefinition, Preset} from "../LayerDefinition";
import {Layout} from "../Layout";
import Translation from "../../UI/i18n/Translation";
import {type} from "os";
import Combine from "../../UI/Base/Combine";
import {UIElement} from "../../UI/UIElement";
import {And, Tag, TagsFilter} from "../../Logic/TagsFilter";
import FixedText from "../Questions/FixedText";
import {ImageCarouselWithUploadConstructor} from "../../UI/Image/ImageCarouselWithUpload";


export interface TagRenderingConfigJson {
    // If this key is present, then...
    key?: string,
    // Use this string to render
    render: string,
    // One of string, int, nat, float, pfloat, email, phone. Default: string
    type?: string,
    // If it is not known (and no mapping below matches), this question is asked; a textfield is inserted in the rendering above
    question?: string,
    // If a value is added with the textfield, this extra tag is addded. Optional field
    addExtraTags?: string | string[] | { k: string, v: string }[];
    // Alternatively, these tags are shown if they match - even if the key above is not there
    // If unknown, these become a radio button
    mappings?:
        {
            if: string,
            then: string
        }[]
}

export interface LayerConfigJson {

    id: string;
    icon: string;
    title: TagRenderingConfigJson;
    description: string;
    minzoom: number,
    color: string;
    overpassTags: string | string[] | { k: string, v: string }[];
    presets: [
        {
            // icon: optional. Uses the layer icon by default
            icon?: string;
            // title: optional. Uses the layer title by default
            title?: string;
            // description: optional. Uses the layer description by default
            description?: string;
            // tags: optional list {k:string, v:string}[]
            tags?: string | string[] | { k: string, v: string }[]
        }
    ],
    tagRenderings: TagRenderingConfigJson []
}

export interface LayoutConfigJson {
    name: string;
    title: string;
    description: string;
    language: string;
    layers: LayerConfigJson[],
    startZoom: number;
    startLat: number;
    startLon: number;
    /**
     * Either a URL or a base64 encoded value (which should include 'data:image/svg+xml;base64,'
     */
    icon: string;
}

export class CustomLayoutFromJSON {

    public static exampleLayer: LayerConfigJson = {
        id: "bookcase",
        icon: "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly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NTExLDAgeiIgLz4KICAgIDwvZz4KICA8L2c+Cjwvc3ZnPgo=",
        title: {render: "Bookcase"},
        description: "A small, public cabinet with books. Anyone can leave or take a book",
        minzoom: 12,
        color: "#0000ff",
        overpassTags: "amenity=public_bookcase",
        presets: [
            {
                title: "bookcase"
                // icon: optional. Uses the layer icon by default
                // title: optional. Uses the layer title by default
                // description: optional. Uses the layer description by default
                // tags: optional list {k:string, v:string}[]
            }
        ],
        tagRenderings: [
            {
                // If this key is present, then...
                key: "name",
                // Use this string to render
                render: "{name}",
                // One of string, int, nat, float, pfloat, email, phone. Default: string
                type: "string",
                // If it is not known (and no mapping below matches), this question is asked; a textfield is inserted in the rendering above
                question: "Wat is de naam van dit boekenruilkastje?",
                // If a value is added with the textfield, this extra tag is addded. Optional field
                addExtraTags: [{
                    "k": "fixme",
                    "v": "Added with mapcomplete, to be checked"
                }],
                // Alternatively, these tags are shown if they match - even if the key above is not there
                // If unknown, these become a radio button
                mappings: [
                    {
                        if: "noname=yes",
                        then: "Dit boekenruilkastje heeft geen naam"
                    }
                ]
            }
        ]
    }

    public static exampleLayout: LayoutConfigJson = {
        name: "bookcases",
        title: "Custom Open bookcases map",
        description: "Welcome to a custom layout",
        language: "en",
        layers: [CustomLayoutFromJSON.exampleLayer],
        startZoom: 12,
        startLat: 0,
        startLon: 0,
        icon: "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+CjxzdmcKICAgeG1sbnM6ZGM9Imh0dHA6Ly9wdXJsLm9yZy9kYy9lbGVtZW50cy8xLjEvIgogICB4bWxuczpjYz0iaHR0cDovL2NyZWF0aXZlY29tbW9ucy5vcmcvbnMjIgogICB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiCiAgIHhtbG5zOnN2Zz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciC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TI4MSAtNi45ZS00LDQ0LjMyMDgxIC0xOS4xNTI1Myw5MC43ODYzOCAtNDMuMDcyNiwxMjguMzMyOTkgLTE4LjM4OTQ3LDMwLjkwOTM4IC02MC4zNzUxMSw2Ni40NTIzNiAtMTE4LjIxMjM3LDEwNC40MTYyOCAtNDIuODM2MDcsMjUuNzY4NiAtNjYuNjcxOTYsNTMuMTE5MjYgLTc3LjAzOTY0LDcyLjA5NDYgLTEwLjM2ODYzLDE4Ljk3NjAzIC0xNS41NTI3MSw0My43MjI2NyAtMTUuNTUyMjUsNzQuMjM5OTkgeiIgLz4KICAgICAgPHBhdGgKICAgICAgICAgaWQ9InBhdGgxMTQ3NCIKICAgICAgICAgc3R5bGU9ImZpbGw6IzAwMDAwMDtmaWxsLW9wYWNpdHk6MTtzdHJva2U6bm9uZTtzdHJva2Utd2lkdGg6MztzdHJva2UtbGluZWNhcDpzcXVhcmU7c3Ryb2tlLWxpbmVqb2luOm1pdGVyO3N0cm9rZS1taXRlcmxpbWl0OjQ7c3Ryb2tlLWRhc2hhcnJheTpub25lO3N0cm9rZS1kYXNob2Zmc2V0OjA7c3Ryb2tlLW9wYWNpdHk6MSIKICAgICAgICAgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoMS4xMDYzODMsLTUuNTMxOTE0OSkiCiAgICAgICAgIGQ9Im0gNDgyLjM4Mjk4LDg2OS44MDkwMiBhIDk0LjA0MjU1Nyw3My4wMjEyNzggMCAxIDEgLTE4OC4wODUxMSwwIDk0LjA0MjU1Nyw3My4wMjEyNzggMCAxIDEgMTg4LjA4NTExLDAgeiIgLz4KICAgIDwvZz4KICA8L2c+Cjwvc3ZnPgo="
    }


    public static FromQueryParam(layoutFromBase64: string): Layout {
        if(layoutFromBase64 === "test"){
            console.log(btoa(JSON.stringify(CustomLayoutFromJSON.exampleLayout)));
            return CustomLayoutFromJSON.LayoutFromJSON(CustomLayoutFromJSON.exampleLayout);
        }
        const spec = JSON.parse(atob(layoutFromBase64));
        return CustomLayoutFromJSON.LayoutFromJSON(spec);
    }

    private static TagRenderingFromJson(json: any): TagRenderingOptions {

        if (typeof (json) === "string") {
            return new FixedText(json);
        }

        let freeform = undefined;
        if (json.key !== undefined && json.key !== "" && json.render !== undefined) {
            const type = json.type ?? "text";
            freeform = {
                key: json.key,
                template: json.render.replace("{" + json.key + "}", "$" + type + "$"),
                renderTemplate: json.render,
                extraTags: CustomLayoutFromJSON.TagsFromJson(json.addExtraTags),
            }
        }

        let mappings = undefined;
        if (json.mappings !== undefined) {
            mappings = [];
            for (const mapping of json.mappings) {
                mappings.push({
                    k: new And(CustomLayoutFromJSON.TagsFromJson(mapping.if)), txt: mapping.then
                })
            }
        }

        return new TagRenderingOptions({
            question: json.question,
            freeform: freeform,
            mappings: mappings
        })
    }

    private static PresetFromJson(layout: any, preset: any): Preset {
        const t = CustomLayoutFromJSON.MaybeTranslation;
        const tags = CustomLayoutFromJSON.TagsFromJson;
        return {
            icon: preset.icon ?? layout.icon,
            tags: tags(preset.tags) ?? tags(layout.overpassTags),
            title: t(preset.title) ?? t(layout.title),
            description: t(preset.description) ?? t(layout.description)
        }
    }

    private static StyleFromJson(layout: any, styleJson: any): ((tags) => {
        color: string,
        weight?: number,
        icon: {
            iconUrl: string,
            iconSize: number[],
        },
    }) {
        return (tags) => {
            return {
                color: layout.color,
                weight: 10,
                icon: {
                    iconUrl: layout.icon,
                    iconSize: [40, 40],
                },
            }
        };
    }

    private static TagFromJson(json: string | { k: string, v: string }): Tag {
        if (json === undefined) {
            return undefined;
        }
        console.log(json)
        if (typeof (json) === "string") {
            const kv = json.split("=");
            return new Tag(kv[0].trim(), kv[1].trim());
        }
        return new Tag(json.k.trim(), json.v.trim())
    }

    private static TagsFromJson(json: string | { k: string, v: string }[]): Tag[] {
        if (json === undefined || json === "") {
            return undefined;
        }
        if (typeof (json) === "string") {
            return json.split(",").map(CustomLayoutFromJSON.TagFromJson);
        }
        return json.map(CustomLayoutFromJSON.TagFromJson)
    }

    private static LayerFromJson(json: any): LayerDefinition {
        const t = CustomLayoutFromJSON.MaybeTranslation;
        const tr = CustomLayoutFromJSON.TagRenderingFromJson;
        return new LayerDefinition(
            json.id,
            {
                description: t(json.description),
                name: t(json.title),
                icon: json.icon,
                minzoom: json.minzoom,
                title: tr(json.title) ,
                presets: json.presets.map((preset) => {
                    return CustomLayoutFromJSON.PresetFromJson(json, preset)
                }),
                elementsToShow:
                    [new ImageCarouselWithUploadConstructor()].concat(json.tagRenderings.map(tr)),
                overpassFilter: new And(CustomLayoutFromJSON.TagsFromJson(json.overpassTags)),
                wayHandling: LayerDefinition.WAYHANDLING_CENTER_AND_WAY,
                maxAllowedOverlapPercentage: 0,
                style: CustomLayoutFromJSON.StyleFromJson(json, json.style)
            }
        )
    }


    private static MaybeTranslation(json: any): Translation | string {
        if (json === undefined) {
            return undefined;
        }
        if (typeof (json) === "string") {
            return json;
        }
        return new Translation(json);
    }

    private static LayoutFromJSON(json: any) {
        const t = CustomLayoutFromJSON.MaybeTranslation;
        const layout = new Layout(json.name,
            [json.language],
            t(json.title),
            json.layers.map(CustomLayoutFromJSON.LayerFromJson),
            json.startZoom,
            json.startLat,
            json.startLon,
            new Combine(['<h3>', t(json.title), '</h3><br/>', t(json.description)])
        );
        layout.icon = json.icon;
        return layout;
    }


    public static TagRenderingOptionsFromJson(spec: any): TagRenderingOptions {
        return new TagRenderingOptions(spec);
    }

}