package org.dominokit.domino.view.slots;

import elemental2.dom.HTMLBodyElement;
import elemental2.dom.HTMLElement;
import jsinterop.base.Js;
import org.dominokit.domino.api.client.mvp.slots.IsSlot;
import org.dominokit.domino.api.client.mvp.view.ContentView;
import org.dominokit.domino.api.client.mvp.view.HasContent;
import org.dominokit.domino.ui.utils.DominoElement;

import static java.util.Objects.nonNull;

public class BodyElementSlot implements IsSlot<ContentView> {

    private DominoElement<HTMLBodyElement> body = DominoElement.body();

    private static final BodyElementSlot INSTANCE = new BodyElementSlot();

    private ContentView currentView;

    public static BodyElementSlot create(){
        return INSTANCE;
    }

    private BodyElementSlot(){}

    @Override
    public void updateContent(ContentView view, HasContent.CreateHandler createHandler) {
        if(nonNull(currentView)){
            HTMLElement contentElement = Js.uncheckedCast(currentView.getContent().get());
            DominoElement.of(contentElement)
                    .remove();
        }
        currentView = view;
        body.appendChild(Js.<HTMLElement>uncheckedCast(view.getContent(createHandler).get()));
    }
}

@@ -309,24 +309,82 @@
 HasContent;%0A
+import org.dominokit.domino.api.shared.extension.Content;%0A
 import org.d
@@ -701,20 +701,16 @@
 tent
-View
  current
 View
@@ -705,20 +705,23 @@
  current
-View
+Content
 ;%0A%0A    p
@@ -757,16 +757,17 @@
 create()
+ 
 %7B%0A      
@@ -821,17 +821,23 @@
 ntSlot()
-%7B
+ %7B%0A    
 %7D%0A%0A    @
@@ -945,16 +945,17 @@
       if
+ 
 (nonNull
@@ -966,14 +966,18 @@
 rent
-View
+Content
 ))
+ 
 %7B%0A  
@@ -1043,25 +1043,15 @@
 rent
-View.get
 Content
-()
 .get
@@ -1153,26 +1153,56 @@
     
-currentView = view
+Content content = view.getContent(createHandler)
 ;%0A  
@@ -1258,46 +1258,57 @@
 ast(
-view.getContent(createHandler).get()))
+content.get()));%0A        currentContent = content
 ;%0A  

fix body slot