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
) {
  //todo android create room api
  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 (IS_ONLINE != "true") {
    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 (IS_ONLINE == "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
) {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.joinCollaboration.postMessage({
      collaborationType: collaborationType,
      meetingId: meetingId,
    });
  } else if (CHAT_UTIL.isAndroid()) {
    if (
      collaborationType == COLLABORATION_TYPE.DOCUMENT &&
      androidVersion < ANDROID_SDK_VERSION.O
    ) {
      alert(getLocalizedString("not_support_version"));
      return;
    }
    android.joinCollaboration(collaborationType, meetingId);
  }
};

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

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

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

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 (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.startPipMode.postMessage({});
  }
};

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

NativeBridgeDelegate.setHostRequestFlg = function (hostRequestFlag) {
  if (CHAT_UTIL.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 (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.updateArchiveList.postMessage({});
  } else if (CHAT_UTIL.isAndroid()) {
    android.updateArchiveList();
  }
};

NativeBridgeDelegate.updateArchiveDetail = function (archiveId) {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.updateArchiveDetail.postMessage(archiveId);
  } else if (CHAT_UTIL.isAndroid()) {
    android.updateArchiveDetail(archiveId);
  }
};

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

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

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

NativeBridgeDelegate.startCollaboration = function (type) {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.startCollaboration.postMessage(type);
  } else if (CHAT_UTIL.isAndroid()) {
    android.startCollaboration(type);
  }
};

NativeBridgeDelegate.finishCollaboration = function (needsAndroid = false) {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.finishCollaboration.postMessage({});
  } else if (needsAndroid) {
    android.finishCollaboration();
  }
};

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

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

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 (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.roomInfosaveMessageHandlerId.postMessage({
      roomId: roomID,
      roomName: roomName,
    });
  } else if (CHAT_UTIL.isAndroid()) {
    if (roomId == undefined && roomName == undefined) {
      android.saveVisitRoomInfo("", "");
    } else {
      android.saveVisitRoomInfo(roomID, roomName);
    }
  }
};

NativeBridgeDelegate.exitMeetingRoom = function () {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.exitMeetingRoom.postMessage({});
  } else if (CHAT_UTIL.isAndroid()) {
    android.exitMeetingRoom();
  }
};

NativeBridgeDelegate.joinChangedCollaboration = function (
  joinCollaborationType,
  meetingID
) {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.joinChangedCollaboration.postMessage({
      joinCollaborationType: joinCollaborationType,
      meetingId: meetingID,
    });
  } else if (CHAT_UTIL.isAndroid()) {
    android.joinChangedCollaboration(joinCollaborationType, meetingID);
  }
};

NativeBridgeDelegate.joinMeetingRoom = function (meetingID) {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.joinMeetingRoom.postMessage(meetingID);
  } else if (CHAT_UTIL.isAndroid()) {
    android.joinMeetingRoom(meetingID);
  }
};

NativeBridgeDelegate.setJoinCollaborationType = function (
  type,
  androidVersion
) {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.setJoinCollaborationType.postMessage(type);
  } else if (CHAT_UTIL.isAndroid()) {
    if (
      type == COLLABORATION_TYPE.DOCUMENT &&
      androidVersion < ANDROID_SDK_VERSION.O
    ) {
      alert(getLocalizedString("not_support_version"));
      return;
    }
    android.setJoinCollaborationType(type);
  }
};

NativeBridgeDelegate.showExitHostAlert = function () {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.showExitHostAlert.postMessage({});
  }
  // TODO: peacekim:: check not to need this method for android
};

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

NativeBridgeDelegate.changeCollaboration = function (type, meetingID) {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.changeCollaboration.postMessage({
      changeCollaborationType: type,
      meetingId: meetingID,
    });
  } else if (CHAT_UTIL.isAndroid()) {
    android.changeCollaboration(type, meetingID);
  }
};
