/*
 * Copyright (c) 2011 by RibbonSoft, GmbH. All rights reserved.
 * 
 * This file is part of the QCAD project.
 *
 * Licensees holding valid QCAD Professional Edition licenses 
 * may use this file in accordance with the QCAD License
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, 
 * INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE.
 * 
 * See http://www.ribbonsoft.com for further details.
 */

include("../File.js");
include("scripts/ImportExport/SvgImporter/SvgImporter.js");
//include("env.js");

/*
var window = new Object();
window.navigator = new Object();
window.navigator.userAgent = "WebKit";
*/

//include("cake.js");

function SvgImport(guiAction) {
    File.call(this, guiAction);
}

SvgImport.prototype = new File();

SvgImport.basePath = includeBasePath;

SvgImport.prototype.beginEvent = function() {
    File.prototype.beginEvent.call(this);
    
    var lastDir = RSettings.getStringValue(
            "SvgImport/Path",
            QDesktopServices.storageLocation(QDesktopServices.DocumentsLocation));
    var filename = QFileDialog.getOpenFileName(
        this, qsTr("Import SVG"), lastDir,
        qsTr("SVG Files") + " (*.svg);;" + qsTr("All Files") + " (*)");
    if (filename.length===0) {
        this.terminate();
        return;
    }
    RSettings.setValue("SvgImport/Path", new QFileInfo(filename).absolutePath());
    
    //this.importSvg(SvgImport.basePath + "/my.svg");
    var svgImporter = new SvgImporter(EAction.getDocument());
    svgImporter.importFile(filename);
    if (!isNull(EAction.getDocumentInterface())) {
        EAction.getDocumentInterface().regenerateScenes();
    }

    
    this.terminate();
};

SvgImport.prototype.importSvg = function(filename) {
    /*
    var xhr = new XMLHttpRequest();
    xhr.open('GET', filename, true);
    //xhr.overrideMimeType('text/xml')
    var failureFired = false;
    xhr.onreadystatechange = function() {
        //if (xhr.readyState == 4) {
            //if (xhr.status == 200 || xhr.status == 0) {
                try {
                    var svg = xhr.responseXML;
                    var svgNode = SVGParser.parse(svg, config);
                    svgNode.svgRootElement = svg;
                }
                catch(e) {
                    return;
                }
            //}
        //}
    }

    SVGParser.load(
            filename,
            {
            width: 640,
            height: 480,
            fontSize: 12,
            //currentColor: cc,
            onSuccess: function (svgNode, xhr) {
                debugger;
                qDebug(svgNode);
//                if (th.file == filename) {
//                    th.fileTitle.innerHTML = svgNode.title || "";
//                    th.fileDesc.innerHTML = svgNode.description || "";
//                    if (svgNode.docWidth) {
//                        svgNode.scale = Math.min(1, 640 / svgNode.docWidth, 480 / svgNode.docHeight);
//                        svgNode.x = (640 - svgNode.docWidth * svgNode.scale) / 2;
//                        svgNode.y = (480 - svgNode.docHeight * svgNode.scale) / 2;
//                    }
//                    th.svgScene.append(svgNode);
//                    if (xhr.status == 0 && filename.search(/^svg_emblems/) != -1) {
//                        th.fileEmbed.innerHTML = "<object " + "type=\"image/svg+xml\" width=\"48\" height=\"48\" data=" + filename.escape() + "></object>";
//                    } else {
//                        th.fileEmbed.innerHTML = "";
//                    }
//                }
                loading.removeSelf();
            },
            onFailure: function (xhr, e) {
                debugger;
                qDebug("Error: " + xhr.status);
            }
            }
    );
    //debugger;
    */


};

SvgImport.init = function(basePath) {
    var action = new RGuiAction(qsTr("SVG &Import..."), RMainWindowQt
            .getMainWindow());
    action.setRequiresDocument(true);
    action.setScriptFile(basePath + "/SvgImport.js");
    action.setIcon(basePath + "/SvgImport.svg");
    action.setDefaultCommands( [ "svgimport" ]);
    action.setSortOrder(2300);
    //action.setNoState();
    EAction.addGuiActionTo(action, File, true, false, false, false);
};
