/* -*- Mode: java; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */

package com.danger.terminal;

import danger.ui.Bitmap;
import danger.ui.Font;
import danger.ui.Pen;
import danger.ui.Style;
import danger.util.StringUtils;


/* package */ class SessionListItem
    implements TerminalResources
{
    public
    SessionListItem (ServerInfo session)
    {
        mServerInfo = session;
        mConsole = null;
        mType = SERVER_TYPE;
    }
    
    public
    SessionListItem (TerminalConsole console)
    {
        mServerInfo = null;
        mConsole = console;
        mType = SESSION_TYPE;
    }
    
    public ServerInfo
    getServerInfo ()
    {
        return mServerInfo;
    }
    
    public TerminalConsole
    getConsole ()
    {
        return mConsole;
    }
    
    public int
    getType()
    {        
        return mType;
    }
    
    public void
    paintItem (Style style, Pen p, int left, int top, int right, int bottom, boolean focused)
    {
        switch(getType()) {
        case SERVER_TYPE:
            paintServer(style, p, left, top, right, bottom, focused);
            break;

        case SESSION_TYPE:
            paintSession(style, p, left, top, right, bottom, focused);
            break;
        }
    }
    
    private void
    paintServer (Style style, Pen p, int left, int top, int right, int bottom, boolean focused)
    {
        Font oldFont = p.getFont();
        Font font = Font.findBoldSystemFont();
        p.setFont(font);
        
        int fontDescent = font.getDescent();
        int fontHeight = fontDescent + font.getAscent();
        int baseline = ((top + bottom) >> 1) + (fontHeight >> 1) - fontDescent + 1;

        /* figure out how big the protocol label is so we can trim ourselves */
        String protocol = mServerInfo.getProtocolName();
        int protocolWidth = font.getWidth(protocol);
        
        left += SESSION_LABEL_LEFT_EDGE;
        right -= SESSION_LABEL_RIGHT_INSET;
        
        int width = (right - left) - protocolWidth - LABEL_GAP;
        
        String displayString = StringUtils.makeDisplayString(mServerInfo.getSessionName(), width, font);
        p.drawText(left, baseline, displayString);
        
        /* right align this one */
        p.drawText(right - protocolWidth, baseline, protocol);
        p.setFont(oldFont);
    }
    
    private void
    paintSession (Style style, Pen p, int left, int top, int right, int bottom, boolean focused)
    {
        Font font = p.getFont();
        int fontDescent = font.getDescent();
        int fontHeight = fontDescent + font.getAscent();
        int baseline = ((top + bottom) >> 1) + (fontHeight >> 1) - fontDescent + 1;

        Bitmap dot = null;
        if (mConsole.isOpen()) {
            dot = Terminal.instance().getBitmap(ID_CONNECTED_DOT);
        } else {
            dot = Terminal.instance().getBitmap(ID_DISCONNECTED_DOT);
        }
        
        int dotY = ((top + bottom) >> 1) - (dot.getHeight() >> 1);
        p.drawBitmap(left + CONSOLE_ICON_LEFT_EDGE, dotY, dot);

        int width = right - left - CONSOLE_NAME_LEFT_EDGE;
        String displayString = StringUtils.makeDisplayString(mConsole.getTitle(), width, font);
        p.drawText(left + CONSOLE_NAME_LEFT_EDGE, baseline, displayString);
    }

    public void
    itemActivated ()
    {
        switch (getType()) {
        case SERVER_TYPE:
            Terminal.instance().openSession(mServerInfo);
            break;
        case SESSION_TYPE:
            TerminalScreen window = Terminal.instance().getTerminalScreen();
            window.switchToConsole(mConsole);
            window.show();
            break;
        }
    }

    public void
    close ()
    {
        if (getType() != SESSION_TYPE) {
            System.err.println("@@@ should not happen!  SessionListItem.close called on HOST");
            return;
        }
        mConsole.disconnect();
    }
    
    
    private TerminalConsole     mConsole;
    private ServerInfo          mServerInfo;
    private int                 mType;

    static final int            SERVER_TYPE = 0;
    static final int            SESSION_TYPE = 1;
    
    /** the corner size of the border round-rects */
    static private final int    LABEL_GAP = 5;
    private static final int    SESSION_LABEL_RIGHT_INSET = 4;
    private static final int    SESSION_LABEL_LEFT_EDGE = 4;
    private static final int    CONSOLE_ICON_LEFT_EDGE = 8;
    private static final int    CONSOLE_NAME_LEFT_EDGE = 22;
}
