const d3 = require('d3');

const _ = {
    map: require('lodash/map'),
    uniqBy: require('lodash/uniqBy'),
    capitalize: require('lodash/capitalize'),
    each: require('lodash/each')
};

const Radar = require('../models/radar');
const Quadrant = require('../models/quadrant');
const Ring = require('../models/ring');
const Blip = require('../models/blip');
const GraphingRadar = require('../graphing/radar');
const MalformedDataError = require('../exceptions/malformedDataError');
const SheetNotFoundError = require('../exceptions/sheetNotFoundError');
const ContentValidator = require('./contentValidator');
const ExceptionMessages = require('./exceptionMessages');

const RadarBuilder = function (data) {
    let self = {};

    self.build = function () {
        // Error handler.
        function displayErrorMessage(exception) {
            d3.selectAll('.loading').remove();
            let message = 'Oops! It looks like there are some problems with loading your data. ';

            if (exception instanceof MalformedDataError) {
                message = message.concat(exception.message);
            } else if (exception instanceof SheetNotFoundError) {
                message = exception.message;
            } else {
                console.error(exception);
            }

            d3.select('body')
                .append('div')
                .attr('class', 'error-container')
                .append('div')
                .attr('class', 'error-container__message')
                .append('p')
                .html(message);
        }

        function createHeader(data) {

        }

        // Build radar data.
        function createRadar(blips) {
            d3.selectAll('.loading').remove();
            var rings = _.map(_.uniqBy(blips, 'ring'), 'ring');
            var ringMap = {};
            var maxRings = 4;

            _.each(rings, function (ringName, i) {
                if (i === maxRings) {
                    throw new MalformedDataError(ExceptionMessages.TOO_MANY_RINGS);
                }
                ringMap[ringName] = new Ring(ringName, i);
            });

            var quadrants = {};
            _.each(blips, function (blip) {
                if (!quadrants[blip.quadrant]) {
                    quadrants[blip.quadrant] = new Quadrant(_.capitalize(blip.quadrant));
                }
                quadrants[blip.quadrant].add(new Blip(blip.name, ringMap[blip.ring], blip.isNew, blip.topic, blip.description));
            });

            var radar = new Radar();
            _.each(quadrants, function (quadrant) {
                radar.addQuadrant(quadrant);
            });

            var size = (window.innerHeight - 133) < 620 ? 620 : window.innerHeight - 133;

            new GraphingRadar(size, radar).init().plot();
        }

        // Invoke.
        createRadar(data);
    }

    // Initializer.
    self.init = function () {
        var content = d3.select('body')
            .append('div')
            .attr('class', 'loading')
            .append('div')
            .attr('class', 'input-sheet');

        var bannerText = '<h1>Building your radar...</h1><p>Your Technology Radar will be available in just a few seconds</p>';
        // TODO: loading?
        return self;
    };
    return self;
};

module.exports = {
    RadarBuilder
};

@@ -1673,24 +1673,169 @@
 ar(blips) %7B%0A
+            // See if we already have a radar.%0A            var existingRadar = d3.select('#radar');%0A            if (existingRadar.empty()) %7B%0A    
             
@@ -1861,32 +1861,36 @@
 ing').remove();%0A
+    
             var 
@@ -1941,32 +1941,36 @@
 ');%0A            
+    
 var ringMap = %7B%7D
@@ -1979,24 +1979,28 @@
             
+  
+  
 var maxRings
@@ -2006,16 +2006,20 @@
 s = 4;%0A%0A
+    
         
@@ -2073,24 +2073,28 @@
             
+  
+  
 if (i === ma
@@ -2099,24 +2099,28 @@
 maxRings) %7B%0A
+    
             
@@ -2199,34 +2199,42 @@
                 
+  
-%7D%0A
+  %7D%0A    
                 
@@ -2280,37 +2280,45 @@
 i);%0A            
+    
 %7D);%0A%0A
+    
             var 
@@ -2341,24 +2341,28 @@
             
+ 
+   
 _.each(blips
@@ -2377,24 +2377,28 @@
 on (blip) %7B%0A
+    
             
@@ -2430,24 +2430,28 @@
 uadrant%5D) %7B%0A
+    
             
@@ -2536,32 +2536,36 @@
                 
+    
 %7D%0A              
@@ -2558,32 +2558,36 @@
                 
+ 
+   
 quadrants%5Bblip.q
@@ -2687,37 +2687,45 @@
 ));%0A            
+    
 %7D);%0A%0A
+    
             var 
@@ -2741,24 +2741,28 @@
 ew Radar();%0A
+    
             
@@ -2817,16 +2817,20 @@
         
+    
 radar.ad
@@ -2854,37 +2854,45 @@
 t);%0A            
+    
 %7D);%0A%0A
+    
             var 
@@ -2978,16 +2978,20 @@
         
+   
+ 
 new Grap
@@ -3024,24 +3024,38 @@
 t().plot();%0A
+            %7D%0A
         %7D%0A%0A 

Make sure the radar isn't initialized/plotted multiple times.