const fs = require("fs");
const path = require("path");
const mkdirp = require("mkdirp");
const request = require("request");
const {ArgumentParser} = require("argparse");
const Canvas = require("canvas");
const Promise = require("promise");
const Sunwell = require("../dist/sunwell.node");

function renderCard(sunwell, card, filePath, resolution) {
	if (!card.type || !card.cardClass) {
		console.log("Skipping", card.id, "(no card to render)");
		return;
	}

	if (
		card.type !== "MINION" &&
		card.type !== "SPELL" &&
		card.type !== "WEAPON" &&
		card.type !== "HERO"
	) {
		console.log("Skipping", card.id, "(not a renderable card)");
		return;
	}

	if (!fs.existsSync(card.texture)) {
		console.log("Skipping", card.id, "(texture not found)", card.texture);
		return;
	}

	// Turn the texture into an image object
	fs.readFile(card.texture, function(err, data) {
		if (err) {
			throw err;
		}
		card.texture = new Canvas.Image();
		card.texture.src = data;

		let callback = function(canvas) {
			let out = fs.createWriteStream(filePath);
			let stream = canvas.pngStream();
			stream.on("data", chunk => {
				out.write(chunk);
			});
			stream.on("end", chunk => {
				console.log("Done rendering", filePath);
			});
			stream.on("error", chunk => {
				console.log("Error writing chunk", filePath);
			});
		};

		sunwell.createCard(card, resolution, null, callback);
	});
}

function drawFromJSON(sunwell, body, textureDir, outputDir, resolution) {
	const data = JSON.parse(body);
	drawFromData(sunwell, data, textureDir, outputDir, resolution);
}

function drawFromData(sunwell, data, textureDir, outputDir, resolution) {
	for (let i in data) {
		var card = data[i];
		card.key = i;
		var renderPath = path.join(outputDir, card.id + ".png");
		var textureName = card.id + ".jpg";
		var texturePath = path.join(textureDir, textureName);

		if (!fs.existsSync(renderPath)) {
			card.texture = texturePath;
			renderCard(sunwell, card, renderPath, resolution);
		}
	}
}

const fonts = {
	"belwe/belwe-extrabold.ttf": {family: "Belwe"},
	"franklin-gothic-bold/franklingothic-demicd.ttf": {
		family: "Franklin Gothic Bold",
		weight: "bold",
	},
	"franklin-gothic-italic/franklingothic-medcdit.ttf": {
		family: "Franklin Gothic Italic",
		style: "italic",
	},
	"franklin-gothic/franklingothic-medcd.ttf": {family: "Franklin Gothic"},
};

function main() {
	var p = new ArgumentParser({
		description: "Generate card renders",
	});
	p.addArgument("file", {nargs: "+"});
	p.addArgument("--assets-dir", {defaultValue: path.resolve("..", "src", "assets")});
	p.addArgument("--font-dir", {defaultValue: path.resolve("fonts")});
	p.addArgument("--output-dir", {defaultValue: path.resolve("out")});
	p.addArgument("--texture-dir", {defaultValue: path.resolve("textures")});
	p.addArgument("--resolution", {type: "int", defaultValue: 512});
	p.addArgument("--only", {type: "string", defaultValue: ""});
	p.addArgument("--debug", {action: "storeTrue"});
	var args = p.parseArgs();
	var sunwell = new Sunwell({
		titleFont: "Belwe",
		bodyFontBold: "Franklin Gothic Bold",
		bodyFontItalic: "Franklin Gothic Italic",
		bodyFontSize: 38,
		bodyLineHeight: 42,
		bodyFontOffset: {x: 0, y: 40},
		assetFolder: path.resolve(args.assets_dir) + "/",
		debug: args.debug,
		// platform: new NodePlatform(),
		cacheSkeleton: false,
	});

	for (let key of Object.keys(fonts)) {
		let font = fonts[key];
		let fontPath = path.join(args.font_dir, key);
		if (!fs.existsSync(fontPath)) {
			console.log("WARNING: Font not found:", fontPath);
		} else {
			Canvas.registerFont(fontPath, font);
		}
	}

	const only = args.only.split(",").map(id => id.trim());

	let outdir = path.resolve(args.output_dir);
	mkdirp.sync(outdir);

	for (let file of args.file) {
		fs.readFile(file, function(err, body) {
			if (err) {
				console.log("Error reading", file, err);
				return;
			}
			let data = JSON.parse(body);
			if (only.length && only[0].length) {
				data = data.filter(card => only.indexOf(card.id) !== -1);
			}
			drawFromData(sunwell, data, path.resolve(args.texture_dir), outdir, args.resolution);
		});
	}
}

main();

@@ -336,24 +336,33 @@
 , resolution
+, premium
 ) %7B%0A%09if (!ca
@@ -1325,17 +1325,16 @@
 );%0A%09%09%7D;%0A
-%0A
 %09%09sunwel
@@ -1363,16 +1363,25 @@
 olution,
+ premium,
  null, c
@@ -1638,24 +1638,33 @@
 , resolution
+, premium
 ) %7B%0A%09for (le
@@ -1716,16 +1716,83 @@
 ey = i;%0A
+%09%09var renderName = card.id + (premium ? %22_premium%22 : %22%22) + %22.png%22;%0A
 %09%09var re
@@ -1827,24 +1827,18 @@
 ir, 
-card.id + %22.png%22
+renderName
 );%0A%09
@@ -2045,24 +2045,33 @@
 , resolution
+, premium
 );%0A%09%09%7D%0A%09%7D%0A%7D%0A
@@ -2929,24 +2929,76 @@
 lue: 512%7D);%0A
+%09p.addArgument(%22--premium%22, %7Baction: %22storeTrue%22%7D);%0A
 %09p.addArgume
@@ -3298,17 +3298,17 @@
 eight: 4
-2
+0
 ,%0A%09%09body
@@ -3329,18 +3329,18 @@
 : 0, y: 
-40
+26
 %7D,%0A%09%09ass
@@ -4231,16 +4231,30 @@
 solution
+, args.premium
 );%0A%09%09%7D);

Add premium option to node script