utilities.tstypescript
//a collection of miscellaneous utility functions

import { GLViewer } from "GLViewer";
import { getGradient, Gradient, GradientType } from "./Gradient";
import { VolumeData } from "./VolumeData";
import { builtinColorSchemes, CC, elementColors, htmlColors, Color } from "./colors";
import { IsoSurfaceSpec } from "GLShape";
import { inflate, InflateFunctionOptions, Data } from "pako"

/** Copy defined own properties from src1 onto obj1 and return obj1 (a shallow merge). */
export function extend(obj1, src1) {
    for (var key in src1) {
        if (src1.hasOwnProperty(key) && src1[key] !== undefined) {
            obj1[key] = src1[key];
        }
    }
    return obj1;
};

//deep copy, cannot deal with circular refs; undefined input becomes an empty object
//https://medium.com/javascript-in-plain-english/how-to-deep-copy-objects-and-arrays-in-javascript-7c911359b089
export function deepCopy(inObject) {
    let outObject, value, key;

    if (inObject == undefined) {
        return {};
    }
    if (typeof inObject !== "object" || inObject === null) {
        return inObject; // Return the value if inObject is not an object
    }

    // Create an array or object to hold the values
    outObject = Array.isArray(inObject) ? [] : {};

    for (key in inObject) {
        value = inObject[key];
        // Recursively (deep) copy for nested objects, including arrays
        outObject[key] = deepCopy(value);
    }

    return outObject;
};

export function isNumeric(obj) {

    var type = typeof (obj);
    return (type === "number" || type === "string") &&
        !isNaN(obj - parseFloat(obj));
};

export function isEmptyObject(obj: object) {
    return Object.keys(obj).length === 0;
};

export type Func = Function|string|undefined|null;

export function makeFunction(callback:Func): Function {
    //for py3dmol let users provide callback as string
    if (callback && typeof callback === "string") {
        /* jshint ignore:start */
        callback = eval("(" + callback + ")");
        /* jshint ignore:end */
    }
    // report to console if callback is not a valid function
    if (callback && typeof callback != "function") {
        console.warn("Invalid callback provided.");
        return ()=>{}; //return noop function
    }
    return callback as Function;
};

//standardize voldata/volscheme in style
export function adjustVolumeStyle(style: IsoSurfaceSpec) {
    if (style) {
        if (style.volformat && !(style.voldata instanceof VolumeData)) {
            style.voldata = new VolumeData(style.voldata, style.volformat);
        }
        if (style.volscheme) {
            style.volscheme = Gradient.getGradient(style.volscheme);
        }
    }
};


/**
 * Compute coordinate bounds and the arithmetic mean position of the supplied atoms.
 * @param {AtomSpec[]} atomlist - Atoms with finite x, y, and z coordinates.
 * @param {boolean} [ignoreSymmetries=false] - Exclude transformed symmetry positions.
 * @return {number[][]} Minimum corner, maximum corner, and mean position, each as
 * [x, y, z]. An empty atom list returns three [0, 0, 0] vectors.
 */
export function getExtent(atomlist, ignoreSymmetries?) {
    var xmin, ymin, zmin, xmax, ymax, zmax, xsum, ysum, zsum, cnt;
    var includeSym = !ignoreSymmetries;

    xmin = ymin = zmin = 9999;
    xmax = ymax = zmax = -9999;
    xsum = ysum = zsum = cnt = 0;

    if (atomlist.length === 0)
        return [[0, 0, 0], [0, 0, 0], [0, 0, 0]];
    for (var i = 0; i < atomlist.length; i++) {
        var atom = atomlist[i];
        if (typeof atom === 'undefined' || !isFinite(atom.x) ||
            !isFinite(atom.y) || !isFinite(atom.z))
            continue;
        cnt++;
        xsum += atom.x;
        ysum += atom.y;
        zsum += atom.z;

        xmin = (xmin < atom.x) ? xmin : atom.x;
        ymin = (ymin < atom.y) ? ymin : atom.y;
        zmin = (zmin < atom.z) ? zmin : atom.z;
        xmax = (xmax > atom.x) ? xmax : atom.x;
        ymax = (ymax > atom.y) ? ymax : atom.y;
        zmax = (zmax > atom.z) ? zmax : atom.z;

        if (atom.symmetries && includeSym) {
            for (var n = 0; n < atom.symmetries.length; n++) {
                cnt++;
                xsum += atom.symmetries[n].x;
                ysum += atom.symmetries[n].y;
                zsum += atom.symmetries[n].z;
                xmin = (xmin < atom.symmetries[n].x) ? xmin : atom.symmetries[n].x;
                ymin = (ymin < atom.symmetries[n].y) ? ymin : atom.symmetries[n].y;
                zmin = (zmin < atom.symmetries[n].z) ? zmin : atom.symmetries[n].z;
                xmax = (xmax > atom.symmetries[n].x) ? xmax : atom.symmetries[n].x;
                ymax = (ymax > atom.symmetries[n].y) ? ymax : atom.symmetries[n].y;
                zmax = (zmax > atom.symmetries[n].z) ? zmax : atom.symmetries[n].z;
            }
        }
    }

    return [[xmin, ymin, zmin], [xmax, ymax, zmax],
    [xsum / cnt, ysum / cnt, zsum / cnt]];
};


/**
 * Get the minimum and maximum values of an atom property.
 * Uses {@link getAtomProperty}, so entries in atom.properties take precedence
 * over same-named fields on the atom itself.
 * @param {AtomSpec[]} atomlist - Atoms to evaluate.
 * @param {string} prop - Numeric property name.
 * @return {number[]} [min, max]; [0, 0] when no finite range is available.
 */
export function getPropertyRange(atomlist, prop) {
    var min = Number.POSITIVE_INFINITY;
    var max = Number.NEGATIVE_INFINITY;

    for (var i = 0, n = atomlist.length; i < n; i++) {
        var atom = atomlist[i];
        var val = getAtomProperty(atom, prop);

        if (val != null) {
            if (val < min)
                min = val;
            if (val > max)
                max = val;
        }
    }

    if (!isFinite(min) && !isFinite(max))
        min = max = 0;
    else if (!isFinite(min))
        min = max;
    else if (!isFinite(max))
        max = min;

    return [min, max];
};


//adapted from https://stackoverflow.com/questions/3969475/javascript-pause-settimeout
export class PausableTimer {
    ident: any;
    total_time_run = 0;
    start_time: number;
    countdown: number;
    fn: any;
    arg: any;
    private paused = false;
    private pending = true;
    private canceled = false;

    constructor(fn, countdown, arg?) {
        this.fn = fn;
        this.arg = arg;
        this.countdown = countdown;
        this.schedule(countdown);
    }

    private schedule(countdown: number) {
        this.start_time = new Date().getTime();
        const timer = this;
        const fn = this.fn;
        this.ident = setTimeout(function (arg) {
            timer.pending = false;
            fn.call(this, arg);
        }, countdown, this.arg);
    }

    cancel() {
        clearTimeout(this.ident);
        this.pending = false;
        this.canceled = true;
    }

    pause() {
        if (this.paused || this.canceled) return;
        this.paused = true;
        // Remember pauses during an async callback without replaying that callback.
        if (this.pending) {
            clearTimeout(this.ident);
            this.total_time_run += new Date().getTime() - this.start_time;
        }
    }

    resume() {
        if (!this.paused || this.canceled) return;
        this.paused = false;
        if (this.pending) {
            this.schedule(Math.max(0, this.countdown - this.total_time_run));
        }
    }

    isPaused() {
        return this.paused;
    }

};

/**
 * Decode base64 into bytes.
 * @param {string} base64 - Base64 text without a data URI prefix.
 * @return {Uint8Array} Decoded bytes.
 */
export function base64ToArray(base64) {
    var binary_string = window.atob(base64);
    var len = binary_string.length;
    var bytes = new Uint8Array(len);
    for (var i = 0; i < len; i++) {
        bytes[i] = binary_string.charCodeAt(i);
    }
    return bytes;
};

/**
 * Read an atom property, first from atom.properties and then from the atom itself.
 * @param {AtomSpec} atom - Atom to inspect.
 * @param {string} prop - Property name.
 * @return {*} Property value, or null if neither location defines it.
 */
export function getAtomProperty(atom, prop) {
    var val = null;
    if (atom.properties &&
        typeof (atom.properties[prop]) != "undefined") {
        val = atom.properties[prop];
    } else if (typeof (atom[prop]) != 'undefined') {
        val = atom[prop];
    }
    return val;
};

//Miscellaneous functions and classes - to be incorporated into $3Dmol proper
/*
 * 
 * @param {$3Dmol.Geometry} geometry
 * @param {$3Dmol.Mesh} mesh
 * @returns {undefined}
 */
export function mergeGeos(geometry, mesh) {

    var meshGeo = mesh.geometry;

    if (meshGeo === undefined)
        return;

    geometry.geometryGroups.push(meshGeo.geometryGroups[0]);

};


/**
 * Parse a style or selection from JSON or the compact syntax used by embedded
 * viewers and viewer URLs. JSON supports nested specifications. Compact syntax
 * has one level of nesting: semicolons separate fields, a colon introduces a
 * value, commas separate list items or property assignments, and = or ~ assigns
 * a nested property. A field without a value becomes an empty object.
 * For example, "chain:A;resi:1,2" selects a chain and residue list, and
 * "stick:radius=0.2,color=blue" describes a stick style.
 * Numeric and boolean scalar values are converted; comma-separated list items
 * remain strings. Use JSON for nested selections such as and, or, and within.
 * @param {string|Object|null|undefined} str - Specification. Objects, null, and
 * undefined pass through unchanged; "all" becomes an empty selection object.
 * @returns {*} Parsed specification.
 */
export function specStringToObject(str) {
    if (typeof (str) === "object") {
        return str; //not string, assume was converted already
    }
    else if (typeof (str) === "undefined" || str == null) {
        return str;
    }

    //if this is a json string, parse it directly
    try {
        let parsed = JSON.parse(str);
        return parsed;
    } catch (error) {

    }

    str = str.replace(/%7E/g, '~'); //copy/pasting urls sometimes does this
    //convert things that look like numbers into numbers
    var massage = function (val) {
        if (isNumeric(val)) {
            //hexadecimal does not parse as float
            if (Math.floor(parseFloat(val)) == parseInt(val)) {
                return parseFloat(val);
            }
            else if (val.indexOf('.') >= 0) {
                return parseFloat(val); // ".7" for example, does not parseInt
            }
            else {
                return parseInt(val);
            }
        }
        //boolean conversions
        else if (val === 'true') {
            return true;
        }
        else if (val === 'false') {
            return false;
        }
        return val;
    };

    var ret = {};
    if (str === 'all') return ret;
    var fields = str.split(';');
    for (var i = 0; i < fields.length; i++) {
        var fv = fields[i].split(':');
        var f = fv[0];
        var val = {};
        var vstr = fv[1];
        if (vstr) {
            vstr = vstr.replace(/~/g, "=");
            if (vstr.indexOf('=') !== -1) {
                //has key=value pairs, must be object
                var kvs = vstr.split(',');
                for (var j = 0; j < kvs.length; j++) {
                    var kv = kvs[j].split('=', 2);
                    val[kv[0]] = massage(kv[1]);
                }
            }
            else if (vstr.indexOf(',') !== -1) {
                //has multiple values, must list
                val = vstr.split(',');
            }
            else {
                val = massage(vstr); //value itself
            }
        }
        ret[f] = val;
    }

    return ret;
};



function checkStatus(response) {
    if (!response.ok) {
        throw new Error(`HTTP ${response.status} - ${response.statusText}`);
    }
    return response;
}

/**
 * Fetch data from URL
 * 
 * @param {string} uri - URL to fetch.
 * @param {Function} [callback] - Called with the response text.
 * @return {Promise} Resolves to the response text, or the callback return value when supplied. Rejects on request or callback failure.
 */
export function get(uri, callback?) {
    var promise = fetch(uri).then(checkStatus).then((response) => response.text());
    if (callback)
        return promise.then(callback);
    else
        return promise;
}

/**
 * Download binary data (e.g. a gzipped file) into an array buffer and provide
 * arraybuffer to callback.
 * @param {string} uri - location of data
 * @param {Function} [callback] - Function to call with arraybuffer as argument.  
 * @param {string} [request] - Use "POST" to submit postdata; otherwise uses GET.
 * @param {string} [postdata] - data for POST request
 * @return {Promise} Resolves to the ArrayBuffer, or the callback return value when supplied. Rejects on request or callback failure.
 */
export function getbin(uri, callback?, request?, postdata?) {
    var promise;
    if (request == "POST") {
        promise = fetch(uri, { method: 'POST', body: postdata })
            .then((response) => checkStatus(response))
            .then((response) => response.arrayBuffer());
    } else {
        promise = fetch(uri).then((response) => checkStatus(response))
            .then((response) => response.arrayBuffer());
    }

    if (callback) return promise.then(callback);
    else return promise;
};


/**
 * Load a PDB/PubChem structure or URL into an existing viewer. Calls zoomTo()
 * and render() after loading succeeds. An empty model is added immediately,
 * including when validation or loading later fails. Use the Promise form when
 * the caller needs to distinguish success from failure.
 * @param {string} query - Structure query prefixed with "pdb:", "bcif:", "cid:",
 * or "url:". Without a prefix, four characters imply a PDB ID, a numeric value
 * implies a PubChem CID, and other strings are treated as URLs. The legacy
 * "mmtf:" prefix is redirected to BCIF. Cross-origin URLs must permit browser access.
 * @param {GLViewer} viewer - Viewer to receive the new model.
 * @param {ParserOptionsSpec} options - Options passed to the model's parser,
 * plus the download options below. Pass {} for defaults.
 * @param {string} [options.format="bcif"] - Format requested for a "pdb:" query.
 * Failed BCIF requests fall back to text PDB. For "url:" queries the URL supplies
 * the format; use get()/getbin() and addModel() to specify a format explicitly.
 * @param {string} [options.pdbUri="https://files.rcsb.org/view/"] - Base URI for
 * text PDB downloads and BCIF fallback, including its trailing slash.
 * @param {Function} [callback] - Called with the model after the asynchronous load attempt, including failures. On failure, the error is logged before calling the callback.
 * @return {GLModel|Promise<GLModel>|undefined} With a callback, returns the model immediately. Otherwise returns a Promise that resolves to the loaded model or rejects with the load error. Invalid PDB or PubChem identifiers display an alert and return undefined before starting the request or calling the callback.
 * @example
 viewer.setBackgroundColor(0xffffffff);
       $3Dmol.download('pdb:2nbd',viewer,{onemol: true,multimodel: true},function(m) {
        m.setStyle({'cartoon':{colorscheme:{prop:'ss',map:$3Dmol.ssColors.Jmol}}});
       viewer.zoomTo();
       viewer.render(callback);
    });
 */
export function download(query, viewer: GLViewer, options, callback?) {
    var type = "";
    var pdbUri = "";
    var uri = "";
    var promise = null;
    var m = viewer.addModel();

    if (query.indexOf(':') < 0) {
        //no type specifier, guess
        if (query.length == 4) {
            query = 'pdb:' + query;
        } else if (!isNaN(query)) {
            query = 'cid:' + query;
        } else {
            query = 'url:' + query;
        }
    }
    if (query.substring(0,5) == 'mmtf:') {
        console.warn('WARNING: MMTF now deprecated.  Reverting to bcif.');
        query = 'bcif:' + query.slice(5);
    }

    var handler = function (ret) {
        m.addMolData(ret, type, options);
    };

    if (query.substring(0, 5) === 'bcif:') {
        type = 'bcif';
        query = query.substring(5).toUpperCase();
        uri = "https://models.rcsb.org/" + query + '.bcif.gz';
        if (options && typeof options.noComputeSecondaryStructure === 'undefined') {
            //when fetch directly from pdb, trust structure annotations
            options.noComputeSecondaryStructure = true;
        }
    }
    else {
        if (query.substring(0, 4) === 'pdb:') {
            type = 'bcif';
            if (options && options.format) {
                type = options.format; //can override and require pdb
            }

            if (options && typeof options.noComputeSecondaryStructure === 'undefined') {
                // CIF does not read structure annotations, so keep computing them by default.
                options.noComputeSecondaryStructure = !/^(?:cif|mcif)(?:\.gz)?$/i.test(type);
            }
            query = query.substring(4).toUpperCase();
            if (!query.match(/^[1-9][A-Za-z0-9]{3}$/)) {
                alert("Wrong PDB ID");
                return;
            }
            if (type == 'bcif') {
                uri = 'https://models.rcsb.org/' + query.toUpperCase() + '.bcif.gz';
            }
            else {
                pdbUri = options && options.pdbUri ? options.pdbUri : "https://files.rcsb.org/view/";
                uri = pdbUri + query + "." + type;
            }

        } else if (query.substring(0, 4) == 'cid:') {
            type = "sdf";
            query = query.substring(4);
            if (!query.match(/^[0-9]+$/)) {
                alert("Wrong Compound ID"); return;
            }
            uri = "https://pubchem.ncbi.nlm.nih.gov/rest/pug/compound/cid/" + query +
                "/SDF?record_type=3d";
        } else if (query.substring(0, 4) == 'url:') {
            uri = query.substring(4);
            type = uri;
        }
    }

    // Keep request/setup failures asynchronous and preserve the actual rejection.
    promise = Promise.resolve().then(function () {
        return type == 'bcif' ? getbin(uri) : get(uri);
    }).then(handler);

    if (type == 'bcif') {
        promise = promise.catch(function () {
            //models.rcsb.org has rate limits and can fail, fallback to text
            pdbUri = options && options.pdbUri ? options.pdbUri : "https://files.rcsb.org/view/";
            uri = pdbUri + query + ".pdb";
            type = "pdb";
            console.warn("falling back to pdb format");
            return get(uri).then(handler);
        });
    }

    // A rendering error must not trigger another download or append data twice.
    promise = promise.then(function () {
        if (!viewer.isDestroyed?.()) {
            viewer.zoomTo();
            viewer.render();
        }
        return m;
    });
    if (callback) {
        promise.then(function (m) {
            callback(m);
        }, function (error) {
            console.error("Unable to load " + uri, error);
            callback(m);
        });
        return m;
    }
    else return promise;
};


/**  Return proper color for atom given style
 * @param {AtomSpec} atom
 * @param {Object} style - Color options: color, colorscheme, or colorfunc.
 * @return {Color}
 */
export function getColorFromStyle(atom, style): Color {
    let scheme = style.colorscheme;
    if (typeof builtinColorSchemes[scheme] != "undefined") {
        scheme = builtinColorSchemes[scheme];
    } else if (typeof scheme == "string" && scheme.endsWith("Carbon")) {
        //any color you want of carbon
        let ccolor = scheme
            .substring(0, scheme.lastIndexOf("Carbon"))
            .toLowerCase();
        if (typeof htmlColors[ccolor] != "undefined") {
            let newscheme = { ...elementColors.defaultColors };
            newscheme.C = htmlColors[ccolor];
            builtinColorSchemes[scheme] = { prop: "elem", map: newscheme };
            scheme = builtinColorSchemes[scheme];
        }
    }

    let color = atom.color;
    if (typeof style.color != "undefined" && style.color != "spectrum")
        color = style.color;
    if (typeof scheme != "undefined") {
        let prop, val;
        if (typeof elementColors[scheme] != "undefined") {
            //name of builtin colorscheme
            scheme = elementColors[scheme];
            if (typeof scheme[atom[scheme.prop]] != "undefined") {
                color = scheme.map[atom[scheme.prop]];
            }
        } else if (typeof scheme[atom[scheme.prop]] != "undefined") {
            //actual color scheme provided
            color = scheme.map[atom[scheme.prop]];
        } else if (
            typeof scheme.prop != "undefined" &&
            typeof scheme.gradient != "undefined"
        ) {
            //apply a property mapping
            prop = scheme.prop;
            var grad = scheme.gradient; //redefining scheme
            if(!(grad instanceof GradientType)) {
                grad = getGradient(typeof grad === "string" ? scheme : grad);
            }
            let range = grad.range() || [-1, 1]; //sensible default
            val = getAtomProperty(atom, prop);
            if (val != null) {
                color = grad.valueToHex(val, range);
            }
        } else if (
            typeof scheme.prop != "undefined" &&
            typeof scheme.map != "undefined"
        ) {
            //apply a discrete property mapping
            prop = scheme.prop;
            val = getAtomProperty(atom, prop);
            if (typeof scheme.map[val] != "undefined") {
                color = scheme.map[val];
            }
        } else if (typeof style.colorscheme[atom.elem] != "undefined") {
            //actual color scheme provided
            color = style.colorscheme[atom.elem];
        } else {
            console.warn("Could not interpret colorscheme " + scheme);
        }
    } else if (typeof style.colorfunc != "undefined") {
        //this is a user provided function for turning an atom into a color
        color = style.colorfunc(atom);
    }

    let C = CC.color(color);
    return C;
};

/**
 * Resolve a viewer container.
 * @param {HTMLElement|Object|string} element - Element, jQuery object, or element
 * ID without a leading "#"; strings are not arbitrary CSS selectors.
 * @return {HTMLElement|null} Container, or null when the ID is not found.
 */
export function getElement(element): HTMLElement | null {
    let ret = element;
    if (typeof (element) === "string") {
        ret = document.querySelector("#" + element);
    } else if (typeof element === 'object' && element.get) { //jquery
        ret = element.get(0);
    }
    return ret;
}

export function inflateString(str: string | ArrayBuffer, tostring: Boolean = true): (string | ArrayBuffer) {
    let data: Data;

    if (typeof str === 'string') {
        const encoder = new TextEncoder();
        data = encoder.encode(str);
    } else {
        data = new Uint8Array(str);
    }

    const inflatedData = inflate(data, {
        to: tostring ? 'string' : null
    } as InflateFunctionOptions & { to: 'string' });

    return inflatedData;
}