var NativeBridge = {};

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

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

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

NativeBridge.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,
    });
  }
};

NativeBridge.createChatRoom = function (
  chatRoomType,
  userIDList,
  newRoomName,
  screenFlag,
  isVoice
) {
  //todo android create room api
  if (typeof android != "undefined") {
    android.createChatRoom(
      chatRoomType,
      userIDList.join(","),
      newRoomName,
      screenFlag,
      isVoice
    );
  } else {
    webkit.messageHandlers.createChatRoom.postMessage({
      roomType: chatRoomType,
      userIdList: userIDList.join(","),
      roomName: newRoomName,
      screenFlag: screenFlag,
      isVoice: isVoice,
    });
  }
};

NativeBridge.updateContactInfo = function () {
  if (IS_ONLINE != "true") {
    return;
  }
  NativeBridge.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({});
  }
};

NativeBridge.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);
    }
  }
};

// todo: peacekim:: it should be datasource
NativeBridge.removeFavoriteGroup = function (groupID) {
  if (CHAT_UTIL.isIOS()) {
    const result = callNativeApp(NATIVE_KEY_IOS.removeFavoriteGroup, {
      groupId: groupID,
    });
    return result == "true" ? true : false;
  } else {
    return android.removeFavoriteGroup(groupID);
  }
};

NativeBridge.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);
  }
};

NativeBridge.joinRoom = function () {
  if (CHAT_UTIL.isIOS()) {
    webkit.messageHandlers.joinRoom.postMessage({ roomId: roomId });
  } else if (CHAT_UTIL.isAndroid()) {
    android.joinRoom(roomId, "");
  }
};

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

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

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

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