package org.joshy.gfx.node.control;

import org.joshy.gfx.css.CSSMatcher;
import org.joshy.gfx.draw.FlatColor;
import org.joshy.gfx.draw.Font;
import org.joshy.gfx.draw.GFX;
import org.joshy.gfx.event.*;
import org.joshy.gfx.node.Bounds;
import org.joshy.gfx.node.NodeUtils;
import org.joshy.gfx.stage.Stage;

import java.awt.geom.Point2D;

public class PopupMenuButton<E> extends Button implements SelectableControl {
    private ListModel model;
    private int selectedIndex;
    private PopupMenu popup;
    private ListView.TextRenderer<E> textRenderer;

    public PopupMenuButton()  {
        setWidth(200);
        setHeight(25);
        setSkinDirty();
        setModel(new ListModel<E>() {
            public E get(int i) {
                return (E) ("Dummy item " + i);
            }
            public int size() {
                return 3;
            }
        });
        setSelectedIndex(0);
/*        EventBus.getSystem().addListener(this, MouseEvent.MouseAll, new Callback<MouseEvent>(){
            public void call(MouseEvent event) {
                //processMouse(event);
            }
        });*/
    }
/*
    private void processMouse(MouseEvent event) {
        
    }*/

    public PopupMenuButton<E> setModel(ListModel<E> model) {
        this.model = model;
        return this;
    }

    public PopupMenuButton<E> setTextRenderer(ListView.TextRenderer<E> textRenderer) {
        this.textRenderer = textRenderer;
        return this;
    }

    @Override
    protected void setPressed(boolean pressed) {
        super.setPressed(pressed);
        if(pressed) {
            if(popup == null) {
                popup = new PopupMenu(getModel(), new Callback<ChangedEvent>() {
                    public void call(ChangedEvent event) {
                        setSelectedIndex((Integer)event.getValue());
                    }
                });
                popup.setTextRenderer(this.textRenderer);
                popup.setWidth(200);
                popup.setHeight(200);
                popup.setVisible(false);
                Stage stage = getParent().getStage();
                stage.getPopupLayer().add(popup);
            }
            Point2D pt = NodeUtils.convertToScene(this,0,0-this.getSelectedIndex()*25-PopupMenu.spacer);
            popup.setTranslateX(Math.max(pt.getX(),0));
            popup.setTranslateY(Math.max(pt.getY(),0));
            popup.setVisible(true);
        } else {
            popup.setVisible(false);
        }
    }

    @Override
    public void draw(GFX g) {
        if(!isVisible()) return;

        if(cssSkin != null) {
            CSSMatcher matcher = new CSSMatcher("PopupMenuButton");
            Bounds bounds = new Bounds(0,0,getWidth(),getHeight());
            cssSkin.drawBackground(g,matcher,"", bounds);
            int col = cssSkin.getCSSSet().findColorValue(matcher, "color");
            g.setPaint(new FlatColor(col));
            drawText(g);
            drawTriangle(g);
            cssSkin.drawBorder(g,matcher,"",bounds);
            return;
        } else {
            g.setPaint(FlatColor.WHITE);
            g.fillRoundRect(0,0,getWidth(),getHeight(), 10,10);
            g.setPaint(FlatColor.BLACK);
            drawText(g);
            drawTriangle(g);
            g.setPaint(FlatColor.BLACK);
            g.drawRoundRect(0,0,getWidth(),getHeight(), 10,10);
        }
    }

    private void drawTriangle(GFX g) {
        double[] points = new double[]{0,0, 14,0, 7, 9};
        g.translate(getWidth()-22,6);
        g.fillPolygon(points);
        g.translate(-getWidth()+22,-6);
    }

    private void drawText(GFX g) {
        E o = getSelectedItem();
        String s = o.toString();
        if(textRenderer != null) {
            s = textRenderer.toString(this,o,0);
        }
        Font.drawCenteredVertically(g, s, cssSkin.getDefaultFont(),6, 0, getWidth(), getHeight(), true);
    }

    public E getSelectedItem() {
        return getModel().get(getSelectedIndex());
    }

    public void setSelectedIndex(int selectedIndex) {
        this.selectedIndex = selectedIndex;
        this.text = getSelectedItem().toString();
        EventBus.getSystem().publish(new SelectionEvent(SelectionEvent.Changed,this));
        setDrawingDirty();
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public ListModel<E> getModel() {
        return model;
    }

}

@@ -589,54 +589,8 @@
   %7B%0A
-        setWidth(200);%0A        setHeight(25);%0A
     
@@ -860,297 +860,13 @@
 0);%0A
-/*        EventBus.getSystem().addListener(this, MouseEvent.MouseAll, new Callback%3CMouseEvent%3E()%7B%0A            public void call(MouseEvent event) %7B%0A                //processMouse(event);%0A            %7D%0A        %7D);*/%0A    %7D%0A/*%0A    private void processMouse(MouseEvent event) %7B%0A        %0A    %7D*/
+    %7D
 %0A%0A  
@@ -2148,32 +2148,33 @@
        %7D%0A    %7D%0A%0A
+%0A
     @Override%0A  
@@ -2188,16 +2188,279 @@
 c void d
+oPrefLayout() %7B%0A        super.doPrefLayout();%0A        if(prefWidth != CALCULATED) %7B%0A            setWidth(prefWidth);%0A            sizeInfo.width = prefWidth;%0A        %7D else %7B%0A            setWidth(sizeInfo.width+20);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void d
 raw(GFX 
@@ -2502,42 +2502,8 @@
 n;%0A%0A
-        if(cssSkin != null) %7B%0A    
     
@@ -2570,20 +2570,16 @@
         
-  
-  
 Bounds b
@@ -2626,36 +2626,32 @@
 ght());%0A        
-    
 cssSkin.drawBack
@@ -2672,36 +2672,32 @@
 er,%22%22, bounds);%0A
-    
         int col 
@@ -2752,36 +2752,32 @@
 olor%22);%0A        
-    
 g.setPaint(new F
@@ -2792,36 +2792,32 @@
 (col));%0A        
-    
 drawText(g);%0A   
@@ -2813,36 +2813,32 @@
 ext(g);%0A        
-   
- 
 drawTriangle(g);
@@ -2830,36 +2830,32 @@
 rawTriangle(g);%0A
-    
         cssSkin.
@@ -2891,360 +2891,8 @@
 s);%0A
-            return;%0A        %7D else %7B%0A            g.setPaint(FlatColor.WHITE);%0A            g.fillRoundRect(0,0,getWidth(),getHeight(), 10,10);%0A            g.setPaint(FlatColor.BLACK);%0A            drawText(g);%0A            drawTriangle(g);%0A            g.setPaint(FlatColor.BLACK);%0A            g.drawRoundRect(0,0,getWidth(),getHeight(), 10,10);%0A        %7D%0A
     
@@ -3024,17 +3024,18 @@
 th()-22,
-6
+10
 );%0A     
@@ -3096,17 +3096,18 @@
 h()+22,-
-6
+10
 );%0A    %7D

fix drawing of PopupMenuButton