//log instance
var LoadLogMemory = new LogMemory();
var GETLogMemory = LoadLogMemory.LogMemoryInstance();
var MemoryAll = LoadLogMemory.GETLog();
//URL instance
var userSetting = new GetUrlElement();
var UrlElement = userSetting.GetUrlElementInstance();


/*
LogMemory()
 Memory()
LogCollect()
LogSimplePhotoCondtionText()
*/

function LogMemory(Log){
  console.log("log");
  var arg = new Array();
  var i=0;
  return {
    LogMemoryInstance: function(log) {
      arg[i] = log;
      console.log(i);
      console.log(arg[i]);
      console.log(JSON.stringify(arg));
      i = i + 1;
      return arg;
    },
    GETLog: function(){
      var argall = "";
      for(var j=1;j<i;j++){
        argall= JSON.stringify(arg);
      }
      console.log(argall);
      return argall;
    },
  }
}

function LogCollect(logsave){
  var date = new Date();
  var logArray = new Array(2048);
  var movex = $("#movex").val();
  var movey = $("#movey").val();
  var movez = $("#movez").val();
  var movea = $("#movea").val();
  var spotSize = $("#spotSize").val();
  var magnificationA = $("#magnificationA").val();
  var magnificationR = $("#magnificationR").val();
  var binning = $("#binnig").val();
  var size = $("#size").val();
  var exptime = $("#exptime").val();
  var Year = date.getFullYear();
  var Month = date.getMonth() + 1;
  var day = date.getDate();
  var Hours = date.getHours();
  var Minutes = date.getMinutes();
  var Seconds = date.getSeconds();
  var  data = Year.toString() +'_'+ Month.toString() +'_'+ day.toString()+'_'+Hours.toString()+'_'+Minutes.toString()+'_'+Seconds.toString();

  var log =
  { "time":data ,
   "x":movex ,
   "y":movey ,
   "z":movez ,
   "a":movea ,
   "imageSize":size ,
   "binning":binning ,
   "exptime":exptime ,
   "spotSize":spotSize ,
   "magR":magnificationA ,
   "magA":magnificationR ,
  };

  //logArray[i] = log;
//  i=i++;
console.log(log);
    logArray.push(log);

    console.log(logArray[2048]);
    LogMemory(log);
    LoadLogMemory.LogMemoryInstance(log);
/*  if(logsave == 0){
    logArray.push(log);
  LogSimplePhotoCondtionText(logArray,data);
}*/
}

function LogSimplePhotoCondtionText(logArray,data){
  //ファイル名を取得、デフォルトは「mytext」
  //      var name = document.getElementById('saveEmConditionFileName').value = 'mytext';
  //テキストファイルを作成
  var blob = new Blob([logArray], {type: 'text/plain'});
  //リンクを取得
  var linkcondition = document.getElementById('DL_link_condition');
  //リンクにダウンロードするファイルをセット
  linkcondition.href = window.URL.createObjectURL(blob);
  //もしダウンロード属性に対応していたら
  if ('download' in linkcondition) {
    var Data = UrlElement['sendManufacturer'] + "_Log_" + data ;
    //リンクにダウンロードする名前をセット
    linkcondition.download = Data + '.txt';
    //リンクをクリックしてダウンロードさせる
    linkcondition.click();
    //もし対応してなかったら
  } else {
    //メッセージを表示する
    linkcondition.textContent = '右クリックから名前を付けて保存してください';
  }
}

$(function(){
  $("#logsave").click(function(){
      var date = new Date();
    var Year = date.getFullYear();
    var Month = date.getMonth() + 1;
    var day = date.getDate();
    var data = Year.toString() +''+ Month.toString() +''+ day.toString();
    LogSimplePhotoCondtionText(JSON.stringify(GETLogMemory),data);
  });
});
