/*
 * Copyright 2016 Red Hat, Inc. and/or its affiliates
 * and other contributors as indicated by the @author tags.
 *
 * Licensed 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.keycloak.quickstart.event.storage;

import org.keycloak.events.admin.AdminEvent;
import org.keycloak.events.admin.AdminEventQuery;
import org.keycloak.events.admin.OperationType;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author <a href="mailto:giriraj.sharma27@gmail.com">Giriraj Sharma</a>
 */
public class MemAdminEventQuery implements AdminEventQuery {
    
    private List<AdminEvent> adminEvents;

    private int first;
    private int max;

    public MemAdminEventQuery(List<AdminEvent> events) {
        this.adminEvents = events;
    }


    @Override
    public AdminEventQuery realm(String realmId) {
        Iterator<AdminEvent> itr = adminEvents.iterator();
        while (itr.hasNext()) {
            if (!itr.next().getRealmId().equals(realmId)) {
                itr.remove();
            }
        }
        return this;
    }

    @Override
    public AdminEventQuery operation(OperationType... operations) {
        Iterator<AdminEvent> itr = this.adminEvents.iterator();
        while (itr.hasNext()) {
            AdminEvent next = itr.next();
            boolean include = false;
            for (OperationType e : operations) {
                if (next.getOperationType().equals(e)) {
                    include = true;
                    break;
                }
            }
            if (!include) {
                itr.remove();
            }
        }
        return this;
    }

    @Override
    public AdminEventQuery resourceType(List<String> resourceTypes) {

        Iterator<AdminEvent> itr = this.adminEvents.iterator();
        while (itr.hasNext()) {
            AdminEvent next = itr.next();
            boolean include = false;
            for (String e : resourceTypes) {
                if (next.getResourceType().equals(e)) {
                    include = true;
                    break;
                }
            }
            if (!include) {
                itr.remove();
            }
        }

        return this;
    }
    
    @Override
    public AdminEventQuery authRealm(String authRealmId) {
        Iterator<AdminEvent> itr = adminEvents.iterator();
        while (itr.hasNext()) {
            if (!itr.next().getAuthDetails().getRealmId().equals(authRealmId)) {
                itr.remove();
            }
        }
        return this;
    }

    @Override
    public AdminEventQuery authClient(String authClientId) {
        Iterator<AdminEvent> itr = adminEvents.iterator();
        while (itr.hasNext()) {
            if (!itr.next().getAuthDetails().getClientId().equals(authClientId)) {
                itr.remove();
            }
        }
        return this;
    }

    @Override
    public AdminEventQuery authUser(String authUserId) {
        Iterator<AdminEvent> itr = adminEvents.iterator();
        while (itr.hasNext()) {
            if (!itr.next().getAuthDetails().getUserId().equals(authUserId)) {
                itr.remove();
            }
        }
        return this;
    }

    @Override
    public AdminEventQuery authIpAddress(String ipAddress) {
        Iterator<AdminEvent> itr = adminEvents.iterator();
        while (itr.hasNext()) {
            if (!itr.next().getAuthDetails().getIpAddress().equals(ipAddress)) {
                itr.remove();
            }
        }
        return this;
    }

    @Override
    public AdminEventQuery resourcePath(String resourcePath) {
        Iterator<AdminEvent> itr = this.adminEvents.iterator();
        while (itr.hasNext()) {
            if(!Pattern.compile(resourcePath).matcher(itr.next().getResourcePath()).find()) {
                itr.remove();
            }
        }
        return (AdminEventQuery) this;
    }

    @Override
    public AdminEventQuery fromTime(Date fromTime) {
        Iterator<AdminEvent> itr = this.adminEvents.iterator();
        while (itr.hasNext()) {
            if (!(itr.next().getTime() >= fromTime.getTime())) {
                itr.remove();
            }
        }
        return this;
    }

    @Override
    public AdminEventQuery toTime(Date toTime) {
        Iterator<AdminEvent> itr = this.adminEvents.iterator();
        while (itr.hasNext()) {
            if (!(itr.next().getTime() <= toTime.getTime())) {
                itr.remove();
            }
        }
        return this;
    }

    @Override
    public AdminEventQuery firstResult(int result) {
        this.first = result;
        return this;
    }

    @Override
    public AdminEventQuery maxResults(int results) {
        this.max = results;
        return this;
    }

    @Override
    public List<AdminEvent> getResultList() {
        return getResultStream().collect(Collectors.toList());
    }

    @Override
    public Stream<AdminEvent> getResultStream() {
        if (adminEvents.size() < first) {
            return Stream.empty();
        }
        int end = first + max <= adminEvents.size() ? first + max : adminEvents.size();

        return adminEvents.subList(first, end).stream();
    }
}

@@ -859,16 +859,63 @@
 ionType;
+%0Aimport org.keycloak.events.admin.ResourceType;
 %0A%0Aimport
@@ -2342,20 +2342,23 @@
 ype(
-List%3CString%3E
+ResourceType...
  res
@@ -2564,22 +2564,28 @@
    for (
-String
+ResourceType
  e : res

Revert "Change enum ResourceType to interface with String constants"