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

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

//ロカールDBからルーム一覧情報を取得
CHAT_DB.getRoomList = function(roomType, keyWord) {
    if (CHAT_UTIL.isIOS()) {
        return JSON.parse(callNativeApp("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("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("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("getMessages", {"roomId": roomId}));
    } else if (CHAT_UTIL.isAndroid()) {
        //String形式をJsonに変更してReturn
    	return JSON.parse(android.getMessageList(roomId));
    }
};

CHAT_DB.getFavoriteUsersNotInRoom = function() {
    if (CHAT_UTIL.isIOS()) {
        return JSON.parse(callNativeApp("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("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("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("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("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("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("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("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("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("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("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("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("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("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("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("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("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("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("getMyGroupUsersNotInRoom", {}));
    } else if (CHAT_UTIL.isAndroid()) {
        //String形式をJsonに変更してReturn
    	return JSON.parse(android.getMyGroupUsersNotInRoom());
    }
};

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

CHAT_DB.getBeforeRoomType = function() {
    // Androidは実装不要
    if (CHAT_UTIL.isIOS()) {
        return callNativeApp("getBeforeRoomType", {});
    }
};

CHAT_DB.getRoomType = function() {
    // Androidは実装不要
    if (CHAT_UTIL.isIOS()) {
        return callNativeApp("getRoomType", {});
    }
};

CHAT_DB.removeFavoriteUser = function(shopMemberId) {
    // Androidは実装不要
    if (CHAT_UTIL.isIOS()) {
        var result = callNativeApp("removeFavoriteUser", {"shopMemberId": shopMemberId});
        if (result == "true") {
            return true;
        } else {
            return false;
        } 
    }
};

CHAT_DB.addFavoriteUser = function(shopMemberId) {
    // Androidは実装不要
    if (CHAT_UTIL.isIOS()) {
        var result = callNativeApp("addFavoriteUser", {"shopMemberId": shopMemberId});
        if (result == "true") {
            return true;
        } else {
            return false;
        } 
    }
};

CHAT_DB.removeFavoriteGroup = function(groupId) {
    // Androidは実装不要
    if (CHAT_UTIL.isIOS()) {
        var result = callNativeApp("removeFavoriteGroup", {"groupId": groupId});
        if (result == "true") {
            return true;
        } else {
            return false;
        } 
    }
};

CHAT_DB.addFavoriteGroup = function(groupId) {
    // Androidは実装不要
    if (CHAT_UTIL.isIOS()) {
        var result = callNativeApp("addFavoriteGroup", {"groupId": groupId});
        if (result == "true") {
            return true;
        } else {
            return false;
        } 
    }
};


CHAT_DB.getContentList = function() {
    if (CHAT_UTIL.isIOS()) {
        //TODO ローカルDBからダウンロード済のコンテンツを取得
    } else if (CHAT_UTIL.isAndroid()) {
        return JSON.parse(android.getContentList());
    }
};