package jongs;

import java.awt.image.BufferedImage;
import java.util.Vector;
import java.awt.FontMetrics;
import java.awt.Point;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.Graphics;
import java.awt.image.ImageObserver;

// 
// JONGS - Java Open Novels Game Script
// 
//    JongsImage.java 
//
//  Copyright 2007  DENZI TOMIZKA
//
//  ver 1.00 Create by DENZI TOMIZKA 2007.5.25
//  ver 1.01           DENZI TOMIZKA 2007.6.03
//
/*
 *-----------------------------------------------------------------------
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *----------------------------------------------------------------------
 */

public class jongsImage  {
	private Vector<int[]> transColors ;
	private BufferedImage image ;

	private Rectangle posSize ;
	private String filename;
	boolean view ;
	boolean shiftview ;
	
	private String text ;
	private Font txtFont ;
	private Point txtPos;
	private Color txtColor;
	

	public jongsImage() {
		this.resetTransColor();
		posSize = new Rectangle() ;
		view = false ;
		shiftview = true ;
		text = null;
		txtFont = null;
		txtColor = null;
		txtPos = null ;
	}
	
	public void setImage(BufferedImage img, ImageObserver io) {
		this.resetTransColor();
		posSize.x = 0;
		posSize.y = 0;
		posSize.width = img.getWidth();
		posSize.height = img.getHeight();
        image = new BufferedImage(posSize.width,posSize.height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics g = image.getGraphics();
		g.drawImage(img,0,0,io);
		text = null;
		txtFont = null;
		txtColor = null;
		txtPos = null ;
		view = false ;
		shiftview = true ;
	}
	
	public void setCanvas( int w , int h , int color) {
		posSize.x = 0;
		posSize.y = 0;
		posSize.width = w ;
		posSize.height = h;
        image = new BufferedImage(posSize.width , posSize.height, BufferedImage.TYPE_4BYTE_ABGR);
        Graphics g = image.getGraphics();
        g.setColor(new Color(color) );
		g.fillRect(0,0,w,h);
		text = null;
		txtFont = null;
		txtColor = null;
		txtPos = null ;
		view = false ;
		shiftview = true ;
	}

	public BufferedImage getImage() {
		return image ;
	}

	public void setFileName(String fname) {
		filename = fname ;
	}
	
	public void setPoint(int x , int y) {
		this.posSize.x = x;
		this.posSize.y = y;
	}

	public void setPoint(Point p) {
		this.posSize.x = p.x;
		this.posSize.y = p.y;
	}

	public void setSize(int w , int h) {
		this.posSize.width = w;
		this.posSize.height = h;
	}
	
	public void setTextParam(String fntname , int style , int size , int fcol ) {
		txtFont = new Font(fntname,style,size);
		txtColor = new Color(fcol);
		text = "";
	}
	
	public void setText(String t){
		int px , py ;
		text = t ;
        Graphics g = image.getGraphics();
		g.setFont(this.txtFont);
		FontMetrics fm = g.getFontMetrics(this.txtFont);
		px = (this.posSize.width  - fm.stringWidth(t) ) / 2 ;
		py = (this.posSize.height + fm.getHeight() ) / 2 - fm.getHeight() / 4 ;
		txtPos = new Point( px , py );
		//System.out.println("setText:"+txtPos.x+","+txtPos.y);
	}
	
	public void drawText(Graphics g) {
		if ( text == null || txtFont == null || txtColor == null ) return ;
		if ( text.trim().length() == 0 ) return ;
		int px = posSize.x + txtPos.x ;
		int py = posSize.y + txtPos.y ;
		g.setFont(this.txtFont);
		g.setColor(Color.WHITE);
		g.drawString(text, px+1 , py+1 );
		g.drawString(text, px-1 , py+1 );
		g.drawString(text, px+1 , py-1 );
		g.drawString(text, px-1 , py-1 );
		g.setColor(this.txtColor);
		g.drawString(text, px , py );
	}
	
	public void setView(boolean v) { view = v  ;}		
	public void setShiftView(boolean v) { shiftview = v  ;}	

	public int getX() { return posSize.x ;}
	public int getY() { return posSize.y ;}
	public int getWidth() { return posSize.width ;}
	public int getHeight() { return posSize.height ;}
	public boolean getView() { return view ;}	
	public boolean getShiftView() { return shiftview ;}		
	
	/**
	 * 透過色を設定する
	 * @param tc
	 */
	public void setTransColor(int tc) {
		int [] col ;
		//透過情報を持つイメージか？
		if ( image.getColorModel().getTransparency() == BufferedImage.TRANSLUCENT ) {
			col = new int[1];
			col[0] = tc ;
			transColors.add(col);
			this.setTransImage(tc);
		} else {
		}
	}
	
	/**
	 * 透過色を座標で指定する
	 * @param x
	 * @param y
	 * @param alpha
	 */
	public void setTransColor (int x , int y , int alpha) {
		int tc ;
		tc = image.getRGB(x,y) & 0x00ffffff ;
		tc = tc | (alpha << 24 );
		this.setTransColor(tc);
	}
	
	/**
	 * 透過色を色と透明度で指定する
	 * @param x
	 * @param y
	 * @param alpha
	 */
	public void setTransColor (int color , int alpha) {
		int tc ;
		tc = color & 0x00ffffff ;
		tc = tc | (alpha << 24 );
		this.setTransColor(tc);
	}
	
	/**
	 * 透過色を一気に設定する
	 * @param tc
	 */
	public void setTransColors(Vector<int[]> tcs) {
		int n , tc ;
		for ( n = 0 ; n < tcs.size() ; n++) {
			transColors.add(tcs.get(n));
			tc = tcs.get(n)[0];
			this.setTransImage(tc);			
		}
	}
	
	/**
	 * 透過色データをリセットする
	 *
	 */
	public void resetTransColor() {
		this.transColors = new Vector <int[]>();		
	}

	/**
	 * 透過色を画面に反映する
	 * @param tc
	 */
	public void setTransImage(int tc){
		int width  = image.getWidth();
		int height = image.getHeight();
		int x , y ;
		int color = tc & 0x00ffffff ;
		int col0 ;
		
		for (y = 0 ; y < height ; y++) {
			for (x = 0 ; x < width ; x++) {
				col0 = image.getRGB(x, y) & 0x00ffffff;
				if ( col0 == color ) image.setRGB(x, y, tc);
			}
		}
	}
	
	
	/**
	 * 指定グローバル座標位置が有効画素か確認。
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean checkImageClick(int x , int y){
		int px , py , bx , by , col0 ;
		//不可視状態なら問答無用でfalse
		if ( this.view == false ) return false ;
		//グローバル座標がエリアに含まれないならfalse
		if ( this.posSize.contains(x, y) == false ) {
			return false;
		}
		//画像内座標を得る
		px = x - posSize.x ;
		py = y - posSize.y ;
		//拡縮を考慮する。
		if ( posSize.width != this.image.getWidth() )
			bx = ( px * this.image.getWidth()) / posSize.width ;
		else 
			bx = px;
		if ( posSize.height != this.image.getHeight() )
			by = ( py * this.image.getHeight()) / posSize.height ;
		else 
			by = py;
		//比率計算の誤差で範囲外になる可能性もある．範囲外であればはじく
		if ( bx < 0 || bx >= this.image.getWidth() ) return false ;
		if ( by < 0 || by >= this.image.getHeight() ) return false ;		
		col0 = ( image.getRGB( bx , by ) >> 24 );
		//色情報の透明度が０ならfalse
		if ( col0 == 0 ) return false;
		//全ての条件をクリアすればＴＲＵＥ
		//System.out.println(filename+"contain x:"+x+"y:"+y);
		return true;
	}
	
	/**
	 * 現在のセルの情報を文字列化する
	 */
	public String toString() {
		int n , tc;
		StringBuffer sb = new StringBuffer() ;
		sb.append("(");
		sb.append(this.filename);sb.append(" ");
		sb.append(this.posSize.x);sb.append(" ");
		sb.append(this.posSize.y);sb.append(" ");
		sb.append(this.posSize.width);sb.append(" ");
		sb.append(this.posSize.height);sb.append(" ");
		if (view) sb.append("1 ");
		else sb.append("0 ");
		if (shiftview) sb.append("1 ");
		else sb.append("0 ");
		for (  n = 0 ; n < this.transColors.size() ; n++  ) {
			tc =  this.transColors.get(n)[0] ;
			sb.append( Integer.toHexString(tc) );
		}
		sb.append("(");		
		return sb.toString();
	}
	
}
