'use strict';

const fs = require('fs');
const Canvas = require('canvas');

const Point = require('./Point.js');

// Source: https://github.com/sunng87/heatcanvas/blob/master/heatcanvas.js
class RegionVisualizer {
    constructor(region, edges, debugpoints) {
        this.region = region;
        this.edges = edges;
        this.debugpoints = debugpoints;
    }

    render(filename) {
        if (!filename) filename = 'edges.png';
        var size = this.region.max;
        var canvas = new Canvas(size.x, size.y);
        var ctx = canvas.getContext('2d');
        ctx.clearRect(0, 0, size.x, size.y);

        var defaultColor = this.bgcolor || [0, 0, 0, 255];
        var canvasData = ctx.createImageData(size.x, size.y);
        for (var i = 0; i < canvasData.data.length; i += 4){
            canvasData.data[i  ] = defaultColor[0]; // r
            canvasData.data[i+1] = defaultColor[1];
            canvasData.data[i+2] = defaultColor[2];
            canvasData.data[i+3] = defaultColor[3];
        }

        var stopColor = [100, 255, 100, 255];
        for (var x = 0; x < size.x; x++) {
            for (var y = 0; y < size.y; y++) {
                var value = this.region._data[x][y];
                if (value && value.station) {
                    // MDC ImageData:
                    // data = [r1, g1, b1, a1, r2, g2, b2, a2 ...]
                    var pixelColorIndex = y * size.x * 4 + x * 4;

                    var color = stopColor;
                    canvasData.data[pixelColorIndex] = color[0]; // r
                    canvasData.data[pixelColorIndex + 1] = color[1]; // g
                    canvasData.data[pixelColorIndex + 2] = color[2]; // b
                    canvasData.data[pixelColorIndex + 3] = color[3]; // a
                }
            }
        }

        if (this.debugpoints) {
            var debugColor = [255, 0, 0, 255];
            for (var i = 0; i < this.debugpoints.length; i++) {
                var point = this.debugpoints[i];
                var pixelColorIndex = point.y * size.x * 4 + point.x * 4;

                var color = debugColor;
                canvasData.data[pixelColorIndex] = color[0]; // r
                canvasData.data[pixelColorIndex + 1] = color[1]; // g
                canvasData.data[pixelColorIndex + 2] = color[2]; // b
                canvasData.data[pixelColorIndex + 3] = color[3]; // a
            }
        }

        ctx.putImageData(canvasData, 0, 0);

        if (this.edges) {
            for (var i = 0; i < this.edges.length; i++) {
                var edge = this.edges[i];
                ctx.strokeStyle = 'rgba(0,0,255,255)';
                ctx.beginPath();
                ctx.lineTo(edge.from.x, edge.from.y);
                ctx.lineTo(edge.to.x, edge.to.y);
                ctx.stroke();
            }
        }

        // Write to file
        var out = fs.createWriteStream(filename);
        var stream = canvas.pngStream();
        stream.on('data', function(chunk){
            out.write(chunk);
        });

        stream.on('end', function(){
            console.log('saved png');
        });
    }
}

module.exports = RegionVisualizer;

@@ -241,32 +241,39 @@
 ges, debugpoints
+, scale
 ) %7B%0A        this
@@ -358,16 +358,54 @@
 points;%0A
+        if (!scale) this.scale = 5.0;%0A
     %7D%0A%0A 
@@ -543,32 +543,58 @@
 anvas(size.x
-, size.y
+ * this.scale, size.y * this.scale
 );%0A        v
@@ -661,24 +661,50 @@
 , size.x
-, size.y
+ * this.scale, size.y * this.scale
 );%0A%0A    
@@ -747,15 +747,21 @@
 %7C%7C %5B
-0, 0, 0
+255, 255, 255
 , 25
@@ -815,24 +815,50 @@
 a(size.x
-, size.y
+ * this.scale, size.y * this.scale
 );%0A     
@@ -1166,20 +1166,15 @@
  = %5B
-10
 0, 
-255, 10
+50, 
 0, 2
@@ -1522,22 +1522,61 @@
 y * 
-size.x * 4 + x
+this.scale * size.x * this.scale * 4 + x * this.scale
  * 4
@@ -2200,28 +2200,67 @@
 y * 
-size.x * 4 + point.x
+this.scale * size.x * this.scale * 4 + point.x * this.scale
  * 4
@@ -2814,18 +2814,22 @@
 = 'rgba(
-0,
+150,15
 0,255,25
@@ -2908,21 +2908,47 @@
 om.x
-, edge.from.y
+ * this.scale, edge.from.y * this.scale
 );%0A 
@@ -2986,19 +2986,45 @@
 to.x
-, edge.to.y
+ * this.scale, edge.to.y * this.scale
 );%0A 

Use white background color and allow scaling