import {Configurator, FileUtils} from './fileutils'
import {loadTemplateSingle} from "./templates"

const shell = require('electron').shell;


export class CardModel {
    title: string;
    description: string;
    imgPath: string;
    resourcePath: string;

    constructor(title: string, description: string, imagePath: string, resourcePath: string) {
        this.title = title;
        this.description = description;
        this.imgPath = imagePath;
        this.resourcePath = resourcePath;
    }
}

export function buildFileCard(filePath: string, elem: Element, append: boolean = false, $: any = require('jquery')) {
    // let model = new CardModel(getImagePathFromDocumentName(filePath), fileNameToPrettyString(filePath));
    // loadTemplateSingle("file-card.mustache", model, (content: string, id: string) => {
    //     let snip = $(content);
    //     let container = $("#" + elem.id);
    //
    //     if (append) {
    //         container.append(snip);
    //     } else {
    //         container.empty().append(snip);
    //     }
    //     setTimeout(() => {
    //         $(`#${id}`).on("click", () => {
    //             launchDocument(filePath);
    //         });
    //     }, 1); //for some reason we have to let the dom breathe before it will let us do this?
    // });
}

export function buildWebCardsFromConfig(configName: string) {
    let elementConfig = Configurator.loadAppConfig(configName);
    let $ = require('jquery')
    let keys = Object.keys(elementConfig);


    for (let i = 0, l = keys.length; i < l; i++) {
        let key = keys[i];
        let contentList = elementConfig[key];
        let element = $(`#${key}`);
        console.log(element);
        for (let j = 0, m = contentList.length; j<m;j++){
            let content = contentList[j];
            buildWebCard(element, content, true, $);
        }

    }
}

export function buildWebCard(elem: JQuery<HTMLElement>, obj, append: boolean = false, $: any = require('jquery')) {
    let model = new CardModel(
        obj["title"],
        obj["description"],
        FileUtils.getPathToImage(obj["imagePath"]),
        obj["urlText"]
    );

    loadTemplateSingle("web-card.mustache", model, (content: string, id: string) => {
        console.log(elem);
        if (append) {
            elem.append(content);
        } else {
            elem.html(content)
        }

        $(`#${id}`).on("click", () => {
            launchWebsite(obj["urlText"].toString());
        });

    });
}


function fileNameToPrettyString(fileName: string): string {
    let name = fileName.substr(0, fileName.lastIndexOf('.'));
    name = name.replace(/[.\-_]/ig, " ")
    let buffer = name.split('');
    for (let i = 0, l = buffer.length; i < l; i++) {
        if (0 === i || ' ' === buffer[i - 1]) {
            buffer[i] = buffer[i].toUpperCase()
        }
    }
    return buffer.join('');
}

function getImagePathFromDocumentName(name: string): string {
    let ext = FileUtils.getFileExtension(name);
    let thumbnail = Configurator.getFileExtensionToImageMap()[ext];
    return FileUtils.getPathToImage(thumbnail);
}

function launchDocument(filename: string) {
    let fullPath = FileUtils.getPathToDocument(filename);
    console.log(`Attempting to open file: ${fullPath}`);
    shell.openItem(fullPath);
}

function launchWebsite(url: string) {
    console.log("Attempting to launch " + url)
    shell.openItem(url);
}