// インスタンス化
var userSetting = new GetUrlElement();
var UrlElement = userSetting.GetUrlElementInstance();
var hello = userSetting.sayHello;
console.log(UrlElement['sendIpAddress']);

var LoadLogMemory = new LogMemory();
var GETLogMemory = LoadLogMemory.LogMemoryInstance();
var MemoryAll = LoadLogMemory.GETLog();

/*
getCurrentParams()
clickMove()
setStage()
setSpotSize()
setMagnificationA()
setMagnificationR()
correntConditionText()
setSpotSize()
setMagnificationR()
setMagnificationA()
nowCondition()
*/


function getCurrentParams(){
  var parameter;
console.log("getcurrentparams");
  $.ajax({
    type:"GET",
    url:"//"+UrlElement['sendIpAddress']+
        ":"+UrlElement['sendPortNumber']+
        "/"+UrlElement['sendElectronMicroscopeName']+
        "/get",
//    url:"http://192.168.4.146:9090/fei/get",
    success: function(data){
      console.log(data);
    }
  });
}

function clickMove(direction){
  var movingdistance = $("#movingdistance").val();
  var movexr = $("#movex").val();
  var moveyr = $("#movey").val();
  var movezr = $("#movez").val();
  var directionSwitch = direction;

console.log("movingdistance="+movingdistance);
console.log("movex="+movexr);
console.log("movey="+moveyr);

switch(directionSwitch){
      case "Up":
      moveyr = parseInt(moveyr) + (parseInt(movingdistance));
      $("#movey").val(String(moveyr));
      break;

    case "Down":
      moveyr = parseInt(moveyr) - (parseInt(movingdistance));
      $("#movey").val(String(moveyr));
    break;

    case "Left":
      movexr = parseInt(movexr) + (parseInt(movingdistance));
      $("#movex").val(String(movexr));
    break;

    case "Right":
    movexr = parseInt(movexr) - (parseInt(movingdistance));
    $("#movex").val(String(movexr));
    $("#currentx").val(String(movexr));
    break;
    }
    $.ajax({
      type:"GET",
      url:"http://" +UrlElement['sendIpAddress']+
      ":"+UrlElement['sendPortNumber']+"/"+UrlElement['sendManufacturer']+
      "/set/stagea?x="+encodeURIComponent(movexr)+
      "&y="+encodeURIComponent(moveyr)+
      "&z="+encodeURIComponent(movezr)+
      "&a="+encodeURIComponent(movezr),
      success: function(data){
        console.log(data);//string型
      }
    });
  }

function setStage(){
  var movingdistance = $("#movingdistance").val();
  var movexr = $("#movex").val();
  var moveyr = $("#movey").val();
  var movezr = $("#movez").val();

  $("#movey").keydown(function(e){
    console.log(e.keyCode);
    console.log(typeof(e.keyCode));

    //UP(↑,w,h)
    if (e.keyCode == 38 || e.keyCode == 87 || e.keyCode == 72) {
        moveyr = parseInt(moveyr) + (parseInt(movingdistance)-1);
        $("#movey").val(String(moveyr));
    }
    //DOWN(↓,s,j)
    else if (e.keyCode == 40 || e.keyCode == 83 || e.keyCode == 74) {
      moveyr = parseInt(moveyr) - (parseInt(movingdistance)-1);
      $("#movey").val(String(moveyr));
    }
  });

  $("#movex").keydown(function(e){
    console.log(e.keyCode);
    console.log(typeof(e.keyCode));
    //RIGHT(→,a,k)
    if (e.keyCode == 39 || e.keyCode == 68 || e.keyCode == 76) {
    movexr = parseInt(movexr) + (parseInt(movingdistance)-1);
    $("#movex").val(String(movexr));
    }
    //LEFT(←,d,l)
    else if (e.keyCode == 37 || e.keyCode == 65 || e.keyCode == 75) {
      movexr = parseInt(movexr) - (parseInt(movingdistance)-1);
      $("#movex").val(String(movexr));
    }
  });

  $.ajax({
    type:"GET",
    //電子顕微鏡通信時
//    url:"http://192.168.4.146:9090/fei/set/stagea?x="+encodeURIComponent(movexr)+
    url:"http://"+UrlElement['sendIpAddress']+
    ":"+UrlElement['sendPortNumber']+
    "/"+UrlElement['sendManufacturer']+
    "/set/stagea?x="+encodeURIComponent(movexr)+
    "&y="+encodeURIComponent(moveyr)+
    "&z="+encodeURIComponent(movezr)+
    "&a="+encodeURIComponent(movezr),
    success: function(data){
      console.log(data);
    }
  });
}

function currentConditionText(){
  var movingdistance = $("#movingdistance").val();
  var movexr = $("#movex").val();
  var moveyr = $("#movey").val();
  var movezr = $("#movez").val();
  console.log("currentcondition");
  $("#Currentcondition").innerHTML='x:"movexr" y:"moveyr" z:"movezr"'
}

function setSpotSize(){
  var setSpotSize = $("#SpotSize").val();
  $.ajax({
    type:"GET",
    url:"http://"+UrlElement['sendIpAddress']+
    ":"+UrlElement['sendPortNumber']+
    "/"+UrlElement['sendManufacturer']+
    "/set/spotsizea?sps="+encodeURIComponent(setSpotSize),
    success: function(data){
      console.log(data);
    }
  });
}

function setMagnificationA(){
  var magnificationA = $("#magnificationA").val();
  $.ajax({
    type:"GET",
    url:"http://"+UrlElement['sendIpAddress']+
    ":"+UrlElement['sendPortNumber']+
    "/"+UrlElement['sendManufacturer']+
    "/set/magnificationa?mag="+encodeURIComponent(magnificationA),
    success: function(data){
      console.log(data);
    }
  });
}
function setMagnificationR(){
  var magnificationR = $("#magnificationR").val();
  $.ajax({
    type:"GET",
    url:"http://"+UrlElement['sendIpAddress']+
    ":"+UrlElement['sendPortNumber']+
    "/"+UrlElement['sendManufacturer']+
    "/set/magnificationr?mag="+encodeURIComponent(magnificationR),
    success: function(data){
      console.log(data);
    }
  });
}

function nowCondition(){
  var movexr = $("#movex").val();
  var moveyr = $("#movey").val();
  var movezr = $("#movez").val();
  var movea = $("#movea").val();
  var spotSize = $("#spotSize").val();
  var exptime = $("#exptime").val();
  var manufacturer = UrlElement['sendElectronMicroscopeName'];
  document.getElementById("tem").innerHTML = manufacturer;
  document.getElementById("currentx").innerHTML = parseInt(movexr);
  document.getElementById("currenty").innerHTML = parseInt(moveyr);
  document.getElementById("currentz").innerHTML = parseInt(movezr);
  document.getElementById("currenta").innerHTML = parseInt(movea);
  document.getElementById("SpotSizeIndex").innerHTML = parseInt(spotSize);
  document.getElementById("MagIndex").innerHTML = parseInt(magnificationA);

}
