/**
 * Copyright 2010 The PlayN Authors
 *
 * 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 playn.html;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Node;

import playn.core.Asserts;
import playn.core.GroupLayer;
import playn.core.GroupLayerImpl;
import playn.core.Layer;
import playn.core.ParentLayer;

class HtmlGroupLayerDom extends HtmlLayerDom implements GroupLayer, ParentLayer {

  private GroupLayerImpl<HtmlLayerDom> impl = new GroupLayerImpl<HtmlLayerDom>();

  HtmlGroupLayerDom() {
    super(Document.get().createDivElement());
  }

  HtmlGroupLayerDom(Element elem) {
    super(elem);
  }

  @Override
  public Layer get(int index) {
    return impl.children.get(index);
  }

  @Override
  public void add(Layer layer) {
    Asserts.checkArgument(layer instanceof HtmlLayerDom);
    HtmlLayerDom hlayer = (HtmlLayerDom) layer;
    int index = impl.add(this, hlayer);
    Node refChild = element().getChild(index);
    element().insertBefore(hlayer.element(), refChild);
  }

  @Override @Deprecated
  public void add(int index, Layer layer) {
    Asserts.checkArgument(layer instanceof HtmlLayerDom);
    HtmlLayerDom hlayer = (HtmlLayerDom) layer;
    if (index == size()) {
      element().appendChild(hlayer.element());
    } else {
      Node refChild = element().getChild(index);
      element().insertBefore(hlayer.element(), refChild);
    }
    impl.add(this, index, hlayer);
  }

  @Override
  public void remove(Layer layer) {
    Asserts.checkArgument(layer instanceof HtmlLayerDom);
    HtmlLayerDom hlayer = (HtmlLayerDom) layer;
    impl.remove(this, hlayer);
    element().removeChild(hlayer.element());
  }

  @Override @Deprecated
  public void remove(int index) {
    impl.remove(this, index);
    element().removeChild(element().getChild(index));
  }

  @Override
  public void clear() {
    impl.clear(this);
    while (element().hasChildNodes()) {
      element().removeChild(element().getFirstChild());
    }
  }

  @Override
  public int size() {
    return impl.children.size();
  }

  @Override
  public void destroy() {
    super.destroy();
    impl.destroy(this);
  }

  @Override
  public void onAdd() {
    super.onAdd();
    impl.onAdd(this);
  }

  @Override
  public void onRemove() {
    super.onRemove();
    impl.onRemove(this);
  }

  @Override
  public void depthChanged(Layer layer, float oldDepth) {
    Asserts.checkArgument(layer instanceof HtmlLayerDom);
    HtmlLayerDom hlayer = (HtmlLayerDom) layer;
    element().removeChild(hlayer.element());
    int index = impl.depthChanged(this, layer, oldDepth);
    if (index == size()) {
      element().appendChild(hlayer.element());
    } else {
      Node refChild = element().getChild(index);
      element().insertBefore(hlayer.element(), refChild);
    }
  }

  void update() {
    super.update();
    for (HtmlLayerDom child : impl.children) {
      child.update();
    }
  }
}

@@ -1417,24 +1417,47 @@
 Dom) layer;%0A
+    int size = size();%0A
     int inde
@@ -1480,24 +1480,111 @@
 s, hlayer);%0A
+    if (index == size) %7B%0A      element().appendChild(hlayer.element());%0A    %7D else %7B%0A  
     Node ref
@@ -1614,32 +1614,34 @@
 ild(index);%0A    
+ 
+ 
 element().insert
@@ -1676,16 +1676,22 @@
 Child);%0A
+    %7D%0A
   %7D%0A%0A  @

Fix HtmlGroupLayerDom::add() bug