var fs = require('fs');
var _ = require('../js/lib/lodash');

var maki = require('maki/www/maki-sprite.json');
var lineIcons = require('./line-icons.json');
var relationIcons = require('./relation-icons.json');

// Generate CSS
var template = '.feature-{name}{background-position:-{x}px -{y}px;}\n';
var css = "/* This file is generated by make. Do NOT edit manually. */\n\n";
css += ".preset-icon{background-image:url(img/maki-sprite.png);background-repeat:no-repeat;width:24px;height:24px;}\n";
css += ".preset-icon-line{background-image:url(img/line-presets.png);background-repeat:no-repeat;width:60px;height:60px;}\n";
css += ".preset-icon-relation{background-image:url(img/relation-presets.png);background-repeat:no-repeat;width:60px;height:60px;}\n";

var images = {};

_.forEach(maki, function(dimensions, name) {
    var match = name.match(/(.*)-(12|18|24)/),
        name = match[1],
        size = match[2],
        group = images[name] = images[name] || {};
    group[size] = [dimensions.x, dimensions.y];

    if (dimensions.width === 24) {
        css += template.replace('{name}', name.replace('-24', ''))
            .replace('{x}', dimensions.x)
            .replace('{y}', dimensions.y);
    }
});

template = '.preset-icon-line.feature-{name}{background-position:-{x}px -{y}px;}\n';

_.forEach(lineIcons, function(position, name) {
    css += template.replace('{name}', name)
        .replace('{x}', position[0])
        .replace('{y}', position[1]);

    images[name] = images[name] || {};
    images[name].line = position;
});

template = '.preset-icon-relation.feature-{name}{background-position:-{x}px -{y}px;}\n';

_.forEach(relationIcons, function(position, name) {
    css += template.replace('{name}', name)
        .replace('{x}', position[0])
        .replace('{y}', position[1]);

    images[name] = images[name] || {};
    images[name].relation = position;
});

fs.writeFileSync('./css/feature-icons.css', css);
fs.writeFileSync('./data/feature-icons.json', JSON.stringify(images));
