import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
/*
 * Created on 2006/02/12
 *
 */

/**
 * @author mori
 *
 */
public class MessageWindow {
    // gÌ
    private static final int EDGE_WIDTH = 2;

    // sÔÌå«³
    protected static final int LINE_HEIGHT = 8;
    // 1sÌÅå¶
    private static final int MAX_CHAR_IN_LINE = 20;
    // 1y[WÉ\¦Å«éÅås
    private static final int MAX_LINES = 3;
    // 1y[WÉ\¦Å«éÅå¶
    private static final int MAX_CHAR_IN_PAGE = MAX_CHAR_IN_LINE * MAX_LINES;

    // êÔO¤Ìg
    private Rectangle rect;
    // êÂà¤Ìgi¢güªÅ«éæ¤Éj
    private Rectangle innerRect;
    // ÀÛÉeLXgð`æ·ég
    private Rectangle textRect;

    // bZ[WEBhEð\¦©
    private boolean isVisible = false;

    // J[\ÌAj[VGIF
    private Image cursorImage;
    
    // bZ[Wði[µ½zñ
    private char[] text = new char[128 * MAX_CHAR_IN_LINE];
    // Ååy[W
    private int maxPage;
    // »Ý\¦µÄ¢éy[W
    private int curPage = 0;

    // bZ[WGW
    private MessageEngine messageEngine;

    public MessageWindow(Rectangle rect) {
        this.rect = rect;

        innerRect = new Rectangle(
                rect.x + EDGE_WIDTH,
                rect.y + EDGE_WIDTH,
                rect.width - EDGE_WIDTH * 2,
                rect.height - EDGE_WIDTH * 2);

        textRect = new Rectangle(
                innerRect.x + 16,
                innerRect.y + 16,
                320,
                120);
        
        // bZ[WGWðì¬
        messageEngine = new MessageEngine();

        // J[\C[Wð[h
        ImageIcon icon = new ImageIcon(getClass().getResource("image/cursor.gif"));
        cursorImage = icon.getImage();
    }

    public void draw(Graphics g) {
        if (isVisible == false) return;
        
        // gð`­
        g.setColor(Color.WHITE);
        g.fillRect(rect.x, rect.y, rect.width, rect.height);

        // à¤Ìgð`­
        g.setColor(Color.BLACK);
        g.fillRect(innerRect.x, innerRect.y, innerRect.width, innerRect.height);
        
        // »ÝÌy[WicurPagejÌ1y[WªÌàeð\¦
        for (int i=0; i<MAX_CHAR_IN_PAGE; i++) {
            char c = text[curPage * MAX_CHAR_IN_PAGE + i];
            int dx = textRect.x + MessageEngine.FONT_WIDTH * (i % MAX_CHAR_IN_LINE);
            int dy = textRect.y + (LINE_HEIGHT + MessageEngine.FONT_HEIGHT) * (i / MAX_CHAR_IN_LINE);
            messageEngine.drawCharacter(dx, dy, c, g);
        }

        // ÅãÌy[WÅÈ¢êÍîóð\¦·é
        if (curPage < maxPage) {
            int dx = textRect.x + (MAX_CHAR_IN_LINE / 2) * MessageEngine.FONT_WIDTH - 8;
            int dy = textRect.y + (LINE_HEIGHT + MessageEngine.FONT_HEIGHT) * 3;
            g.drawImage(cursorImage, dx, dy, null);
        }
    }

    /**
     * bZ[WðZbg·é
     * @param msg bZ[W
     */
    public void setMessage(String msg) {
        curPage = 0;

        // SpXy[XÅú»
        for (int i=0; i<text.length; i++) {
            text[i] = '@';
        }

        int p = 0;  // Ì¶Êu
        for (int i=0; i<msg.length(); i++) {
            char c = msg.charAt(i);
            if (c == '\\') {
                i++;
                if (msg.charAt(i) == 'n') {  // üs
                    p += MAX_CHAR_IN_LINE;
                    p = (p / MAX_CHAR_IN_LINE) * MAX_CHAR_IN_LINE;
                } else if (msg.charAt(i) == 'f') {  // üy[W
                    p += MAX_CHAR_IN_PAGE;
                    p = (p / MAX_CHAR_IN_PAGE) * MAX_CHAR_IN_PAGE;
                }
            } else {
                text[p++] = c;
            }
        }
        
        maxPage = p / MAX_CHAR_IN_PAGE;
    }
    
    /**
     * bZ[WðæÉißé
     * @return bZ[WªI¹µ½çtrueðÔ·
     */
    public boolean nextMessage() {
        // »Ýy[WªÅãÌy[W¾Á½çbZ[WðI¹·é
        if (curPage == maxPage) {
            return true;
        }
        curPage++;
        
        return false;
    }

    /**
     * EBhEð\¦
     */
    public void show() {
        isVisible = true;
    }

    /**
     * EBhEðB·
     */
    public void hide() {
        isVisible = false;
    }
    
    /**
     * EBhEð\¦©
     */
    public boolean isVisible() {
        return isVisible;
    }
}
