/*
Duosi Principles of Computer Composition Virtual Experiment System ,DS-VLAB v2.0
Copyright(C)2013 ZHANG Wen-fen, Email: yydzhwf@xnu.edu.cn  Address: Xiangnan University, Chenzhou Hunan, China
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or any later version.

多思计算机组成原理网络虚拟实验系统, DS-VLAB v2.0 
版权所有(C) 张雯雰, 电子邮箱: yydzhwf@xnu.edu.cn
本程序为自由软件；您可依据自由软件基金会所发表的GNU GENERAL PUBLIC LICENSE，对本程序再次发布和/或修改。

作者：湘南学院计算机与人工智能学院张雯雰老师
*/



;
$(function () {
    $("#opener").click(function () {
        if (mycircuit.componentAll.length > 0) {
            var r = confirm("是否放弃保存并导入新电路？");
            if (r == true) {
                mycircuit.deletecircuit();
                readFile();
            };
        } else {
            readFile();
        }
    });
    $("#saver").click(function () {
        writeFile();
    });
    $("#newfile").click(function () {
        if (mycircuit.componentAll.length > 0) {
            var r = confirm("是否放弃保存并新建电路？");
            if (r == true) {
                mycircuit.deletecircuit();
            }
        } else {
            mycircuit.deletecircuit();
        }
    });
});

var filename = "circuit.txt";
var filecontent = "";

//读文件
function readFile( ) {
    // try {
    //     window.open("readcircuit.html", "test", "toolbar=no,location=no,top=100,left=100,directories=no,status=yes,menubar=no,scrollbars=yes,location=no,resizable=yes,width=500,height=200");
    // } catch (e) {
    //     alert('read error!');
    // };
    $("body").append("<input type='file' style='display: none' id='fileUpload' onchange='upload()'  />")
    $("#fileUpload").click();
};
function upload(){
    if (window.FileReader) {
        var input=$("#fileUpload");
        var file = input[0].files[0];
        filename = file.name.split(".")[0];
        var reader = new FileReader();
        reader.onload = function() {
            recovercircuit('demo',mycircuit, this.result);
            $("#fileUpload").remove();
        }
        reader.readAsText(file);
    }
}

//写文件 
function writeFile() {
    // try {
       // var blobObject = new Blob(filecontent);
        // window.navigator.msSaveOrOpenBlob(blobObject, filename);
        filecontent = circuitdata(mycircuit);
        // var blobObject = new Blob(filecontent);
        // var url=window.URL.createObjectURL(blobObject)
        // window.open(url)
        exportRaw(filename,filecontent);
        // window.name=filecontent;
        // window.open("savecircuit.html", "test", "toolbar=no,location=no,top=100,left=100,directories=no,status=yes,menubar=no,scrollbars=yes,location=no,resizable=yes,width=500,height=200");
    // } catch (e) {
    //     console.log('write error!',e);
    // };
};
function fakeClick(obj) {
    var ev = document.createEvent("MouseEvents");
    ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    obj.dispatchEvent(ev);
}

function exportRaw(name, data) {
    var urlObject = window.URL || window.webkitURL || window;
    var export_blob = new Blob([data]);
    var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
    save_link.href = urlObject.createObjectURL(export_blob);
    save_link.download = name;
    fakeClick(save_link);
}
//把电路数据保存到一个字符串中
function circuitdata(circuit) {
    var cAll = circuit.componentAll;
    var comp= "",line="";
    var c,d,x,y,i,j,k,lId,p,sCId;
    for (i = 0; i < cAll.length; i++) {
        c = cAll[i];
        d = document.getElementById(c.id);
        x = String(d.style.left);
        y = String(d.style.top);


        // Save customName if it exists
        var customNameData = "";
        if (c.showName === true && c.showPinNo === true) {
            customNameData = "," + c.customName;
        }
        comp = comp + c.name + "," + x + "," + y + "," + c.id + customNameData + "$";
        for (j = 0; j < c.connection.length; j++) {
            if (c.pinFunction[j] == 1 || c.pinFunction[j] == 11) {
                for (k = 0; k < c.connection[j].length; k++) {
                    lId = c.connection[j][k][0].id;
                    p = lId.indexOf("Pin");
                    sCId = lId.substring(0, p);//获取起点器件的ID
                    if (sCId == c.id) {//如果是输出连线
                        line = line + lId + ",";
                    };
                };
            };
        };
    };
    return comp + "&" + line + "@" + String(circuit.count);
};

//按照字符串恢复电路图
function recovercircuit(parentId,circuit,s) {
    var comp, strline, count, p1,p2, n,x,y,id,lid,line,sPid,ePid,sPin,ePin;
    p1 = s.indexOf("&");
    comp = s.substring(0, p1);
    p2 = s.indexOf("@");
    strline = s.substring(p1 + 1, p2);
    count = Number(s.substring(p2 + 1));

    while (!(comp.length == 0)) { //绘制器件
        p1 = comp.indexOf(",");
        n = comp.substring(0, p1);
        comp = comp.substring(p1 + 1);

        p1 = comp.indexOf(",");
        x = comp.substring(0, p1);
        comp = comp.substring(p1 + 1);

        p1 = comp.indexOf(",");
        y = comp.substring(0, p1);
        comp = comp.substring(p1 + 1);

        p1 = comp.indexOf("$");
        // Check if there's a customName (additional field before $)
        var idAndCustomName = comp.substring(0, p1);
        var commaPos = idAndCustomName.indexOf(",");
        
        if (commaPos !== -1) {
            // Has customName
            id = idAndCustomName.substring(0, commaPos);
            customName = idAndCustomName.substring(commaPos + 1);
        } else {
            // No customName
            id = idAndCustomName;
            customName = null;
        }
        comp = comp.substring(p1 + 1);

        p1 = x.indexOf("px");
        x=x.substring(0,p1);
        p1 = y.indexOf("px");
        y = y.substring(0, p1);

        circuit.addComponent(parentId, "Compo" + n, x, y, id, customName);
    };

    while (!(strline.length == 0)) {//绘制连接线
        p1 = strline.indexOf(",");
        lid = strline.substring(0, p1);
        strline = strline.substring(p1 + 1);
        p1 = lid.indexOf("To");
        sPid = lid.substring(0, p1);
        ePid = lid.substring(p1 + 2);       
        sPin = document.getElementById(sPid);
        ePin = document.getElementById(ePid);
        line = circuit.lineCreate(parentId, 0, 0, 10, 10);
        sPin.focus();
        ePin.focus();
        circuit.lineAdjust(line, sPin, ePin);
        circuit.addLineToComponent(line, sPin, ePin);
    };

    circuit.count = count;//设置电路的count值

};

