var ws ;
var i = 0;
var toggleSwitch =0;
var params = {
  size : 0,
  binninb : 1,
  exptime : 1
};
// インスタンス化
var userSetting = new GetUrlElement();
var UrlElement = userSetting.GetUrlElementInstance();
var hello = userSetting.sayHello;
console.log(UrlElement['sendIpAddress']);

/*function
wsConnectionSwitch();
apiConnectionSwitch();
setParams();
settings()
connection()
image()
simplePhotoCamera()

*/



function wsConnectionSwitch(){
  setParams();
  var data;
  toggleSwitch ^= 1 ;
  switch(toggleSwitch){
    case 1:
 ws = new WebSocket("ws://"+UrlElement['sendIpAddress']+":3001");
  ws.onopen = function(){
    console.log('websocket connected')
  //  ws.send('req');
  };
  document.getElementById("wsConnectBtn").value = "websocket close";
  ws.onmessage = function(ture){
    data = JSON.parse(ture.data)
    console.log('server:' +data["type"]);
  switch(data["type"]){
    case "connection":
    connection(data);
    break;

    case "img":
    image(data);
    break;
  }
};
  break;

  case 0:
    ws.close();
    console.log('closed');
    document.getElementById("wsConnectBtn").value = "websocket start";
    break;
  }
}

function apiConnectionSwitch(){
toggleSwitch ^= 1;
switch(toggleSwitch){
  case 1:
  //start
  $.ajax({
    type:"GET",
    url:"http://" +UrlElement['sendIpAddress']+
    ":"+UrlElement['sendPortNumber']+"/"+UrlElement['sendManufacturer']+
    "/set/",
    success: function(data){
      console.log(data);//string型
    }
  });
  document.getElementById("apiConnectBtn").value = "API close";
  break;

  case 0:
//stop
  $.ajax({
    type:"GET",
    url:"http://" +UrlElement['sendIpAddress']+
    ":"+UrlElement['sendPortNumber']+"/"+UrlElement['sendManufacturer']+
    "/set/",
    success: function(data){
      console.log(data);//string型
    }
  });
  document.getElementById("apiConnectBtn").value = "API start";
  break;
};
}



function setParams(){
  settings();
}

function settings(){
  params["size"] = $( "#size option:selected" ).val();
  var exptime = $( "#exptime" ).val();
  if( 1 >= exptime && exptime >= 0.01){
    params["exptime"] = exptime;
    params["binning"] = $( "#binning option:selected" ).val();
  }
  $.ajax({
    type:"GET",
    url:"http://"+UrlElement["sendipAddress"]+":"+UrlElement["sendPortNumber"]+"/"+UrlElement["sendManufacturer"]+"/set/acq_init?bng="+params["binning"]+"&&exp="+params["exptime"]+"&&size="+params["size"],
    success: function(data){
    }
  });
}

function connection(data){
ws.send('{"type":"req", "data":"img"}');
}

function image(data){
  ws.send('{"type":"req", "data":"img"}');
  drawMycrograph(data.data);
}


function simplePhotoCamera(){
  var simplePhotoImage = $("canvasOne").val();

}

window.onload=canvasMap;
function canvasMap(){
  var canvas = document.getElementById("canvasMap");
  var ctx = canvas.getContext("2d");
  var x = $("#movex").val();
  var y = $("#movey").val();
  var z = $("#movez").val();
  var a = $("#movea").val();

  ctx.beginPath();
  ctx.fillStyle = "#808080";
  ctx.arc(100,100,100,0,Math.PI*2,true);
  ctx.fill();
  ctx.closePath();


  canvas.addEventListener("mousedown",function(e){
    $.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);
      }
    });
  });
}

/*
function canvasMapDraw(){
  var step = (1/2000);//倍率変化はここの通知を変化
  var canvas = document.getElementById("canvasMap");
  var ctx = canvas.getContext("2d");
  var x = $("#movex").val();
  var y = $("#movey").val();
  if(x == 0 || y==0){
  ctx.beginPath();
  ctx.fillStyle = "#000000";
  ctx.moveTo(100,100);
  ctx.lineTo((parseInt(x)*step+100,-(parseInt(y)*step)+100);
  //ctx.lineTo(150,100)
  ctx.closePath();
  ctx.stroke();
  moveX = (parseInt(x)*step)+100;
  moveY = -(parseInt(y)*step)+100;
}
  else if(x !=0 || y!= 0){
  ctx.beginPath();
  ctx.fillStyle = "#000000";
  ctx.moveTo(moveX,moveY);
  ctx.lineTo((parseInt(x)*step+100,-(parseInt(y)*step)+100);
  //ctx.lineTo(150,100)
  ctx.closePath();
  ctx.stroke();
  moveX = (parseInt(x)*step)+100;
  moveY = -(parseInt(y)*step)+100;
}
}
*/

/*window.onload = drawCanvasMap;
function drawCanvasMap(){
  var drawCanvas = document.getElementById("canvasMap");
  var ctx = drawCanvas.getContext("2D");
//  if(canvas.getContext){
  var x = $("#movex").val();
  var y = $("#movey").val();
  ctx.fillStyle = "#000000";
  ctx.beginPath();
  ctx.moveTo(parseInt(x)+100,parseInt(y)+100);
  ctx.lineTo(150,100);
  ctx.closePath();
  ctx.stroke();
//}
}
*/
