// ios用
function callNativeApp(iosKey, jsonData) {
  console.log("callNativeApp called: "+iosKey);
  var result;
  try {
    var key = iosKey;
    var data = jsonData;
    var payload = { key: key, data: data };
    // payloadの形 {"type":"SJbridge","data":{"name":"abc","role":"dev"}}
    // resにObjective-cからのレスポンスが返る
    result = prompt(JSON.stringify(payload));
  } catch (err) {
    console.log("The native context does not exist yet");
  }
  return result;
}

var NativeBridgeDataSource = {};

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

NativeBridgeDataSource.addFavoriteGroup = function (groupID) {
  if (typeof android != "undefined") {
    return android.addFavoriteGroup(groupID);
  } else {
    const result = callNativeApp(NATIVE_KEY_IOS.addFavoriteGroup, {
      groupId: groupId,
    });
    if (result == "true") {
      return true;
    } else {
      return false;
    }
  }
};

NativeBridgeDataSource.getRoomType = function () {
  if (typeof android != "undefined") {
    return android.getRoomType();
  } else {
    return parseInt(callNativeApp(NATIVE_KEY_IOS.getRoomType, {}), 10);
  }
};

//ロカールDBからルーム一覧情報を取得
NativeBridgeDataSource.getRoomList = function (roomType, keyWord) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getRoomList, {
        roomType: roomType,
        keyWord: keyWord,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    return JSON.parse(android.getRoomList(roomType, keyWord));
  }
};

NativeBridgeDataSource.callGetRoomList = function (roomType) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getRoomList, { roomType: roomType })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getRoomList(roomType));
  }
};

//ロカールDBからログインしたユーザのデータを取得する。
NativeBridgeDataSource.getMyInfo = function () {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(callNativeApp(NATIVE_KEY_IOS.getMyInfo, {}));
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getMyInfo());
  }
};

NativeBridgeDataSource.getMessages = function (roomId) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getMessages, { roomId: roomId })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getMessageList(roomId));
  }
};

NativeBridgeDataSource.getMessagesWithMessageId = function (messageId) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getMessageListFromMessageId, {
        messageId: messageId,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    return JSON.parse(android.getMessageListFromMessageId(messageId));
  }
};

NativeBridgeDataSource.getFavoriteUsersNotInRoom = function () {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getFavoriteUsersNotInRoom, {})
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getFavoriteUsersNotInRoom());
  }
};

NativeBridgeDataSource.getFavoriteUsers = function () {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(callNativeApp(NATIVE_KEY_IOS.getFavoriteUsers, {}));
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getFavoriteUsers());
  }
};

NativeBridgeDataSource.getFavoriteGroups = function () {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(callNativeApp(NATIVE_KEY_IOS.getFavoriteGroups, {}));
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getFavoriteGroups());
  }
};

NativeBridgeDataSource.getMyGroupUsers = function () {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(callNativeApp(NATIVE_KEY_IOS.getMyGroupUsers, {}));
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getMyGroupUsers());
  }
};

NativeBridgeDataSource.getGroupInfo = function (groupId) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getGroupInfo, { groupId: groupId })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getGroupInfo(groupId));
  }
};

NativeBridgeDataSource.getGroupInfoForAddUser = function (groupId) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getGroupInfoForAddUser, { groupId: groupId })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getGroupInfoForAddUser(groupId));
  }
};

NativeBridgeDataSource.loadSelectedUsers = function () {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(callNativeApp(NATIVE_KEY_IOS.getSelectedUserList, {}));
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    console.log(JSON.parse(android.getSelectedUserList()));
    return JSON.parse(android.getSelectedUserList());
  }
};

NativeBridgeDataSource.getNameCardData = function (shopMemberId) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getNameCardData, {
        shopMemberId: shopMemberId,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    return JSON.parse(android.getNameCardData(shopMemberId));
  }
};

NativeBridgeDataSource.getMyGroupShopMemberByName = function (shopMemberName) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getMyGroupShopMemberByName, {
        shopMemberName: shopMemberName,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getMyGroupShopMemberByName(shopMemberName));
  }
};

NativeBridgeDataSource.getMyGroupShopMemberNotInRoomByName = function (
  shopMemberName
) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getMyGroupShopMemberNotInRoomByName, {
        shopMemberName: shopMemberName,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(
      android.getMyGroupShopMemberNotInRoomByName(shopMemberName)
    );
  }
};

NativeBridgeDataSource.getAllGroupShopMemberByName = function (shopMemberName) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getAllGroupShopMemberByName, {
        shopMemberName: shopMemberName,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getAllGroupShopMemberByName(shopMemberName));
  }
};

NativeBridgeDataSource.getAllGroupShopMemberNotInRoomByName = function (
  shopMemberName
) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getAllGroupShopMemberNotInRoomByName, {
        shopMemberName: shopMemberName,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(
      android.getAllGroupShopMemberNotInRoomByName(shopMemberName)
    );
  }
};

NativeBridgeDataSource.getGroupByName = function (groupName) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getGroupByName, { groupName: groupName })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getGroupByName(groupName));
  }
};

NativeBridgeDataSource.getUsersInRoom = function (roomId) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getUsersInRoom, { roomId: roomId })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getUsersInRoom(roomId));
  }
};

// アーカイブ一覧
NativeBridgeDataSource.getArchiveList = function () {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(callNativeApp(NATIVE_KEY_IOS.getArchiveList, {}));
  } else if (CHAT_UTIL.isAndroid()) {
    return JSON.parse(android.getArchiveList());
  }
};

// アーカイブ一覧検索
NativeBridgeDataSource.getArchiveByName = function (archiveName) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getArchiveByName, {
        archiveName: archiveName,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    return JSON.parse(android.getArchiveListByName(archiveName));
  }
};

// アーカイブ詳細
NativeBridgeDataSource.getArchiveDetail = function (archiveId) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getArchiveDetail, { archiveId: archiveId })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    return JSON.parse(android.getArchiveDetail(archiveId));
  }
};

NativeBridgeDataSource.searchMessages = function (keyword, userList) {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.searchMessages, {
        keyWord: keyword,
        userList: userList,
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    return JSON.parse(android.searchMessages(keyword, userList));
  }
};

NativeBridgeDataSource.getMyGroupUsersNotInRoom = function () {
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getMyGroupUsersNotInRoom, {})
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getMyGroupUsersNotInRoom());
  }
};

NativeBridgeDataSource.getUserInfo = function (shopMemberId) {
  if (shopMemberId == "") {
    return;
  }
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getUserInfo, { shopMemberId: shopMemberId })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    //String形式をJsonに変更してReturn
    return JSON.parse(android.getUserInfo(shopMemberId));
  }
};

NativeBridgeDataSource.getUserListByLoginId = function (loginIdList) {
  console.log(loginIdList);
  if (CHAT_UTIL.isIOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getUserListByLoginId, {
        loginIdList: loginIdList.join(","),
      })
    );
  } else if (CHAT_UTIL.isAndroid()) {
    return JSON.parse(android.getUserListByLoginId(loginIdList.join(",")));
  }
};

NativeBridgeDataSource.getBeforeRoomType = function () {
  if (CHAT_UTIL.isIOS()) {
    return callNativeApp(NATIVE_KEY_IOS.getBeforeRoomType, {});
  } else {
    return android.getBeforeRoomType();
  }
};

NativeBridgeDataSource.removeFavoriteUser = function (shopMemberId) {
  if (CHAT_UTIL.isIOS()) {
    var result = callNativeApp(NATIVE_KEY_IOS.removeFavoriteUser, {
      shopMemberId: shopMemberId,
    });
    if (result == "true") {
      return true;
    } else {
      return false;
    }
  } else {
    return android.removeFavoriteUser(shopMemberId);
  }
};

NativeBridgeDataSource.addFavoriteUser = function (shopMemberId) {
  if (CHAT_UTIL.isIOS()) {
    var result = callNativeApp(NATIVE_KEY_IOS.addFavoriteUser, {
      shopMemberId: shopMemberId,
    });
    if (result == "true") {
      return true;
    } else {
      return false;
    }
  } else {
    return android.addFavoriteUser(shopMemberId);
  }
};

NativeBridgeDataSource.getJoinCollaborationType = function () {
  if (CHAT_UTIL.isIOS()) {
    return parseInt(
      callNativeApp(NATIVE_KEY_IOS.getJoinCollaborationType, {}),
      10
    );
  } else {
    return android.getJoinCollaborationType();
  }
};

NativeBridgeDataSource.getCollaborationJoinFlg = function () {
  if (CHAT_UTIL.isIOS()) {
    return callNativeApp(NATIVE_KEY_IOS.getCollaborationJoinFlg, {});
  } else {
    return android.getCollaborationJoinFlg();
  }
};

NativeBridgeDataSource.getUserInfoList = function (shopMemberId) {
  if (CHAT_UTIL.isIOS()) {
    return callNativeApp(NATIVE_KEY_IOS.getUserInfoList, {
      shopMemberId: shopMemberId,
    });
  } else {
    return android.getUserInfoList(collaborationInfo.userList);
  }
};

NativeBridgeDataSource.createContentView = function () {
  if (CHAT_UTIL.isIOS()) {
    return parseInt(callNativeApp(NATIVE_KEY_IOS.createContentView, {}));
  } else {
    return android.createContentView();
  }
};

NativeBridgeDataSource.getToMoveGroupId = function () {
  if (CHAT_UTIL.isIOS()) {
    return callNativeApp(NATIVE_KEY_IOS.getToMoveGroupId, {});
  } else {
    return android.getToMoveGroupId();
  }
};

NativeBridgeDataSource.getHostRequestFlg = function () {
  if (CHAT_UTIL.isIOS()) {
    return parseInt(callNativeApp(NATIVE_KEY_IOS.getHostRequestFlg, {}), 10);
  } else {
    return android.getHostRequestFlg();
  }
};