import Evolver from './evolver'
import {drawPolySet} from './render'
import {getTimeStamp} from './utils'

let startMillis = null,
    totMillis,
    ctxDisplay,
    evolver;

function calculateScale(sliderScale) {
    return Math.pow(2, (4 - sliderScale));
}

export function eCreate(state) {

    ctxDisplay = document.getElementById('canvas-display').getContext('2d', {alpha: false});

    const scale = calculateScale(state.scale);

    evolver = new Evolver(state.inputImage, state.canvasWidth / scale, state.canvasHeight / scale, 0,
        '24-bit', true, true, 0.75,
        state.stepsBeforeHeuristics, scale, ctxDisplay
    );
    totMillis = 0;
    startMillis = null;

}

export function eStep(state) {

    if (startMillis === null) {
        startMillis = getTimeStamp();
    }

    const scale = calculateScale(state.scale);

    if (scale !== evolver.scale) {
        evolver.updateScale(scale);
    }

    return evolver.step(state);
}

export function eDraw() {
    drawPolySet(ctxDisplay, evolver.polySetBest, evolver.imgWidth, evolver.imgHeight, evolver.scale);
    return {
        secondsRun: (totMillis + getTimeStamp() - startMillis) / 1000,
        numSteps: evolver.stats.numSteps,
        numPolygons: evolver.polySetBest.length
    }
}

export function ePause() {
    totMillis += getTimeStamp() - startMillis;
    startMillis = null;
}

export function eSVG() {
    let svgStr = '<?xml version="1.0" encoding="utf-8"?>';
    svgStr += '<!-- Made with dirtytriangles.com -->';
    svgStr += '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">';
    svgStr += '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" version="1.1" baseProfile="full" width="' + evolver.imgWidth + 'px" height="' + evolver.imgHeight + 'px">';

    evolver.polySetBest.forEach(function (poly) {
        svgStr += '<polygon points="';
        for (let j = 0; j < poly.coords.length; j += 2) {
            svgStr += poly.coords[j] + ' ' + poly.coords[j + 1] + ' ';
        }
        svgStr += '" fill="rgb(';
        svgStr += poly.colour.r + ',';
        svgStr += poly.colour.g + ',';
        svgStr += poly.colour.b + ')" opacity="';
        svgStr += poly.colour.a + '" />';
    });
    svgStr += '<\/svg>';

    return svgStr;

}

@@ -1392,22 +1392,45 @@
     
-let svgStr = '
+const s = evolver.scale;%0A    return %60
 %3C?xm
@@ -1463,34 +1463,17 @@
 utf-8%22?%3E
-';%0A    svgStr += '
+%0A
 %3C!-- Mad
@@ -1501,34 +1501,17 @@
 .com --%3E
-';%0A    svgStr += '
+%0A
 %3C!DOCTYP
@@ -1604,26 +1604,9 @@
 td%22%3E
-';%0A    svgStr += '
+%0A
 %3Csvg
@@ -1688,84 +1688,20 @@
 nk%22 
-xmlns:ev=%22http://www.w3.org/2001/xml-events%22 version=%221.1%22 baseProfile=%22full
+version=%221.1
 %22 wi
@@ -1705,20 +1705,18 @@
  width=%22
-' + 
+$%7B
 evolver.
@@ -1724,19 +1724,20 @@
 mgWidth 
-+ '
+* s%7D
 px%22 heig
@@ -1740,20 +1740,18 @@
 height=%22
-' + 
+$%7B
 evolver.
@@ -1764,23 +1764,19 @@
 ght 
-+ 'px%22%3E';%0A%0A    
+* s%7Dpx%22%3E%0A$%7B
 evol
@@ -1795,15 +1795,11 @@
 est.
-forEach
+map
 (fun
@@ -1821,27 +1821,24 @@
         
-svgStr += '
+return %60
 %3Cpolygon
@@ -1850,280 +1850,135 @@
 ts=%22
-';%0A        for (let j = 0; j %3C poly.coords.length; j += 2) %7B%0A            svgStr += poly.coords%5Bj%5D + ' ' + poly.coords%5Bj + 1%5D + ' ';%0A        %7D%0A        svgStr += '%22 fill=%22rgb(';%0A        svgStr += poly.colour.r + ',';%0A        svgStr += poly.colour.g + ',';%0A        svgStr += 
+$%7Bpoly.coords.map(function (c) %7B%0A            return c * s%0A        %7D).join(' ')%7D%22 fill=%22rgb($%7Bpoly.colour.r%7D,$%7Bpoly.colour.g%7D,$%7B
 poly
@@ -1986,20 +1986,17 @@
 colour.b
- + '
+%7D
 )%22 opaci
@@ -2003,29 +2003,10 @@
 ty=%22
-';%0A        svgStr += 
+$%7B
 poly
@@ -2018,25 +2018,21 @@
 ur.a
- + '%22 /%3E'
+%7D%22/%3E%60
 ;%0A    %7D)
 ;%0A  
@@ -2031,54 +2031,23 @@
   %7D)
-;%0A    svgStr += '%3C%5C/svg%3E';%0A%0A    return svgStr;%0A
+.join()%7D%3C/svg%3E%60;
 %0A%7D%0A

Make SVG output the same size as when evolving image.