// ios用
function callNativeApp(iosKey, jsonData) {
  console.debug("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.debug("The native context does not exist yet");
  }
  return result;
}

function parseBoolStringToBool(str) {
  return str == "true" ? true : false;
}

var NativeBridgeDataSource = {};

NativeBridgeDataSource.removeFavoriteGroup = function (groupID) {
  if (deviceInfo.isiOS()) {
    const result = callNativeApp(NATIVE_KEY_IOS.removeFavoriteGroup, {
      groupId: groupID,
    });
    return parseBoolStringToBool(result);
  } 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,
    });
    return parseBoolStringToBool(result);
  }
};

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 (deviceInfo.isiOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getRoomList, {
        roomType: roomType,
        keyWord: keyWord,
      })
    );
  } else if (deviceInfo.isAndroid()) {
    return JSON.parse(android.getRoomList(roomType, keyWord));
  }
};

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

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

NativeBridgeDataSource.getMessagesByMessageID = function (messageID) {
  if (deviceInfo.isiOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getMessageListFromMessageId, {
        messageId: messageID,
      })
    );
  } else if (deviceInfo.isAndroid()) {
    return JSON.parse(android.getMessageListFromMessageId(messageID));
  }
};

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

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

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

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

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

NativeBridgeDataSource.getGroupInfoForAddUser = function (groupId) {
  if (deviceInfo.isiOS()) {
    return JSON.parse(
      callNativeApp(NATIVE_KEY_IOS.getGroupInfoForAddUser, { groupId: groupId })
    );
  } else if (deviceInfo.isAndroid()) {
    return JSON.parse(android.getGroupInfoForAddUser(groupId));
  }
};

NativeBridgeDataSource.loadSelectedUsers = function () {
  if (deviceInfo.isiOS()) {
    return JSON.parse(callNativeApp(NATIVE_KEY_IOS.getSelectedUserList, {}));
  } else if (deviceInfo.isAndroid()) {
    return JSON.parse(android.getSelectedUserList());
  }
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

NativeBridgeDataSource.removeFavoriteUser = function (shopMemberId) {
  if (deviceInfo.isiOS()) {
    const result = callNativeApp(NATIVE_KEY_IOS.removeFavoriteUser, {
      shopMemberId: shopMemberId,
    });
    return parseBoolStringToBool(result);
  } else {
    return android.removeFavoriteUser(shopMemberId);
  }
};

NativeBridgeDataSource.addFavoriteUser = function (shopMemberId) {
  if (deviceInfo.isiOS()) {
    const result = callNativeApp(NATIVE_KEY_IOS.addFavoriteUser, {
      shopMemberId: shopMemberId,
    });
    return parseBoolStringToBool(result);
  } else {
    return android.addFavoriteUser(shopMemberId);
  }
};

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

NativeBridgeDataSource.getUserInfoList = function (shopMemberId) {
  if (deviceInfo.isiOS()) {
    return callNativeApp(NATIVE_KEY_IOS.getUserInfoList, {
      shopMemberId: shopMemberId,
    });
  } else {
    return android.getUserInfoList(shopMemberId);
  }
};

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

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

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

NativeBridgeDataSource.getCachedImage = function (loginID) {
  if (deviceInfo.isiOS()) {
    return callNativeApp(NATIVE_KEY_IOS.getCachedImage, { loginID: loginID });
  }
};
