var NativeBridgeDelegate = {};

NativeBridgeDelegate.updateRoomList = function () {
  console.debug("call Native Delegate - updateRoomList");
  if (typeof android != "undefined") {
    android.updateRoomList();
  } else {
    webkit.messageHandlers.updateRoomList.postMessage({});
  }
};

NativeBridgeDelegate.roomDisplayOff = function () {
  console.debug("call Native Delegate - roomDisplayOff");
  if (typeof android != "undefined") {
    android.roomDisplayOff();
  } else {
    webkit.messageHandlers.roomDisplayOff.postMessage({});
  }
};

NativeBridgeDelegate.openCommunicationHome = function () {
  console.debug("call Native Delegate - openCommunicationHome");
  if (typeof android != "undefined") {
    android.openCommunicationHome();
  } else {
    webkit.messageHandlers.openCommunicationHome.postMessage({});
  }
};

NativeBridgeDelegate.joinRoom = function (roomID, roomName = "") {
  console.debug("call Native Delegate - joinRoom");

  if (typeof android != "undefined") {
    try {
      android.joinRoom(roomID, roomName);
    } catch {
      alert(getLocalizedString("err_not_exist_room"));
    }
  } else {
    webkit.messageHandlers.joinRoom.postMessage({
      roomId: roomID,
      roomName: roomName,
    });
  }
};

NativeBridgeDelegate.createChatRoom = function (
  chatRoomType,
  userIDList,
  newRoomName,
  screenFlag,
  isVoice
) {
  console.debug("call Native Delegate - createChatRoom");
  if (typeof android != "undefined") {
    android.createChatRoom(
      chatRoomType,
      userIDList,
      newRoomName,
      screenFlag,
      isVoice
    );
  } else {
    webkit.messageHandlers.createChatRoom.postMessage({
      roomType: chatRoomType,
      userIdList: userIDList,
      roomName: newRoomName,
      screenFlag: screenFlag,
      isVoice: isVoice,
    });
  }
};

NativeBridgeDelegate.updateContactInfo = function () {
  console.debug("call Native Delegate - updateContactInfo");
  if (serverInfo.isOnline == false) {
    return;
  }
  NativeBridgeDelegate.updateGroupInfo("0");
  if (typeof android != "undefined") {
    android.updateMyInfo();
    // android.updateGroupUser(); ? is this method included in update group info?
    android.updateFavorite();
  } else {
    webkit.messageHandlers.updateMyInfo.postMessage({});
    webkit.messageHandlers.updateGroupUser.postMessage({});
    webkit.messageHandlers.updateFavorite.postMessage({});
  }
};

NativeBridgeDelegate.updateGroupInfo = function (groupID) {
  console.debug("call Native Delegate - updateGroupInfo");
  if (serverInfo.isOnline == true) {
    if (typeof android != "undefined") {
      // TODO: peacekim:: check this method is right or not
      android.updateGroupUser();
    } else {
      webkit.messageHandlers.updateGroupInfo.postMessage(groupID);
    }
  }
};

NativeBridgeDelegate.joinCollaboration = function (
  collaborationType,
  meetingID = 0
) {
  console.debug("call Native Delegate - joinCollaboration");
  Common.showLoadingIndicator();
  const collaborationTypeNumber =
    CHAT_UTIL.getCollaborationTypeNumber(collaborationType);
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.joinCollaboration.postMessage({
      collaborationType: collaborationTypeNumber,
      meetingId: meetingID,
    });
  } else if (deviceInfo.isAndroid()) {
    if (
      collaborationType == COLLABORATION_TYPE.DOCUMENT &&
      deviceInfo.androidVersion < ANDROID_SDK_VERSION.O
    ) {
      alert(getLocalizedString("not_support_version"));
      return;
    }
    android.joinCollaboration(collaborationTypeNumber, meetingID);
  }
  Common.dismissLoadingIndicator();
};

NativeBridgeDelegate.inviteUsers = function (userIDList) {
  console.debug("call Native Delegate - inviteUsers");
  if (typeof android != "undefined") {
    android.inviteUsers(userIDList);
  } else {
    webkit.messageHandlers.inviteUsers.postMessage({
      userIdList: userIDList,
    });
  }
};

NativeBridgeDelegate.saveSelectedUserList = function (selectedUserList) {
  console.debug("call Native Delegate - saveSelectedUserList");
  if (typeof android != "undefined") {
    android.saveSelectedUserList(selectedUserList);
  } else {
    webkit.messageHandlers.saveSelectedUserList.postMessage(selectedUserList);
  }
};

NativeBridgeDelegate.startPIPMode = function () {
  console.debug("call Native Delegate - startPIPMode");
  if (typeof android != "undefined") {
    android.startPIPMode();
  } else if (deviceInfo.isiOS()) {
    webkit.messageHandlers.startPipMode.postMessage({});
  }
};

NativeBridgeDelegate.finishPIPMode = function (needsAndroid = false) {
  console.debug("call Native Delegate - finishPIPMode");
  // not for android
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.finishPipMode.postMessage({});
  } else if (needsAndroid && typeof android != "undefined") {
    android.finishPIP();
  }
};

NativeBridgeDelegate.setHostRequestFlg = function (hostRequestFlag) {
  console.debug("call Native Delegate - setHostRequestFlg");
  if (deviceInfo.isAndroid()) {
    android.setHostRequestFlg(hostRequestFlag);
  } else {
    webkit.messageHandlers.setHostRequestFlg.postMessage(hostRequestFlag);
  }
};

NativeBridgeDelegate.setToMoveGroupId = function (groupID) {
  console.debug("call Native Delegate - setToMoveGroupId");
  if (typeof android != "undefined") {
    android.setToMoveGroupId(groupID);
  } else {
    webkit.messageHandlers.setToMoveGroupId.postMessage(groupID);
  }
};

NativeBridgeDelegate.updateArchiveList = function () {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.updateArchiveList.postMessage({});
  } else if (deviceInfo.isAndroid()) {
    android.updateArchiveList();
  }
};

NativeBridgeDelegate.updateArchiveDetail = function (archiveId) {
  console.debug(
    "Native Delegate -BridgeDelegate.updateArchiveDetail called, archiveId: " +
      archiveId
  );
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.updateArchiveDetail.postMessage(archiveId);
  } else if (deviceInfo.isAndroid()) {
    android.updateArchiveDetail(archiveId);
  }
};

NativeBridgeDelegate.goHome = function () {
  console.debug("call Native Delegate - goHome");
  if (deviceInfo.isAndroid()) {
    android.goHome();
  } else if (deviceInfo.isiOS()) {
    webkit.messageHandlers.goHome.postMessage({});
  }
};

NativeBridgeDelegate.updatePreMessage = function (messageID) {
  console.debug("call Native Delegate - updatePreMessage");
  if (typeof android != "undefined") {
    if (serverInfo.isOnline == true) {
      android.updatePreMessage(messageID);
    }
  } else {
    // 追加メッセージUpdate、get
    webkit.messageHandlers.updatePreMessage.postMessage({
      messageId: messageID,
    });
  }
};

NativeBridgeDelegate.changeRoomName = function (newRoomName) {
  console.debug("call Native Delegate - changeRoomName");
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.changeRoomName.postMessage(newRoomName);
  } else if (deviceInfo.isAndroid()) {
    android.changeRoomName(newRoomName);
  }
};

NativeBridgeDelegate.startCollaboration = function (type) {
  console.debug("call Native Delegate - startCollaboration");
  const collaborationNumber = CHAT_UTIL.getCollaborationTypeNumber(type);
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.startCollaboration.postMessage(collaborationNumber);
  } else if (deviceInfo.isAndroid()) {
    android.startCollaboration(collaborationNumber);
  }
};

NativeBridgeDelegate.finishCollaboration = function (needsAndroid = false) {
  console.debug(
    "Native Delegate -BridgeDelegate.finishCollaboration called, needsAndroid: " +
      needsAndroid
  );
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.finishCollaboration.postMessage({});
  } else if (needsAndroid) {
    android.finishCollaboration();
  }
};

NativeBridgeDelegate.finishAllCollaboration = function () {
  console.debug("call Native Delegate - finishAllCollaboration");
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.finishAllCollaboration.postMessage({});
  } else if (deviceInfo.isAndroid()) {
    android.finishAllCollaboration();
  }
};

NativeBridgeDelegate.inviteCollaboration = function (
  userIDList,
  collaborationType
) {
  console.debug("call Native Delegate - inviteCollaboration");
  const collaborationNumber =
    CHAT_UTIL.getCollaborationTypeNumber(collaborationType);
  if (typeof android != "undefined") {
    android.inviteCollaboration(userIDList, collaborationNumber);
  } else {
    webkit.messageHandlers.inviteCollaboration.postMessage({
      userIdList: userIDList,
      collaborationType: collaborationNumber,
    });
  }
};

NativeBridgeDelegate.clearBeforeRoomType = function () {
  console.debug("call Native Delegate - clearBeforeRoomType");
  if (typeof android != "undefined") {
    android.clearBeforeRoomType();
  } else {
    webkit.messageHandlers.clearBeforeRoomType.postMessage({});
  }
};

NativeBridgeDelegate.updateMessages = function (roomID) {
  console.debug("call Native Delegate - updateMessages");
  if (typeof android != "undefined") {
    android.updateMessages(roomID);
  } else {
    webkit.messageHandlers.updateMessages.postMessage(roomID);
  }
};

NativeBridgeDelegate.saveVisitRoomInfo = function (roomID = "", roomName = "") {
  console.debug("call Native Delegate - saveVisitRoomInfo");
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.roomInfoSaveMessageHandlerId.postMessage({
      roomId: roomID,
      roomName: roomName,
    });
  } else if (deviceInfo.isAndroid()) {
    if (roomId == undefined && roomName == undefined) {
      android.saveVisitRoomInfo("", "");
    } else {
      android.saveVisitRoomInfo(roomID, roomName);
    }
  }
};

NativeBridgeDelegate.exitMeetingRoom = function () {
  console.debug("call Native Delegate - exitMeetingRoom");
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.exitMeetingRoom.postMessage({});
  } else if (deviceInfo.isAndroid()) {
    android.exitMeetingRoom();
  }
};

NativeBridgeDelegate.joinChangedCollaboration = function (
  collaborationType,
  meetingID
) {

  console.debug(
    "call Native Delegate - joinChangedCollaboration collaborationType: " +
      collaborationType +
      ", meetingID: " +
      meetingID
  );

  const collaborationNumber =
    CHAT_UTIL.getCollaborationTypeNumber(collaborationType);
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.joinChangedCollaboration.postMessage({
      joinCollaborationType: collaborationNumber,
      meetingId: meetingID,
    });
  } else if (deviceInfo.isAndroid()) {
    android.joinChangedCollaboration(collaborationNumber, meetingID);
  }
};

NativeBridgeDelegate.joinMeetingRoom = function (meetingID) {
  console.debug("call Native Delegate - joinMeetingRoom");
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.joinMeetingRoom.postMessage(meetingID);
  } else if (deviceInfo.isAndroid()) {
    android.joinMeetingRoom(meetingID);
  }
};

NativeBridgeDelegate.setJoinCollaborationType = function (type) {
  console.debug("call Native Delegate - setJoinCollaborationType");
  const collaborationNumber = CHAT_UTIL.getCollaborationTypeNumber(type);
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.setJoinCollaborationType.postMessage(
      collaborationNumber
    );
  } else if (deviceInfo.isAndroid()) {
    if (
      type == COLLABORATION_TYPE.DOCUMENT &&
      deviceInfo.androidVersion < ANDROID_SDK_VERSION.O
    ) {
      alert(getLocalizedString("not_support_version"));
      return;
    }
    android.setJoinCollaborationType(collaborationNumber);
  }
};

NativeBridgeDelegate.showExitHostAlert = function () {
  console.debug("call Native Delegate - showExitHostAlert");
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.showExitHostAlert.postMessage({});
  }
  // TODO: peacekim:: check not to need this method for android
};

NativeBridgeDelegate.startContentView = function () {
  console.debug("call Native Delegate - startContentView");
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.startContentView.postMessage({});
  } else if (deviceInfo.isAndroid()) {
    android.startContentView();
  }
};

NativeBridgeDelegate.changeCollaboration = function (
  collaborationType,
  meetingID
) {
  console.debug("call Native Delegate - changeCollaboration");
  const collaborationNumber =
    CHAT_UTIL.getCollaborationTypeNumber(collaborationType);
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.changeCollaboration.postMessage({
      changeCollaborationType: collaborationNumber,
      meetingId: meetingID,
    });
  } else if (deviceInfo.isAndroid()) {
    android.changeCollaboration(collaborationNumber, meetingID);
  }
};

NativeBridgeDelegate.showLoadingIndicator = function () {
  console.debug("call Native Delegate - showLoadingIndicator");
  if (deviceInfo.isiOS()) {
    try {
      webkit.messageHandlers.showLoadingIndicator.postMessage({});
    } catch (e) {
      console.error("failed to display loading indicator with error: " + e);
    }
  } else if (deviceInfo.isAndroid()) {
    android.showLoadingIndicator();
  }
};

NativeBridgeDelegate.hideLoadingIndicator = function () {
  console.debug("call Native Delegate - hideLoadingIndicator");
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.hideLoadingIndicator.postMessage({});
  } else if (deviceInfo.isAndroid()) {
    android.hideLoadingIndicator();
  }
};

var ReverseDelegate = {};
/* reverse delegates  */
// for android native call
ReverseDelegate.leaveRoom = function () {
  console.debug("call Native Delegate - leaveRoom");
  CHAT_SOCKET.emitLeaveRoom();
};

ReverseDelegate.displayExistRoom = function (roomID) {
  if (confirm("error_already_exist_same_user")) {
    NativeBridgeDelegate.joinRoom(roomID);
  }
  NativeBridgeDelegate.hideLoadingIndicator();
  return;
};
