import ImageProvider, { PanoramaView, ProvidedImage } from "./ImageProvider"
import BaseUIElement from "../../UI/BaseUIElement"
import { Utils } from "../../Utils"
import { LicenseInfo } from "./LicenseInfo"
import Wikimedia from "../Web/Wikimedia"
import SvelteUIElement from "../../UI/Base/SvelteUIElement"
import Wikimedia_commons_white from "../../assets/svg/Wikimedia_commons_white.svelte"
import { Feature, Point } from "geojson"

/**
 * This module provides endpoints for wikimedia and others
 */
export class WikimediaImageProvider extends ImageProvider {
    public static readonly singleton = new WikimediaImageProvider()
    public static readonly apiUrls = [
        "https://commons.wikimedia.org/wiki/",
        "https://upload.wikimedia.org",
    ]
    public static readonly commonsPrefixes = [...WikimediaImageProvider.apiUrls, "File:"]
    private readonly commons_key = "wikimedia_commons"
    public readonly defaultKeyPrefixes = [this.commons_key, "image"]
    public readonly name = "Wikimedia"

    private constructor() {
        super()
    }

    /**
     * Replaces (multiple) spaces to underscores.
     * Will remove a "File:"-prefix
     *
     * WikimediaImageProvider.makeCanonical("Some File.jpg") // => "Some_File.jpg"
     *
     * // Double spaces
     * WikimediaImageProvider.makeCanonical("Some      File.jpg") // => "Some_File.jpg"
     * WikimediaImageProvider.makeCanonical("Some+File.jpg") // => "Some+File.jpg"
     *
     * // Remove File: prefix
     */
    private static makeCanonical(filename: string): string {
        if (filename.startsWith("File:")) {
            filename = filename.substring(5)
        }
        return filename.trim().replace(/\s+/g, "_")
    }

    /**
     *
     * WikimediaImageProvider.extractFileName("https://commons.wikimedia.org/wiki/File:Somefile.jpg") // => "Somefile.jpg"
     * WikimediaImageProvider.extractFileName("https://commons.wikimedia.org/wiki/File:S%C3%A8vres%20-%20square_madame_de_Pompadour_-_bo%C3%AEte_%C3%A0_livres.jpg?uselang=en") // => "Sèvres_-_square_madame_de_Pompadour_-_boîte_à_livres.jpg"
     */
    private static extractFileName(url: string) {
        if (!url.startsWith("http")) {
            return url
        }
        const path = decodeURIComponent(new URL(url).pathname)
        return WikimediaImageProvider.makeCanonical(path.substring(path.lastIndexOf("/") + 1))
    }

    private static PrepareUrl(value: string, useHd = false): string {
        if (value.toLowerCase().startsWith("https://commons.wikimedia.org/wiki/")) {
            return value
        }
        const baseUrl = `https://commons.wikimedia.org/wiki/Special:FilePath/${encodeURIComponent(
            value
        )}`
        if (useHd) {
            return baseUrl
        }
        return baseUrl + `?width=500&height=400`
    }

    private static startsWithCommonsPrefix(value: string): boolean {
        return WikimediaImageProvider.commonsPrefixes.some((prefix) => value.startsWith(prefix))
    }

    private static removeCommonsPrefix(value: string): string {
        if (value.startsWith("https://upload.wikimedia.org/")) {
            value = value.substring(value.lastIndexOf("/") + 1)
            value = decodeURIComponent(value)
            if (!value.startsWith("File:")) {
                value = "File:" + value
            }
            return value
        }

        for (const prefix of WikimediaImageProvider.commonsPrefixes) {
            if (value.startsWith(prefix)) {
                let part = value.substr(prefix.length)
                if (prefix.startsWith("http")) {
                    part = decodeURIComponent(part)
                }
                return part
            }
        }
        return value
    }

    apiUrls(): string[] {
        return WikimediaImageProvider.apiUrls
    }

    SourceIcon(): BaseUIElement {
        return new SvelteUIElement(Wikimedia_commons_white).SetStyle("width:2em;height: 2em")
    }

    public PrepUrl(value: NonNullable<string>): ProvidedImage
    public PrepUrl(value: undefined): undefined

    public PrepUrl(value: string): ProvidedImage
    public PrepUrl(value: string | undefined): ProvidedImage | undefined {
        if (value === undefined) {
            return undefined
        }
        value = WikimediaImageProvider.removeCommonsPrefix(value)

        if (value.startsWith("File:")) {
            return this.UrlForImage(value)
        }

        // We do a last effort and assume this is a file
        return this.UrlForImage("File:" + value)
    }

    /**
     *
     * @param key
     * @param value
     * @constructor
     *
     * const result = await WikimediaImageProvider.singleton.ExtractUrls("wikimedia_commons", "File:Sèvres_-_square_madame_de_Pompadour_-_boîte_à_livres.jpg")
     * result[0].url_hd // => "https://commons.wikimedia.org/wiki/Special:FilePath/File%3AS%C3%A8vres_-_square_madame_de_Pompadour_-_bo%C3%AEte_%C3%A0_livres.jpg"
     */
    public async ExtractUrls(key: string, value: string): undefined | Promise<ProvidedImage[]> {
        const hasCommonsPrefix = WikimediaImageProvider.startsWithCommonsPrefix(value)
        if (key !== undefined && key !== this.commons_key && !hasCommonsPrefix) {
            return undefined
        }

        value = WikimediaImageProvider.removeCommonsPrefix(value)
        if (value.startsWith("Category:")) {
            const urls = await Wikimedia.GetCategoryContents(value)
            return urls
                .filter((url) => url.startsWith("File:"))
                .map((image) => this.UrlForImage(image))
        }
        if (value.startsWith("File:")) {
            return [this.UrlForImage(value, key, value)]
        }
        if (value.startsWith("http")) {
            // Probably an error
            return undefined
        }
        // We do a last effort and assume this is a file
        return [this.UrlForImage("File:" + value, key, value)]
    }

    public async DownloadAttribution(img: { id: string }): Promise<LicenseInfo> {
        const filename = "File:" + WikimediaImageProvider.extractFileName(img.id)
        console.log("Downloading attribution for", filename, img.id)
        if (filename === "") {
            return undefined
        }

        const url =
            "https://en.wikipedia.org/w/" +
            "api.php?action=query&prop=imageinfo&iiprop=extmetadata&" +
            "titles=" +
            filename +
            "&format=json&origin=*"
        const data = await Utils.downloadJsonCached<{
            query: { pages: { title: string; imageinfo: { extmetadata }[] }[] }
        }>(url, 365 * 24 * 60 * 60)
        const licenseInfo = new LicenseInfo()
        const pages = data.query.pages
        /*jup, a literal "-1" in an object, not a list!*/
        let pageInfo = pages["-1"]
        if (Array.isArray(pages)) {
            pageInfo = pages.at(-1)
        }
        if (pageInfo === undefined) {
            console.warn("No attribution found for wikimedia image:", filename)
            return undefined
        }

        const license = (pageInfo.imageinfo ?? [])[0]?.extmetadata
        if (license === undefined) {
            console.warn(
                "The file",
                filename,
                "has no usable metedata or license attached... Please fix the license info file yourself!"
            )
            return undefined
        }

        let title = WikimediaImageProvider.makeCanonical(pageInfo.title)
        if (title.endsWith(".jpg") || title.endsWith(".png")) {
            title = title.substring(0, title.length - 4)
        }

        licenseInfo.title = title
        licenseInfo.artist = license.Artist?.value
        licenseInfo.license = license.License?.value
        licenseInfo.copyrighted = license.Copyrighted?.value
        licenseInfo.attributionRequired = license.AttributionRequired?.value
        licenseInfo.usageTerms = license.UsageTerms?.value
        licenseInfo.licenseShortName = license.LicenseShortName?.value
        licenseInfo.credit = license.Credit?.value
        licenseInfo.description = license.ImageDescription?.value
        licenseInfo.informationLocation = new URL("https://en.wikipedia.org/wiki/" + pageInfo.title)
        return licenseInfo
    }

    private UrlForImage(image: string, key?: string, value?: string): ProvidedImage {
        image = "File:" + WikimediaImageProvider.makeCanonical(image)
        const providedImage: ProvidedImage = {
            url: WikimediaImageProvider.PrepareUrl(image),
            url_hd: WikimediaImageProvider.PrepareUrl(image, true),
            key: undefined,
            provider: this,
            id: image,
            isSpherical: false,
        }
        if(key && value){
            providedImage.originalAttribute = {key, value}
        }
        return providedImage
    }

    getPanoramaInfo(): Promise<Feature<Point, PanoramaView>> | undefined {
        return undefined
    }

    visitUrl(): string | undefined {
        return undefined
    }
}
