/**
 * @file 预览组件
 * @author mengke01(kekee000@gmail.com)
 */


define(
    function (require) {

        var writettf = require('./util/writettf');
        var ttf2woff = require('fonteditor-core/ttf/ttf2woff');
        var ttf2eot = require('fonteditor-core/ttf/ttf2eot');
        var ttf2svg = require('fonteditor-core/ttf/ttf2svg');
        var ttf2base64 = require('fonteditor-core/ttf/ttf2base64');
        var woff2base64 = require('fonteditor-core/ttf/woff2base64');
        var eot2base64 = require('fonteditor-core/ttf/eot2base64');
        var svg2base64 = require('fonteditor-core/ttf/svg2base64');
        var extend = require('common/lang').extend;
        var ttf2icon = require('fonteditor-core/ttf/ttf2icon');

        var previewRender = require('../template/preview-render'); // 模板渲染函数

        var isIE = !!window.ActiveXObject || 'ActiveXObject' in window;

        /**
         * 生成预览模板
         *
         * @param {Object} ttf ttfObject
         * @param {string} fontFormat 字体类型
         * @return {string} html字符串
         */
        function generatePreviewHTML(ttf, fontFormat) {
            fontFormat = fontFormat || ttf;

            var fontData = '';
            var buffer;

            if (fontFormat === 'woff') {
                buffer = writettf(ttf);
                fontData = woff2base64(ttf2woff(buffer));
            }
            else if (fontFormat === 'eot') {
                buffer = writettf(ttf);
                fontData = eot2base64(ttf2eot(buffer));
            }
            else  if (fontFormat === 'svg') {
                fontData = svg2base64(ttf2svg(ttf));
            }
            else {
                buffer = writettf(ttf);
                fontData = ttf2base64(buffer);
            }

            var data = extend(
                {
                    fontData: fontData,
                    fontFormat: fontFormat
                },
                ttf2icon(ttf)
            );

            return previewRender.renderPreview(data);
        }


        var previewer = {

            /**
             * 加载预览按钮
             *
             * @param {Object} ttf ttfObject
             * @param {string} fontFormat 字体类型
             */
            load: function (ttf, fontFormat) {
                try {
                    var html = generatePreviewHTML(ttf, fontFormat);
                    var win = window.open('./empty.html');
                    if (isIE && win.document && win.document.body) {
                        win.document.body.innerHTML = html;
                        win.focus();
                    }
                    else {
                        win.onload = function () {
                            win.document.body.innerHTML = html;
                            win.focus();
                            win = null;
                            html = null;
                        };
                    }
                }
                catch (exp) {
                    alert(exp.message);
                    throw exp;
                }
            }
        };

        return previewer;
    }
);
