/**
 * Created by gooma on 4/16/2016.
 *
 * Generate font of same style, with different scale
 **/
Dr.Declare('Graphic.FontImageDataGenerator', 'class');
Dr.Require('Graphic.FontImageDataGenerator', 'Graphic.ImageDataExt');
Dr.Require('Graphic.FontImageDataGenerator', 'Graphic.ImageOperation');
Dr.Implement('Graphic.FontImageDataGenerator', function (global, module_get) {
	var ImageDataExt = module_get('Graphic.ImageDataExt');
	var ImageOperation = module_get('Graphic.ImageOperation');

	var FontImageDataGenerator = function () {
		this.symbol_image_data_map = {};
		this.scaled_symbol_image_data_map = {};
		this.setDefaultAttribute(8, 15, 'normal', 'consolas, monaco, monospace', '#000000');
	};

	FontImageDataGenerator.prototype = {
		constructor: FontImageDataGenerator,

		setDefaultAttribute: function (font_size, line_height, font_style, font_family, fill_style) {
			this._default_font_size = font_size;
			this._default_line_height = line_height;
			this._default_font_style = font_style; //normal, italic, oblique
			this._default_font_family = font_family;
			this._default_fill_style = fill_style;	//color
			this._default_font_config = this.getFontConfig();
		},
		getFontConfig: function (font_size, line_height, font_style, font_family, fill_style) {
			//check CSS font for usage
			font_size = font_size || this._default_font_size;
			line_height = line_height || this._default_line_height;
			font_style = font_style || this._default_font_style;
			font_family = font_family || this._default_font_family;
			fill_style = fill_style || this._default_fill_style;
			return {
				font_size: font_size,
				line_height: line_height,
				font_style: font_style,
				font_family: font_family,
				fill_style: fill_style,
				attribute: font_style + ' ' + font_size + 'px/' + line_height + 'px ' + font_family, //css text
				cache_tag: font_style + '|' + font_size + '|' + line_height + '|' + font_family + '|' + fill_style,
			}
		},
		//intended to be used for single character
		getSymbolMetricsWidth: function (symbol, font_config) {
			var quick_context = ImageOperation.getQuickContext();
			quick_context.font = font_config.attribute;
			return quick_context.measureText(symbol).width;
		},
		//with generated cache, intended to be used for single character
		generateSymbolImageData: function (symbol, font_config, metrics_width) {
			//Dr.log('[generateSymbolImageData]', symbol, font_config.attribute);
			//generate
			var generated_image_data_ext = ImageDataExt.create(
			var context = generated_image_data_ext.data.getContext('2d');
			context.font = font_config.attribute;
			context.textAlign = "start";
			context.textBaseline = "middle"; // better than 'top'
			context.fillStyle = font_config.fill_style;
			context.fillText(symbol, 0, font_config.line_height * 0.5);

			this.symbol_image_data_map[symbol] = this.symbol_image_data_map[symbol] || {};
			this.symbol_image_data_map[symbol][font_config.attribute] = generated_image_data_ext;

			return generated_image_data_ext;
		},
		getSymbolImageData: function (symbol, font_config) {
			if (this.symbol_image_data_map[symbol] && this.symbol_image_data_map[symbol][font_config.cache_tag]) {
				return this.symbol_image_data_map[symbol][font_config.cache_tag];
			}
			else {
				return this.generateSymbolImageData(symbol, font_config, this.getSymbolMetricsWidth(symbol, font_config));
			}
		},
		getScaledSymbolImageData: function (symbol, scale_ratio, font_config) {
			font_config = font_config || this._default_font_config;
			var cache_key = symbol + '|' + scale_ratio + ':' + font_config.cache_tag;
			var scaled_image_data = this.scaled_symbol_image_data_map[cache_key];
			if (!scaled_image_data) {
				Dr.debug(5, 'cache add', cache_key);
				var symbol_image_data = this.getSymbolImageData(symbol, font_config);
				scaled_image_data = ImageDataExt.copy(symbol_image_data);
				scaled_image_data.toCanvas();
				scaled_image_data.scale(scale_ratio);
				this.scaled_symbol_image_data_map[cache_key] = scaled_image_data;
			}
			return scaled_image_data;
		},
	};

	return FontImageDataGenerator;
});
				ImageDataExt.type.CANVAS_ELEMENT,
				metrics_width,
				font_config.line_height);