/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.streampipes.storage.couchdb.impl;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.streampipes.model.Notification;
import org.apache.streampipes.model.NotificationCount;
import org.apache.streampipes.storage.api.INotificationStorage;
import org.apache.streampipes.storage.couchdb.dao.AbstractDao;
import org.apache.streampipes.storage.couchdb.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class NotificationStorageImpl extends AbstractDao<Notification> implements
        INotificationStorage {

  Logger LOG = LoggerFactory.getLogger(NotificationStorageImpl.class);

  public NotificationStorageImpl() {
    super(Utils::getCouchDbNotificationClient, Notification.class);
  }

  @Override
  public Notification getNotification(String notificationId) {
    return findWithNullIfEmpty(notificationId);
  }

  @Override
  public List<Notification> getAllNotifications(String notificationTypeId,
                                                Integer offset,
                                                Integer count) {
    Gson gson = couchDbClientSupplier.get().getGson();
    List<JsonObject> notifications =
            couchDbClientSupplier
                    .get()
                    .view("notificationtypes/notificationtypes")
                    .startKey(Arrays.asList(notificationTypeId, "\ufff0"))
                    .endKey("\ufff0")
                    .descending(true)
                    .includeDocs(true)
                    .skip(offset)
                    .limit(count)
                    .query(JsonObject.class);

    return notifications
            .stream()
            .map(notification -> gson.fromJson(notification, Notification.class))
            .collect(Collectors.toList());
  }

  @Override
  public boolean addNotification(Notification notification) {
    persist(notification);
    return true;
  }

  @Override
  public boolean changeNotificationStatus(String notificationId) {
    Notification msg = getNotification(notificationId);
    msg.setRead(!msg.isRead());

    return update(msg);
  }

  @Override
  public boolean deleteNotification(String notificationId) {
    return delete(notificationId);
  }

  @Override
  public List<Notification> getUnreadNotifications() {
    List<Notification> msgs = findAll();

    return msgs
            .stream()
            .filter(m -> !m.isRead())
            .collect(Collectors.toList());
  }

  @Override
  public NotificationCount getUnreadNotificationsCount(String username) {
    List<JsonObject> count =
            couchDbClientSupplier
                    .get()
                    .view("unread/unread")
                    .key(username)
                    .group(true)
                    .query(JsonObject.class);

    if (count.size() > 0) {
      Integer countValue = count.get(0).get("value").getAsInt();
      return new NotificationCount(countValue);
    } else {
      return new NotificationCount(0);
    }
  }
}
