"use strict";

exports.initBDimpl = function(opts) {
    return function(wrapRenderer) {
        return function(browser) {
            return function(el) {
                return function() {
                    var renderers = {};
                    opts.renderers.forEach(function(r) {
                        renderers[r.name] = wrapRenderer(r.renderer, r.canvasHeight);
                    });

                    // TODO: make sure to only add BD tracks when that becomes relevant
                    var sources = opts.sources;

                    var b = new browser({

                        injectionPoint: el,

                        // these should be set to default and updated from the main container;
                        // BD shouldn't do any scrolling on its own
                        chr:        '11',
                        defaultStart: 10000000,
                        defaultEnd: 30000000,
                        viewStart:  10000000,
                        viewEnd:    30000000,

                        // Can this be changed dynamically?
                        // It should be controlled by PS, too.
                        // Maybe it doesn't matter if we're controlling the viewport w/ the API
                        maxViewWidth: 20000000,

                        // All the maxHeight does is set the BD holder CSS...
                        // Should (somehow) be controlled by PS.
                        // maxHeight: opts.maxHeight || 1000,
                        maxHeight: null,

                        // BD remembering anything will probably only lead to
                        // inconsistencies with the main container
                        noPersist: true,

                        // these should be set based on the track configs in opts,
                        // but that's probably overkill right now...
                        externalRenderers: renderers,

                        // idk how best to do this. for now let it be hardcoded,
                        // later send as plain JSON, maybe use a function to generate it.
                        // the first four are mandatory;
                        // `taxon` should be an integer from the NCBI taxonomy,
                        // `auth` and `version` should match authorty and version strings
                        // used in the DAS Registry
                        // however the DAS Registry isn't a thing anymore
                        // given how we use BD, I think we can skip most of it
                        coordSystem: {
                            speciesName: 'Mouse',
                            taxon: 10090,
                            auth: 'GRCm',
                            version: 38,
                            ucscName: 'mm10'
                        },

                        // workers/threading should be configurable in the main container
                        maxWorkers: 0,

                        sources: sources,
                        /*uiPrefix: '../',*/

                        fullScreen: true
                    });

                    return b;
                };
            };
        };
    };
};


exports.addFeatureListener = function(bd) {
    return function(callback) {
        return function() {
            bd.addFeatureListener(function(ev, feature, hit, tier) {
                callback(feature)();
            });
        };
    };
};

exports.addInitListener = function(bd) {
    return function(callback) {
        return function() {
            bd.addInitListener(function() {
                callback();
            });
        };
    };
};

exports.setLocationImpl = function(bd) {
    return function(chr) {
        return function(xl) {
            return function(xr) {
                return function() {
                    bd.setLocation(chr, xl, xr);
                };
            };
        };
    };
};

exports.scrollViewImpl = function(bd) {
    return function(x) {
        return function() {
            var xl = (bd.viewStart|0) + x;
            var xr = (bd.viewEnd|0) + x;
            bd.setLocation(null, xl, xr);
        };
    };
};

@@ -1520,20 +1520,23 @@
 Height: 
-null
+1000000
 ,%0A%0A     

Fix vertical size of BD browser