// 名前空間
var CHAT_DB = {};

//ロカールDBからルーム一覧情報を取得
CHAT_DB.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));
  }
};

CHAT_DB.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からログインしたユーザのデータを取得する。
CHAT_DB.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());
  }
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// アーカイブ一覧検索
CHAT_DB.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));
  }
};

// アーカイブ詳細
CHAT_DB.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));
  }
};

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

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

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

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

CHAT_DB.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(",")));
  }
};
