/**************************
 * Toggle Feature Buttons
 **************************/
function togglePen() {
  $("#coviewPenCtrBtn").click();
  if ($("#penBtn .pen").hasClass("disable")) {
    $("#penBtn .pen").removeClass("disable");
  } else {
    $("#penBtn .pen").addClass("disable");
  }
}

function penOff() {
  if (!$("#penBtn .pen").hasClass("disable")) {
    $("#coviewPenCtrBtn").click();
    $("#penBtn .pen").addClass("disable");
  }
}

function togglePhotoOpenPlaceHolder() {
  $("#open_file_upload").trigger("click");
}

function toggleEraser() {
  $("#coviewEraserCtrBtn").click();
}

function 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() {
  FermiWebSocketBridge.captureRequest();
}

function micOn() {
  coview_api.MicMuteControl("off");
  $("#micBtn .voice").removeClass("disable");
}

function micOff() {
  coview_api.MicMuteControl("on");
  $("#micBtn .voice").addClass("disable");
}

function toggleMic() {
  if ($("#micBtn .voice").hasClass("disable")) {
    micOn();
  } else {
    micOff();
  }
}

/*************
 * レコード関連
 *************/
function recordStart() {
  MainManRecord("on");
  $("#recordBtn .record").addClass("disable");
  $(".flash-txt").addClass("on");
}

function recordFinished() {
  $("#recordBtn .record").removeClass("disable");
  $(".flash-txt").removeClass("on");
  // ロック画面の削除
  deleteDOMObject("screenLock");
}

function toggleRecord() {
  if (!$("#recordBtn .record").hasClass("disable")) {
    recordStart();
  } else {
    recordStop(function () {
      console.info("Did record stop");
    });
  }
}

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

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

function mainManRecordWithCollaboration(action, url, callback) {
  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);
    if (globalUserInfo.collaborationType == COLLABORATION_TYPE.VIDEO) {
      formData.append("archiveType", 1);
    } else {
      formData.append("archiveType", 2);
    }
    $.ajax({
      type: "post",
      url,
      data: formData,
      contentType: false,
      processData: false,
      success: function (res) {
        recordFinished();
        Common.dismissLoadingIndicator();
        callback();
      },
      error: function (err) {
        recordFinished();
        Common.dismissLoadingIndicator();
        callback();
      },
    });
  }, 1000);
}

function postRecords() {}

// div削除関数
function deleteDOMObject(id_name) {
  const dom_obj = document.getElementById(id_name);
  const dom_obj_parent = dom_obj.parentNode;
  dom_obj_parent.removeChild(dom_obj);
}
