/**************************
 * 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().then(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().then(function () {
      console.info("Did record stop");
    });
  }
}

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

    // アーカイブ保存処理
    mainManRecordStopWithCollaboration().then(function () {
      done();
    });
  });
}

async function sendRecordedData() {
  return new Promise(function (done) {
    // get lastest message id and update message from server via native
    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(formData).then(function () {
      done();
    });
  });
}

function mainManRecordStopWithCollaboration() {
  return new Promise(function (done) {
    try {
      mediaRecorder.stop();
    } catch (exeption) {
      console.error("Record stop failed");
      done();
    }
    sendRecordedData().then(function () {
      done();
    });
  });
}

function postRecords(formData) {
  return new Promise(function (done) {
    $.ajax({
      type: "post",
      url: serverInfo.cmsURL + "/chatapi/file/uploadArchive",
      data: formData,
      contentType: false,
      processData: false,
      success: function (res) {
        recordFinished();
        Common.dismissLoadingIndicator();
        done();
      },
      error: function (err) {
        recordFinished();
        Common.dismissLoadingIndicator();
        done();
      },
    });
  });
}

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