package com.firebase.ui.database;

/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * 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.
 */

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * This class implements an array-like collection on top of a Firebase location.
 */
class FirebaseArray implements ChildEventListener {
    private Query mQuery;
    private OnChangedListener mListener;
    private List<DataSnapshot> mSnapshots = new ArrayList<>();
    public FirebaseArray(Query ref) {
        mQuery = ref;
        mQuery.addChildEventListener(this);
    }

    public void cleanup() {
        mQuery.removeEventListener(this);
    }

    public int getCount() {
        return mSnapshots.size();
    }

    public DataSnapshot getItem(int index) {
        return mSnapshots.get(index);
    }

    private int getIndexForKey(String key) {
        int index = 0;
        for (DataSnapshot snapshot : mSnapshots) {
            if (snapshot.getKey().equals(key)) {
                return index;
            } else {
                index++;
            }
        }

        throw new IllegalArgumentException("Key not found");
    }

    // [START] of ChildEventListener methods
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChildKey) {
        int index = 0;
        if (previousChildKey != null) {
            index = getIndexForKey(previousChildKey) + 1;
        }

        mSnapshots.add(index, snapshot);
        notifyChangedListeners(OnChangedListener.EventType.Added, index);
    }

    @Override
    public void onChildChanged(DataSnapshot snapshot, String previousChildKey) {
        int index = getIndexForKey(snapshot.getKey());
        mSnapshots.set(index, snapshot);
        notifyChangedListeners(OnChangedListener.EventType.Changed, index);
    }

    @Override
    public void onChildRemoved(DataSnapshot snapshot) {
        int index = getIndexForKey(snapshot.getKey());
        mSnapshots.remove(index);
        notifyChangedListeners(OnChangedListener.EventType.Removed, index);
    }

    @Override
    public void onChildMoved(DataSnapshot snapshot, String previousChildKey) {
        int oldIndex = getIndexForKey(snapshot.getKey());
        mSnapshots.remove(oldIndex);
        int newIndex = previousChildKey == null ? 0 : (getIndexForKey(previousChildKey) + 1);
        mSnapshots.add(newIndex, snapshot);
        notifyChangedListeners(OnChangedListener.EventType.Moved, newIndex, oldIndex);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        notifyCancelledListeners(databaseError);
    }

    public void setOnChangedListener(OnChangedListener listener) {
        mListener = listener;
    }
    // [END] of ChildEventListener methods

    protected void notifyChangedListeners(OnChangedListener.EventType type, int index) {
        notifyChangedListeners(type, index, -1);
    }

    protected void notifyChangedListeners(OnChangedListener.EventType type, int index, int oldIndex) {
        if (mListener != null) {
            mListener.onChanged(type, index, oldIndex);
        }
    }

    protected void notifyCancelledListeners(DatabaseError databaseError) {
        if (mListener != null) {
            mListener.onCancelled(databaseError);
        }
    }

    public interface OnChangedListener {
        void onChanged(EventType type, int index, int oldIndex);

        void onCancelled(DatabaseError databaseError);

        enum EventType {Added, Changed, Removed, Moved}
    }
}

@@ -1025,24 +1025,254 @@
 tListener %7B%0A
+    public interface OnChangedListener %7B%0A        void onChanged(EventType type, int index, int oldIndex);%0A%0A        void onCancelled(DatabaseError databaseError);%0A%0A        enum EventType %7BAdded, Changed, Removed, Moved%7D%0A    %7D%0A    %0A
     private 
@@ -1385,24 +1385,25 @@
 ayList%3C%3E();%0A
+%0A
     public F
@@ -4209,233 +4209,7 @@
    %7D
-%0A%0A    public interface OnChangedListener %7B%0A        void onChanged(EventType type, int index, int oldIndex);%0A%0A        void onCancelled(DatabaseError databaseError);%0A%0A        enum EventType %7BAdded, Changed, Removed, Moved%7D%0A    %7D
 %0A%7D%0A

Fix Intellij's annoying obsession with putting interfaces at the bottom of a class