/**************************
 * Toggle Feature Buttons
 **************************/
function togglePen() {
  console.log("peacekim:: share-bind-button-action - togglepend()");
  $("#coviewPenCtrBtn").click();
  if ($("#penBtn .pen").hasClass("disable")) {
    $("#penBtn .pen").removeClass("disable");
  } else {
    $("#penBtn .pen").addClass("disable");
  }
}

function penOff() {
  console.log("peacekim:: share-bind-button-action - penOff()");
  if (!$("#penBtn .pen").hasClass("disable")) {
    $("#coviewPenCtrBtn").click();
    $("#penBtn .pen").addClass("disable");
  }
}

function togglePhotoOpenPlaceHolder() {
  console.log(
    "peacekim:: share-bind-button-action - togglePhotoOpenPlaceHolder()"
  );
  $("#open_file_upload").trigger("click");
}

function toggleEraser() {
  console.log("peacekim:: share-bind-button-action - toggleEraser()");
  $("#coviewEraserCtrBtn").click();
}

function toggleCapture() {
  console.log("peacekim:: share-bind-button-action - toggleCapture()");
  if ($("#recordBtn").hasClass("bg_red")) {
    screenLock();
    recordStop(function () {
      $("#screenLock").remove();
      coview_api.Capture(serverInfo.cmsURL + "/chatapi/file/uploadArchive");
    });
  } else {
    coview_api.Capture(serverInfo.cmsURL + "/chatapi/file/uploadArchive");
  }
}

function toggleCaptureRequest() {
  console.log("peacekim:: share-bind-button-action - toggleCaptureRequest()");
  FermiWebSocketBridge.captureRequest();
}

function micOn() {
  console.log("peacekim:: share-bind-button-action - micOn()");
  coview_api.MicMuteControl("off");
  $("#micBtn .voice").removeClass("disable");
}

function micOff() {
  console.log("peacekim:: share-bind-button-action - micOff()");
  coview_api.MicMuteControl("on");
  $("#micBtn .voice").addClass("disable");
}

function toggleMic() {
  console.log("peacekim:: share-bind-button-action - toggleMic()");
  if ($("#micBtn .voice").hasClass("disable")) {
    micOn();
  } else {
    micOff();
  }
}

/*************
 * レコード関連
 *************/
function recordStart() {
  console.log("peacekim:: share-bind-button-action - recordStart()");
  MainManRecord("on");
  $("#recordBtn .record").addClass("disable");
  $(".flash-txt").addClass("on");
}

function recordFinished() {
  console.log("peacekim:: share-bind-button-action - recordFinished()");
  $("#recordBtn .record").removeClass("disable");
  $(".flash-txt").removeClass("on");
  // ロック画面の削除
  deleteDOMObject("screenLock");
}

function toggleRecord() {
  console.log("peacekim:: share-bind-button-action - toggleRecord()");
  if (!$("#recordBtn .record").hasClass("disable")) {
    recordStart();
  } else {
    recordStop(function () {
      console.info("Did record stop");
    });
  }
}

function recordStop(callback) {
  console.log("peacekim:: share-bind-button-action - recordStop()");
  if (!$("#recordBtn .record").hasClass("disable")) {
    callback();
    return;
  }
  Common.showLoadingIndicator();
  // アーカイブ保存中は画面操作不可(協業終了,協業切り替え防止)
  screenLock();

  // アーカイブ保存処理
  mainManRecordWithCollaboration(
    "stop",
    serverInfo.cmsURL + "/chatapi/file/uploadArchive",
    callback
  );
}

function mainManRecordWithCollaboration(action, url, callback) {
  console.log(
    "peacekim:: share-bind-button-action - mainManRecordWithCollaboration()"
  );
  try {
    mediaRecorder.stop();
  } catch (exeption) {
    console.error("Record failed");
    callback();
  }

  setTimeout(function () {
    const blob = new Blob(recordedBlobs, { type: "video/webm" });
    const uploadFileName = "record_" + g_webroom + "_" + g_shareCount + ".webm";
    g_shareCount++;
    let formData = new FormData();
    formData.append("fileData", blob, uploadFileName);
    formData.append("sid", globalUserInfo.sid);
    formData.append("roomId", globalUserInfo.roomId);
    let collaborationType =
      globalUserInfo.collaborationType == COLLABORATION_TYPE.VIDEO
        ? ARCHIVE_TYPE.VIDEO
        : ARCHIVE_TYPE.VOICE;
    formData.append("archiveType", collaborationType);

    postRecords(url, formData, callback);
  }, 1000);
}

function postRecords(url, formData, callback) {
  $.ajax({
    type: "post",
    url: url,
    data: formData,
    contentType: false,
    processData: false,
    success: function (res) {
      recordFinished();
      Common.dismissLoadingIndicator();
      callback();
    },
    error: function (err) {
      recordFinished();
      Common.dismissLoadingIndicator();
      callback();
    },
  });
}

// div削除関数
function deleteDOMObject(idName) {
  const domObj = document.getElementById(idName);
  const domObjParent = domObj.parentNode;
  domObjParent.removeChild(domObj);
}
