/*
 * Created on 24.02.2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package de.farafin.snEADy.control.GaphixStuff;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.VolatileImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;



/**
 * @author larsonmars
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class C_GraphixTools implements I_GraphixConstants{

    protected static boolean loadImages(){
    	int i=0;
    	File tmpF=null;
    	BufferedImage tmpImg=null;
    	//VolatileImage loadedC_GraphixData.gameImages[]=new VolatileImage[C_END];
   	
        

    	C_GraphixData.gameImageFiles[C_FLOOR]=			new File("pics/game/floor.png");
    	C_GraphixData.gameImageFiles[C_PLAYER_S]=		new File("pics/game/snakes.png");
    	C_GraphixData.gameImageFiles[C_EXIT]=			new File("pics/game/exit.png");
        
    	C_GraphixData.gameImageFiles[C_WALL]=			new File("pics/game/wall.png");
        
    	C_GraphixData.gameImageFiles[C_WALL_S]=			new File("pics/game/walledges.png");
        

    	C_GraphixData.gameImageFiles[C_GOODY_S]=		new File("pics/game/goodies.png");
        
        
    	C_GraphixData.menuImageFiles[M_BG]=				new File("pics/menu/m_bg.png");
    	C_GraphixData.menuImageFiles[M_BUTTRUN]=		new File("pics/menu/m_buttrun.png");
        C_GraphixData.menuImageFiles[M_BUTTSTOP]=		new File("pics/menu/m_buttstop.png");
        C_GraphixData.menuImageFiles[M_BUTTQUIT]=		new File("pics/menu/m_buttquit.png");
        C_GraphixData.menuImageFiles[M_BUTTMIN]=		new File("pics/menu/m_buttmin.png");
        C_GraphixData.menuImageFiles[M_BUTTCREDITS]=	new File("pics/menu/m_buttcredits.png");
        C_GraphixData.menuImageFiles[M_BUTTARENA]=		new File("pics/menu/m_buttarena.png");
        C_GraphixData.menuImageFiles[M_BUTTPLAYER]=		new File("pics/menu/m_buttplayer.png");
        C_GraphixData.menuImageFiles[M_BUTTOPTIONS]=	new File("pics/menu/m_buttoptions.png");
        C_GraphixData.menuImageFiles[M_BUTTRMPLAYER]=	new File("pics/menu/m_buttrmplayer.png");
        C_GraphixData.menuImageFiles[M_TABOPEN]=		new File("pics/menu/m_tabopen.png");
        C_GraphixData.menuImageFiles[M_TABCLOSE]=		new File("pics/menu/m_tabclose.png");

        C_GraphixData.menuImageFiles[M_FONTSERP8X14]=	new File("pics/menu/m_fontserp8x14.png");

        
        C_GraphixData.menuImageFiles[S_BUTTQUIT]=		new File("pics/menu/s_buttquit.png");
        C_GraphixData.menuImageFiles[S_BUTTMIN]=		new File("pics/menu/s_buttmin.png");
        C_GraphixData.menuImageFiles[S_BUTTLOAD]=		new File("pics/menu/s_buttload.png");
        C_GraphixData.menuImageFiles[S_BUTTADD]=		new File("pics/menu/s_buttadd.png");
        C_GraphixData.menuImageFiles[S_BUTTOK]=			new File("pics/menu/s_buttok.png");
        C_GraphixData.menuImageFiles[S_VSCROLLBUTT]=	new File("pics/menu/s_vscrollbutt.png");
        C_GraphixData.menuImageFiles[S_VSCROLL]=		new File("pics/menu/s_vscroll.png");
        C_GraphixData.menuImageFiles[S_ARENABG]=		new File("pics/menu/d_arenabg.png");
        C_GraphixData.menuImageFiles[S_PLAYERBG]=		new File("pics/menu/d_playerbg.png");
        C_GraphixData.menuImageFiles[S_OPTIONSBG]=		new File("pics/menu/d_optionsbg.png");
        C_GraphixData.menuImageFiles[M_TEXTAREA]=		new File("pics/menu/m_textarea.png");
        C_GraphixData.menuImageFiles[M_TGLBUTT]=		new File("pics/menu/m_tglbutt.png");



        
        C_GraphixData.gameImages[C_UNKNOWN]=createBackBufferVI(FIELDELEMSIZE,FIELDELEMSIZE);
        try{
            for (i=1;i<C_END;i++){
                tmpF=C_GraphixData.gameImageFiles[i];
                if (tmpF!=null){
                    C_GraphixData.gameImagesO[i]=ImageIO.read(tmpF);
                    C_GraphixData.gameImagesBak[i]=C_GraphixData.gameImagesO[i];
	            }
            }
            for (i=1;i<UIGRAPHIX_END;i++){
                tmpF=C_GraphixData.menuImageFiles[i];
                if (tmpF!=null){
                    C_GraphixData.menuImagesO[i]=ImageIO.read(tmpF);
                }
            }
        }catch(IOException e){
            System.out.println("Exception: "+ e + " FILE: " + tmpF);
            System.exit(1);
        }
        
        drawBufferedBitmapString("Ver.:" + genVerString(),285,92,M_FONTSERP8X14,C_GraphixData.menuImagesO[M_BG].getGraphics(),ALLIG_RIGHT,F_NORMAL);
        
        for (i=1;i<C_END;i++){
        	tmpImg=C_GraphixData.gameImagesO[i];
            if (tmpImg!=null){
                C_GraphixData.gameImages[i]=createBackBufferVI(tmpImg.getWidth(null),tmpImg.getHeight(null));
                C_GraphixData.gameImages[i].getGraphics().drawImage(tmpImg,0,0,null);
            }
        }
        for (i=1;i<M_VOLATILE_END;i++){
        	tmpImg=C_GraphixData.menuImagesO[i];
            if (tmpImg!=null){
            	C_GraphixData.menuImages[i]=createBackBufferVI(tmpImg.getWidth(null),tmpImg.getHeight(null));
            	C_GraphixData.menuImages[i].getGraphics().drawImage(tmpImg,0,0,null);
            }
        }

 
        
        /*
        for (i=1;i<C_END;i++)
            if (loadedC_GraphixData.gameImages[i]!=null){
                C_GraphixData.gameImages[i]=getDoubledImage(loadedC_GraphixData.gameImages[i]);
            }
            */
        return true;
    }
    
    protected static final String getAvailLevelGraphix(String path){
    	File grDir=new File(dir_arena,path);
    	File tmpF=null;
    	StringBuffer str=new StringBuffer(40);

    	if (grDir.isDirectory()){
	    	
	    	if (	(new File(grDir,"floor.png")).canRead()||
	    			(new File(grDir,"wall.png")).canRead()||
					(new File(grDir,"exit.png")).canRead()||
					(new File(grDir,"walledges.png")).canRead())
	  	    	return "Yes";
    	}

   		return "N/A";
    }
    protected static final void loadCustomLevelGraphix(String path){
    	File grDir=new File(dir_arena,path);
    	File tmpF=null;

    	if (grDir.isDirectory()){
	    	C_GraphixData.gameCustImageFiles[C_FLOOR]=new File(grDir,"floor.png");
	    	C_GraphixData.gameCustImageFiles[C_WALL]=new File(grDir,"wall.png");
	    	C_GraphixData.gameCustImageFiles[C_EXIT]=new File(grDir,"exit.png");
	    	C_GraphixData.gameCustImageFiles[C_WALL_S]=new File(grDir,"walledges.png");
	    	try{
	    		tmpF=C_GraphixData.gameCustImageFiles[C_FLOOR];
		    	if (tmpF.canRead())
		    		C_GraphixData.gameImagesO[C_FLOOR]=ImageIO.read(tmpF);
		    	else
		    		C_GraphixData.gameImagesO[C_FLOOR]=C_GraphixData.gameImagesBak[C_FLOOR];

	    		tmpF=C_GraphixData.gameCustImageFiles[C_WALL];
		    	if (tmpF.canRead())
		    		C_GraphixData.gameImagesO[C_WALL]=ImageIO.read(tmpF);
		    	else
		    		C_GraphixData.gameImagesO[C_WALL]=C_GraphixData.gameImagesBak[C_WALL];

	    		tmpF=C_GraphixData.gameCustImageFiles[C_EXIT];
		    	if (tmpF.canRead())
		    		C_GraphixData.gameImagesO[C_EXIT]=ImageIO.read(tmpF);
		    	else
		    		C_GraphixData.gameImagesO[C_EXIT]=C_GraphixData.gameImagesBak[C_EXIT];

	    		tmpF=C_GraphixData.gameCustImageFiles[C_WALL_S];
		    	if (tmpF.canRead())
		    		C_GraphixData.gameImagesO[C_WALL_S]=ImageIO.read(tmpF);
		    	else
		    		C_GraphixData.gameImagesO[C_WALL_S]=C_GraphixData.gameImagesBak[C_WALL_S];

	    	}catch(IOException e){
	    		System.out.println("Exception: "+ e + " FILE: " + tmpF);
	    		System.exit(1);
	    	}

    	}
    	else{
    		C_GraphixData.gameImagesO[C_FLOOR]=C_GraphixData.gameImagesBak[C_FLOOR];
    		C_GraphixData.gameImagesO[C_WALL]=C_GraphixData.gameImagesBak[C_WALL];
    		C_GraphixData.gameImagesO[C_EXIT]=C_GraphixData.gameImagesBak[C_EXIT];
    		C_GraphixData.gameImagesO[C_WALL_S]=C_GraphixData.gameImagesBak[C_WALL_S];
    	}
        C_GraphixData.gameImages[C_FLOOR]=createBackBufferVI(C_GraphixData.gameImagesO[C_FLOOR].getWidth(null),C_GraphixData.gameImagesO[C_FLOOR].getHeight(null));
        C_GraphixData.gameImages[C_FLOOR].getGraphics().drawImage(C_GraphixData.gameImagesO[C_FLOOR],0,0,null);
        C_GraphixData.gameImages[C_WALL]=createBackBufferVI(C_GraphixData.gameImagesO[C_WALL].getWidth(null),C_GraphixData.gameImagesO[C_WALL].getHeight(null));
        C_GraphixData.gameImages[C_WALL].getGraphics().drawImage(C_GraphixData.gameImagesO[C_WALL],0,0,null);
        C_GraphixData.gameImages[C_EXIT]=createBackBufferVI(C_GraphixData.gameImagesO[C_EXIT].getWidth(null),C_GraphixData.gameImagesO[C_EXIT].getHeight(null));
        C_GraphixData.gameImages[C_EXIT].getGraphics().drawImage(C_GraphixData.gameImagesO[C_EXIT],0,0,null);
        C_GraphixData.gameImages[C_WALL_S]=createBackBufferVI(C_GraphixData.gameImagesO[C_WALL_S].getWidth(null),C_GraphixData.gameImagesO[C_WALL_S].getHeight(null));
        C_GraphixData.gameImages[C_WALL_S].getGraphics().drawImage(C_GraphixData.gameImagesO[C_WALL_S],0,0,null);

    	
    }
    private static final String genVerString(){
    	int tmp,tmp2;
    	StringBuffer str=new StringBuffer(6);
    	tmp=C_GraphixData.version/1000;
    	str.append(tmp);
    	str.append('.');
    	tmp=C_GraphixData.version-1000*tmp;
    	tmp2=tmp/100;
    	str.append(tmp2);
    	tmp-=tmp2*100;
    	tmp2=tmp/10;
    	str.append(tmp2);
    	tmp-=tmp2*10;
    	str.append(tmp);
    	
    	return str.toString();
    }
    protected static final VolatileImage createBackBufferVI(int width, int height) {
    	//return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height);
        //return new VolatileImage(width,height,VolatileImage.TYPE_INT_RGB);
    	return C_GraphixData.gc.createCompatibleVolatileImage(width, height);
    	
    }
    
    protected static boolean drawGameImageToBackbuffer(VolatileImage destVImg,int imageID,int dx1,int dy1,int dx2,int dy2,int sx1,int sy1,int sx2,int sy2){
    	if (destVImg.validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK)return false;
   		wantReadGameImage(imageID);
    	destVImg.getGraphics().drawImage(C_GraphixData.gameImages[imageID],dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,null,null);
    	if (destVImg.contentsLost()) return false;
    	return true;
    }
    protected static final void getVImgFromImg(VolatileImage destVImg,Image srcImg){
    	if (destVImg.validate(C_GraphixData.gc)==VolatileImage.IMAGE_INCOMPATIBLE)
    		destVImg=C_GraphixData.gc.createCompatibleVolatileImage(srcImg.getWidth(null), srcImg.getHeight(null));
    	do{
    		destVImg.getGraphics().drawImage(srcImg,0,0,null);
    	}while (destVImg.contentsLost());
    }

    protected static final void wantReadMenuImage(int imageID){
		if (C_GraphixData.menuImages[imageID].validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK)
			getVImgFromImg(C_GraphixData.menuImages[imageID],C_GraphixData.menuImagesO[imageID]);
	}
    protected static final void wantReadGameImage(int imageID){
		if (C_GraphixData.gameImages[imageID].validate(C_GraphixData.gc)!=VolatileImage.IMAGE_OK)
			getVImgFromImg(C_GraphixData.gameImages[imageID],C_GraphixData.gameImagesO[imageID]);
    }
	
    protected static void drawBitmapString(String str,int x, int y,int GFONT,Graphics g,int alligm,int MODE){
        char actC;
        int srcX,srcY,absX,absY;
        int sizeX,sizeY,d;
        sizeX=C_GraphixData.menuImages[GFONT].getWidth(null)/30;
        sizeY=C_GraphixData.menuImages[GFONT].getHeight(null)/16;
        d=MODE*sizeX*6;
        if (alligm==ALLIG_RIGHT)
            absX=x-str.length()*sizeX;
        else
            absX=x;
        absY=y;
        
        for (int i=0;i<str.length();i++){
            actC=str.charAt(i);
            if (actC=='\n'){
            	absY+=sizeY;
                if (alligm==ALLIG_RIGHT)
                    absX=x-str.length()*sizeX;
                else
                    absX=x;
            }
            else{
	            if (actC<' ' || actC>(char)127) actC='?';
	            srcX=((actC-' ')/16)*sizeX+d;
	            srcY=((actC-' ')%16)*sizeY;
	            wantReadMenuImage(GFONT);
	            g.drawImage(C_GraphixData.menuImages[GFONT],absX,absY,absX+sizeX,absY+sizeY,srcX,srcY,srcX+sizeX,srcY+sizeY,null,null);
	            absX+=sizeX;
            }
        }
        
    }
    protected static void drawBufferedBitmapString(String str,int x, int y,int GFONT,Graphics g,int alligm,int MODE){
        char actC;
        int srcX,srcY,absX,absY;
        int sizeX,sizeY,d;
        sizeX=C_GraphixData.menuImagesO[GFONT].getWidth(null)/30;
        sizeY=C_GraphixData.menuImagesO[GFONT].getHeight(null)/16;
        d=MODE*sizeX*6;
        if (alligm==ALLIG_RIGHT)
            absX=x-str.length()*sizeX;
        else
            absX=x;
        absY=y;
        
        for (int i=0;i<str.length();i++){
            actC=str.charAt(i);
            if (actC=='\n'){
            	absY+=sizeY;
                if (alligm==ALLIG_RIGHT)
                    absX=x-str.length()*sizeX;
                else
                    absX=x;
            }
            else{
	            if (actC<' ' || actC>(char)127) actC='?';
	            srcX=((actC-' ')/16)*sizeX+d;
	            srcY=((actC-' ')%16)*sizeY;
	            g.drawImage(C_GraphixData.menuImagesO[GFONT],absX,absY,absX+sizeX,absY+sizeY,srcX,srcY,srcX+sizeX,srcY+sizeY,null,null);
	            absX+=sizeX;
            }
        }
        
    }
    protected static void drawVMenuImage(int imageID,Graphics g){
    	wantReadMenuImage(imageID);
    	g.drawImage(C_GraphixData.menuImages[imageID],0,0,null);
    }
    
}
