/*************************************************************
 *
 *  Copyright (c) 2018 The MathJax Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @fileoverview  Implements a lightweight DOM adaptor
 *
 * @author dpvc@mathjax.org (Davide Cervone)
 */

import {AbstractDOMAdaptor} from '../core/DOMAdaptor.js';
import {LiteDocument} from './lite/Document.js';
import {LiteElement, LiteNode} from './lite/Element.js';
import {LiteText, LiteComment} from './lite/Text.js';
import {LiteList} from './lite/List.js';
import {LiteWindow} from './lite/Window.js';
import {LiteParser} from './lite/Parser.js';
import {Styles} from '../util/Styles.js';
import {userOptions, defaultOptions, OptionList} from '../util/Options.js';

/************************************************************/
/**
 * Implements a lightweight DOMAdaptor on liteweight HTML elements
 */
export class LiteAdaptor extends AbstractDOMAdaptor<LiteElement, LiteText, LiteDocument> {
    /**
     * The default options
     */
    public static OPTIONS: OptionList = {
        fontSize: 16,      // we can't compute the font size, so always use this
    };

    /**
     * The options for the instance
     */
    public options: OptionList;

    /**
     * The document in which the HTML nodes will be created
     */
    public document: LiteDocument;

    /**
     * The window for the document
     */
    public window: LiteWindow;

    /**
     * The parser for serialized HTML
     */
    public parser: LiteParser;

    /**
     * @param {OptionList} options  The options for the lite adaptor (e.g., fontSize)
     * @constructor
     */
    constructor(options: OptionList = null) {
        super();
        let CLASS = this.constructor as typeof LiteAdaptor;
        this.options = userOptions(defaultOptions({}, CLASS.OPTIONS), options);
        this.parser = new LiteParser();
        this.window = new LiteWindow();
    }

    /**
     * @override
     */
    public parse(text: string, format?: string) {
        return this.parser.parseFromString(text, format, this);
    };

    /**
     * @override
     */
    protected create(kind: string, ns: string = null) {
        return new LiteElement(kind);
    }

    /**
     * @override
     */
    public text(text: string) {
        return new LiteText(text);
    }

    /**
     * @param {string} text   The text of the comment
     * @return {LiteComment}  The comment node
     */
    public comment(text: string) {
        return new LiteComment(text);
    }

    /**
     * @return {LiteDocument}  A new document element
     */
    public createDocument() {
        return new LiteDocument();
    }

    /**
     * @override
     */
    public head(doc: LiteDocument) {
        return doc.head;
    }

    /**
     * @override
     */
    public body(doc: LiteDocument) {
        return doc.body;
    }

    /**
     * @override
     */
    public root(doc: LiteDocument) {
        return doc.root;
    }

    /**
     * @override
     */
    public tags(node: LiteElement, name: string, ns: string = null) {
        let stack = [] as LiteNode[];
        let tags = [] as LiteElement[];
        if (ns) {
            return tags;  // we don't have namespaces
        }
        let n: LiteNode = node;
        while (n) {
            let kind = n.kind;
            if (kind !== '#text' && kind !== '#comment') {
                n = n as LiteElement;
                if (kind === name) {
                    tags.push(n);
                }
                if (n.children.length) {
                    stack = n.children.concat(stack);
                }
            }
            n = stack.shift();
        }
        return tags;
    }

    /**
     * @param {LiteELement} node   The node to be searched
     * @param {string} id          The id of the node to look for
     * @return {LiteElement}       The child node having the given id
     */
    public elementById(node: LiteElement, id: string) {
        let stack = [] as LiteNode[];
        let n: LiteNode = node;
        while (n) {
            if (n.kind !== '#text' && n.kind !== '#comment') {
                n = n as LiteElement;
                if (n.attributes['id'] === id) {
                    return n;
                }
                if (n.children.length) {
                    stack = n.children.concat(stack);
                }
            }
            n = stack.shift();
        }
        return null as LiteElement;
    }

    /**
     * @param {LiteELement} node   The node to be searched
     * @param {string} name        The name of the class to find
     * @return {LiteElement[]}     The nodes with the given class
     */
    public elementsByClass(node: LiteElement, name: string) {
        let stack = [] as LiteNode[];
        let tags = [] as LiteElement[];
        let n: LiteNode = node;
        while (n) {
            if (n.kind !== '#text' && n.kind !== '#comment') {
                n = n as LiteElement;
                const classes = (n.attributes['class'] || '').split(/ /);
                if (classes.find(name)) {
                    let tags = [] as LiteElement[];
                }
                if (n.children.length) {
                    stack = n.children.concat(stack);
                }
            }
            n = stack.shift();
        }
        return tags;
    }

    /**
     * @override
     */
    public getElements(nodes: (string | LiteElement | LiteElement[])[], document: LiteDocument) {
        let containers = [] as LiteElement[];
        const body = this.body(this.document);
        for (const node of nodes) {
            if (typeof(node) === 'string') {
                if (node.charAt(0) === '#') {
                    const n = this.elementById(body, node.slice(1));
                    if (n) {
                        containers.push(n);
                    }
                } else if (node.charAt(0) === '.') {
                    containers = containers.concat(this.elementsByClass(body, node.slice(1)))
                } else if (node.match(/^[-a-z][-a-z0-9]*$/i)) {
                    containers = containers.concat(this.tags(body, node));
                }
            } else if (Array.isArray(node)) {
                containers = containers.concat(node);
            } else if (node instanceof this.window.NodeList || node instanceof this.window.HTMLCollection) {
                containers = containers.concat((node as LiteList<LiteElement>).nodes);
            } else {
                containers.push(node);
            }
        }
        return containers;
    }

    /**
     * @override
     */
    public parent(node: LiteNode) {
        return node.parent;
    }

    /**
     * @param {LiteNode} node  The node whose index is needed
     * @return {number}        THe index of the node it its parent's children array
     */
    public childIndex(node: LiteNode) {
        return (node.parent ? node.parent.children.findIndex(n => n === node) : -1);
    }

    /**
     * @override
     */
    public append(node: LiteElement, child: LiteNode) {
        if (child.parent) {
            this.remove(child);
        }
        node.children.push(child);
        child.parent = node;
        return child;
    }

    /**
     * @override
     */
    public insert(nchild: LiteNode, ochild: LiteNode) {
        if (nchild.parent) {
            this.remove(nchild);
        }
        if (ochild && ochild.parent) {
            const i = this.childIndex(ochild);
            ochild.parent.children.splice(i, 0, nchild);
            nchild.parent = ochild.parent;
        }
    }

    /**
     * @override
     */
    public remove(child: LiteNode) {
        const i = this.childIndex(child);
        if (i >= 0) {
            child.parent.children.splice(i, 1);
        }
        child.parent = null;
        return child;
    }

    /**
     * @override
     */
    public replace(nnode: LiteNode, onode: LiteNode) {
        const i = this.childIndex(onode);
        if (i >= 0) {
            onode.parent.children[i] = nnode;
        }
        return onode;
    }

    /**
     * @override
     */
    public clone(node: LiteElement) {
        const nnode = new LiteElement(node.kind);
        nnode.attributes = {...node.attributes};
        nnode.children = node.children.map(n => {
            if (n.kind === '#text') {
                return new LiteText((n as LiteText).value);
            } else if (n.kind === '#comment') {
                return new LiteComment((n as LiteComment).value);
            } else {
                const m = this.clone(n as LiteElement);
                m.parent = nnode;
                return m;
            }
        });
        return nnode;
    }

    /**
     * @override
     */
    public split(node: LiteText, n: number) {
        const text = new LiteText(node.value.slice(n));
        node.value = node.value.slice(0, n);
        node.parent.children.splice(this.childIndex(node) + 1, 0, text);
        text.parent = node.parent;
        return text;
    }

    /**
     * @override
     */
    public next(node: LiteNode) {
        const parent = node.parent;
        if (!parent) return;
        const i = this.childIndex(node) + 1;
        return (i >= 0 && i < parent.children.length ? parent.children[i] : null);
    }

    /**
     * @override
     */
    public previous(node: LiteNode) {
        const parent = node.parent;
        if (!parent) return;
        const i = this.childIndex(node) - 1;
        return (i >= 0 ? parent.children[i] : null);
    }

    /**
     * @override
     */
    public firstChild(node: LiteElement) {
        return node.children[0];
    }

    /**
     * @override
     */
    public lastChild(node: LiteElement) {
        return node.children[node.children.length - 1];
    }

    /**
     * @override
     */
    public childNodes(node: LiteElement) {
        return [...node.children];
    }

    /**
     * @override
     */
    public childNode(node: LiteElement, i: number) {
        return node.children[i];
    }

    /**
     * @override
     */
    public kind(node: LiteNode) {
        return node.kind;
    }

    /**
     * @override
     */
    public value(node: LiteNode | LiteText) {
        return (node.kind === '#text' ? (node as LiteText).value : '');
    }

    /**
     * @override
     */
    public textContent(node: LiteElement): string {
        return node.children.reduce((s: string, n: LiteNode) => {
            return s + (n.kind === '#text' ? (n as LiteText).value :
                        n.kind === '#comment' ? '' : this.textContent(n as LiteElement));
        }, "");
    }

    /**
     * @override
     */
    public innerHTML(node: LiteElement) {
        return this.parser.serializeInner(this, node);
    }

    /**
     * @override
     */
    public outerHTML(node: LiteElement) {
        return this.parser.serialize(this, node);
    }

    /**
     * @override
     */
    public setAttribute(node: LiteElement, name: string, value: string | number, ns: string = null) {
        if (typeof value !== 'string') {
            value = String(value);
        }
        if (ns) {
            name = ns.replace(/.*\//, '') + ':' + name;
        }
        node.attributes[name] = value;
        if (name === 'style') {
            node.styles = null;
        }
    }

    /**
     * @override
     */
    public getAttribute(node: LiteElement, name: string) {
        return node.attributes[name];
    }

    /**
     * @override
     */
    public removeAttribute(node: LiteElement, name: string) {
        delete node.attributes[name];
    }

    /**
     * @override
     */
    public hasAttribute(node: LiteElement, name: string) {
        return node.attributes.hasOwnProperty(name);
    }

    /**
     * @override
     */
    public allAttributes(node: LiteElement) {
        const attributes = node.attributes;
        const list = [];
        for (const name of Object.keys(attributes)) {
            list.push({name: name, value: attributes[name] as string});
        }
        return list;
    }

    /**
     * @override
     */
    public addClass(node: LiteElement, name: string) {
        const classes = (node.attributes['class'] as string || '').split(/ /);
        if (!classes.find(n => n === name)) {
            classes.push(name);
            node.attributes['class'] = classes.join(' ');
        }
    }

    /**
     * @override
     */
    public removeClass(node: LiteElement, name: string) {
        const classes = (node.attributes['class'] as string || '').split(/ /);
        const i = classes.findIndex(n => n === name);
        if (i >= 0) {
            classes.splice(i, 1);
            node.attributes['class'] = classes.join(' ');
        }
    }

    /**
     * @override
     */
    public hasClass(node: LiteElement, name: string) {
        const classes = (node.attributes['class'] as string || '').split(/ /);
        return !!classes.find(n => n === name);
    }

    /**
     * @override
     */
    public setStyle(node: LiteElement, name: string, value: string) {
        if (!node.styles) {
            node.styles = new Styles(this.getAttribute(node, 'style'));
        }
        node.styles.set(name, value);
        node.attributes['style'] = node.styles.cssText;
    }

    /**
     * @override
     */
    public getStyle(node: LiteElement, name: string) {
        if (!node.styles) {
            const style = this.getAttribute(node, 'style');
            if (!style) {
                return '';
            }
            node.styles = new Styles(style);
        }
        return node.styles.get(name);
    }

    /**
     * @override
     */
    public allStyles(node: LiteElement) {
        return this.getAttribute(node, 'style');
    }

    /**
     * @override
     */
    public fontSize(node: LiteElement) {
        return this.options.fontSize;
    }

    /**
     * @override
     */
    public nodeSize(node: LiteElement, em: number = 1, local: boolean = null) {
        const text = this.textContent(node);
        return [.6 * text.length, 0] as [number, number];
    }

    /**
     * @override
     */
    public nodeBBox(node: LiteElement) {
        return {left: 0, right: 0, top: 0, bottom: 0};
    }
}

/************************************************************/
/**
 * The function to call to obtain a LiteAdaptor
 *
 * @param {OptionList} options  The options for the adaptor
 * @return {LiteAdaptor}        The newly created adaptor
 */
export function liteAdaptor(options: OptionList = null) {
    return new LiteAdaptor(options);
}
