var NativeBridgeDelegate = {};

NativeBridgeDelegate.updateRoomList = function () {
  if (typeof android != "undefined") {
    android.updateRoomList();
  } else {
    webkit.messageHandlers.updateRoomList.postMessage({});
  }
};

NativeBridgeDelegate.roomDisplayOff = function () {
  if (typeof android != "undefined") {
    android.roomDisplayOff();
  } else {
    webkit.messageHandlers.roomDisplayOff.postMessage({});
  }
};

NativeBridgeDelegate.openCommunicationHome = function () {
  if (typeof android != "undefined") {
    android.openCommunicationHome();
  } else {
    webkit.messageHandlers.openCommunicationHome.postMessage({});
  }
};

NativeBridgeDelegate.joinRoom = function (roomID, roomName = "") {
  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
) {
  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 () {
  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) {
  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
) {
  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);
  }
};

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

NativeBridgeDelegate.saveSelectedUserList = function (selectedUserList) {
  if (typeof android != "undefined") {
    android.saveSelectedUserList(selectedUserList);
  } else {
    webkit.messageHandlers.saveSelectedUserList.postMessage(selectedUserList);
  }
};

NativeBridgeDelegate.startPIPMode = function () {
  if (typeof android != "undefined") {
    android.startPIPMode();
  } else if (deviceInfo.isiOS()) {
    webkit.messageHandlers.startPipMode.postMessage({});
  }
};

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

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

NativeBridgeDelegate.setToMoveGroupId = function (groupID) {
  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) {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.updateArchiveDetail.postMessage(archiveId);
  } else if (deviceInfo.isAndroid()) {
    android.updateArchiveDetail(archiveId);
  }
};

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

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

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

NativeBridgeDelegate.startCollaboration = function (type) {
  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) {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.finishCollaboration.postMessage({});
  } else if (needsAndroid) {
    android.finishCollaboration();
  }
};

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

NativeBridgeDelegate.inviteCollaboration = function (
  userIDList,
  collaborationType
) {
  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 () {
  if (typeof android != "undefined") {
    android.clearBeforeRoomType();
  } else {
    webkit.messageHandlers.clearBeforeRoomType.postMessage({});
  }
};

NativeBridgeDelegate.updateMessages = function (roomID) {
  if (typeof android != "undefined") {
    android.updateMessages(roomID);
  } else {
    webkit.messageHandlers.updateMessages.postMessage(roomID);
  }
};

NativeBridgeDelegate.saveVisitRoomInfo = function (roomID = "", roomName = "") {
  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 () {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.exitMeetingRoom.postMessage({});
  } else if (deviceInfo.isAndroid()) {
    android.exitMeetingRoom();
  }
};

NativeBridgeDelegate.joinChangedCollaboration = function (
  collaborationType,
  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) {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.joinMeetingRoom.postMessage(meetingID);
  } else if (deviceInfo.isAndroid()) {
    android.joinMeetingRoom(meetingID);
  }
};

NativeBridgeDelegate.setJoinCollaborationType = function (type) {
  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 () {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.showExitHostAlert.postMessage({});
  }
  // TODO: peacekim:: check not to need this method for android
};

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

NativeBridgeDelegate.changeCollaboration = function (
  collaborationType,
  meetingID
) {
  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 () {
  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 () {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.hideLoadingIndicator.postMessage({});
  } else if (deviceInfo.isAndroid()) {
    android.hideLoadingIndicator();
  }
};

var ReverseDelegate = {};
/* reverse delegates  */
// for android native call
ReverseDelegate.leaveRoom = function () {
  CHAT_SOCKET.emitLeaveRoom();
};

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