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 (serverInfo.isOnline != "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 (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
) {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.joinCollaboration.postMessage({
      collaborationType: collaborationType,
      meetingId: meetingId,
    });
  } else if (deviceInfo.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.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) {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.startCollaboration.postMessage(type);
  } else if (deviceInfo.isAndroid()) {
    android.startCollaboration(type);
  }
};

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
) {
  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 (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 (
  joinCollaborationType,
  meetingID
) {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.joinChangedCollaboration.postMessage({
      joinCollaborationType: joinCollaborationType,
      meetingId: meetingID,
    });
  } else if (deviceInfo.isAndroid()) {
    android.joinChangedCollaboration(joinCollaborationType, meetingID);
  }
};

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

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

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 (type, meetingID) {
  if (deviceInfo.isiOS()) {
    webkit.messageHandlers.changeCollaboration.postMessage({
      changeCollaborationType: type,
      meetingId: meetingID,
    });
  } else if (deviceInfo.isAndroid()) {
    android.changeCollaboration(type, meetingID);
  }
};


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

// for android call
ReverseDelegate.displayExistRoom = function (roomId) {
  if (confirm("error_already_exist_same_user")) {
    NativeBridgeDelegate.joinRoom(roomId);
  }
  return;
};
