package cnp.ew.scrolling;

import java.awt.*;
import java.util.*;

import cnp.ew.util.*;
import cnp.ew.lightweight.*;
import cnp.ew.displayer.*;
import cnp.ew.text.*;

public class CpScrollingController extends CpAbstractLc
{
    // All of these variables are referenced by subclasses.  Shouldn't be public though.  Protected?
    public CpScroller scroller;
    public CpScrollbarLc horizontalScrollBar;
    public CpScrollbarLc verticalScrollBar;

    int scrollerMarginX = 0;
    int scrollerMarginY = 0;
    CpFlyingTipLc verticalTip, horizontalTip;

    public CpScrollingController(boolean hasVerticalScrollBar, boolean hasHorizontalScrollBar)
    {
        super();

	    setBorderStyle(BORDER_INWARD3D);

        scroller = new CpScroller();
        add(scroller);
        scroller.addObserver(this);

        setLayout(null);    // Layout will be done by layout() below...
        if (hasVerticalScrollBar) {
            verticalScrollBar = new CpScrollbarLc(Scrollbar.VERTICAL);
            add(verticalScrollBar);
            verticalScrollBar.addObserver(this);
        }
        if (hasHorizontalScrollBar) {
            horizontalScrollBar = new CpScrollbarLc(Scrollbar.HORIZONTAL);
            add(horizontalScrollBar);
            horizontalScrollBar.addObserver(this);
        }
    }

    public void setScrollerMargin(int x, int y)
    {
        scrollerMarginX = x;
        scrollerMarginY = y;
    }

    // This returns the insets for the placement of the scroller
    public Insets scrollerInsets()
    {
        return insets();
    }

    public synchronized void layout()
    {
        Insets insets;
        int verticalWidth = 0;
        int horizontalHeight = 0;
        int verticalHeight, horizontalWidth;

        insets = scrollerInsets();

        verticalHeight = size().height - insets.top - insets.bottom;
        if (horizontalScrollBar != null) {
            horizontalHeight = horizontalScrollBar.preferredSize().height;
            verticalHeight = verticalHeight - horizontalHeight;
        }

        horizontalWidth = size().width - insets.left - insets.right;
        if (verticalScrollBar != null) {
            verticalWidth = verticalScrollBar.preferredSize().width;
            horizontalWidth = horizontalWidth - verticalScrollBar.preferredSize().width;
            verticalScrollBar.reshape(size().width - verticalScrollBar.preferredSize().width - insets.right, insets.top, verticalScrollBar.preferredSize().width, verticalHeight);

        }

        if (horizontalScrollBar != null) {
            horizontalScrollBar.reshape(insets.left, size().height - horizontalScrollBar.preferredSize().height - insets.bottom, horizontalWidth, horizontalScrollBar.preferredSize().height);
        }

        if (scroller != null) {
            scroller.reshape(insets.left + scrollerMarginX, insets.top + scrollerMarginY, size().width - insets.left - insets.right - verticalWidth - scrollerMarginX , size().height - insets.top - insets.bottom - horizontalHeight - scrollerMarginY );
        }
    }

    public void update(CpObservable o, int facet, Object arg)
    {
        if (facet == CpEvent.SCROLLER_PARAMETERS_CHANGED) {
            if (verticalScrollBar != null) {
           //     System.out.println("ScrollerSize = " + scroller.getSizeInCells());
           //     System.out.println("getScrollableSizeInCells = " + scroller.getScrollableSizeInCells());
           //     System.out.println("getScrollableLocationInCells = " + scroller.getScrollableLocationInCells());

                verticalScrollBar.setValues(scroller.getSizeInCells().height, scroller.getScrollableSizeInCells().height, scroller.getScrollableLocationInCells().y);
            }
            if (horizontalScrollBar != null) {
                horizontalScrollBar.setValues(scroller.getSizeInCells().width, scroller.getScrollableSizeInCells().width, scroller.getScrollableLocationInCells().x);
            }
        }
        // Issue.  Hack for now, only implemented for vertical scroll bar...

        if (facet == CpEvent.START_DRAG_SCROLL && o == verticalScrollBar)
        {
//        System.out.println("START DERG form " + o);
            String text;
            if ((text = verticalScrollingTextFor(verticalScrollBar.getValue())) != null) {
//                System.out.println("Poping up for " + text);
                verticalTip = new CpFlyingTipLc(text);
                verticalTip.setDrawOffscreen(true);
//                System.out.println("Here we go...");
                // How to determine width?  Max size of all text?  Compute when they set Items?  ISSUE.
                verticalTip.reshape(new Rectangle(size().width - 210, (size().height - verticalTip.preferredSize().height) / 2, 180, verticalTip.preferredSize().height));
                add(verticalTip);
            }
        }
        if (facet == CpEvent.END_DRAG_SCROLL  && o == verticalScrollBar)
        {
            if (verticalTip != null) {
                remove(verticalTip);
                verticalTip = null;
            }
            horizontalTip = null;
            // Kind of sneaky way to get logic to go to the next if...
            facet = OBJECT_CHANGED;
        }

        if ((o == horizontalScrollBar || o == verticalScrollBar) && (facet == OBJECT_CHANGED || facet == OBJECT_CHANGING)) {

            int verticalPos = 0;
            int horizontalPos = 0;
	        if (verticalScrollBar != null) {
	     //       System.out.println("Vert scroll to: " + verticalScrollBar.getValue());
	            verticalPos = verticalScrollBar.getValue();
	        }
	        if (horizontalScrollBar != null) {
	            horizontalPos = horizontalScrollBar.getValue();
	        }
            if (facet == OBJECT_CHANGING && (verticalTip != null || horizontalTip != null)) {
                verticalTip.setText(verticalScrollingTextFor(verticalPos));
                return;
            }
         //   System.out.println("Scrolling scroller to: " + new Point(horizontalPos, verticalPos) + " obs = " + o);

	        scroller.scrollTo(horizontalPos, verticalPos);
	    }
    }

    public String verticalScrollingTextFor(int i)
    {
        return null;
    }

    public void paint(Graphics g, Rectangle clip)
    {
        g.setColor(getBackground());
        g.fillRect(0, 0, size().width, size().height);
        if (verticalScrollBar != null && horizontalScrollBar != null) {
            g.setColor(Color.lightGray);
            Rectangle bounds = verticalScrollBar.bounds();
            g.fillRect(bounds.x, bounds.y + bounds.height, bounds.width, bounds.height);
        }
    }
}