package MyCanvas.EndingCanvas;

import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;

import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.media.j3d.*;                   //Java3d

import MyMain.CameraPointingGame;
import MyMain.TextData;

import com.sun.j3d.utils.applet.MainFrame;  //MainFrame
import com.sun.j3d.utils.picking.PickCanvas;
import com.sun.j3d.utils.universe.*;        //SimpleUniverse
import com.sun.j3d.utils.geometry.*;        //Primitive

import javax.vecmath.*;                     //Point3f,Color3f

import java.awt.image.BufferedImage;
import java.awt.Font;

public class EndingCanvas{
    private SimpleUniverse simpleU;
    private BoundingSphere bounds = new BoundingSphere(new Point3d(), 10.0);
    public Canvas3D canvas;
    public PickCanvas cnvs=null;
    //private MyOwnCode   code=null;
    
    //終了ボタン
    Transform3D endTfd = null;
    TransformGroup endTg = null;
    Shape3D endShape3D = null;
    //次の面ボタン
    Transform3D nextTfd = null;
    TransformGroup nextTg = null;
    Shape3D nextShape3D = null;

	//文字表示
	//時間経過リミット
	public static Transform3D thanksTF3D ;
	public static TransformGroup thanksTFG;
    //プログラミング
	public static Transform3D programmingTF3D ;
	public static TransformGroup programmingTFG;
	//作画
	public static Transform3D artsTF3D ;
	public static TransformGroup artsTFG;
	//音楽
	public static Transform3D soundTF3D ;
	public static TransformGroup soundTFG;
	//操作方法
	public static Transform3D infoTF3D;
	public static TransformGroup infoTFG;
	
	//拳銃の音
	public static Transform3D gunTF3D;
	public static TransformGroup gunTFG;
    BranchGroup branchG0;

	public float move1 = 0.0f;
	public float move2 = 0.0f;
	public float move3 = 0.0f;
	public static MainFrame frame;
	
	CameraPointingGame cameraPointingGame;

    public EndingCanvas(CameraPointingGame cameraPointingGame){

    	this.cameraPointingGame = cameraPointingGame;

        canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
        canvas.setBounds(0,0,1024,768);

        simpleU = new SimpleUniverse(canvas);
        simpleU.getViewingPlatform().setNominalViewingTransform();

        simpleU.addBranchGraph(createSceneGraph());
    }
    
	public void prepareText(){
        
        thanksTF3D = new Transform3D();
        thanksTF3D.setTranslation(new Vector3f(-0.8f, 0.4f, 0.0f));
        thanksTFG = new TransformGroup(thanksTF3D);  
        createText(thanksTFG, TextData.CLEAR_MESSAGE);
        branchG0.addChild(thanksTFG);
   
        programmingTF3D = new Transform3D();
        programmingTF3D.setTranslation(new Vector3f(-0.6f, 0.1f, 0.0f));
        programmingTFG = new TransformGroup(programmingTF3D);  
        createText(programmingTFG, TextData.CLEAR_PROGRAMER);
        branchG0.addChild(programmingTFG);
        
        artsTF3D = new Transform3D();
        artsTF3D.setTranslation(new Vector3f(-0.6f, -0.1f, 0.0f));
        artsTFG = new TransformGroup(artsTF3D);  
        createText(artsTFG, TextData.CLEAR_ARTS);
        branchG0.addChild(artsTFG);
        
        soundTF3D = new Transform3D();
        soundTF3D.setTranslation(new Vector3f(-0.6f, -0.3f, 0.0f));
        soundTFG = new TransformGroup(soundTF3D);  
        createText(soundTFG, TextData.CLEAR_MUSIC);
        branchG0.addChild(soundTFG);
        
        gunTF3D = new Transform3D();
        gunTF3D.setTranslation(new Vector3f(-0.6f, -0.5f, 0.0f));
        gunTFG = new TransformGroup(gunTF3D);  
        createText(gunTFG, TextData.CLEAR_GUN);
        branchG0.addChild(gunTFG);
        
        infoTF3D = new Transform3D();
        infoTF3D.setTranslation(new Vector3f(-0.9f, -0.7f, 0.0f));
        infoTFG = new TransformGroup(infoTF3D);  
        createText(infoTFG, TextData.MENU_RESTART_AND_EXIT);
        branchG0.addChild(infoTFG);
	}

    private void createText(TransformGroup tg, String str){
    	Text2D text2d = new Text2D(
    			str,
    			new Color3f(0.1f,0.1f,0.4f),
    			TextData.FONT,
    			18,
    			Font.BOLD);
    	

        Appearance app = text2d.getAppearance();
        app.setCapability(Appearance.ALLOW_TEXTURE_READ);
        app.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
    	tg.addChild(text2d);
    }
    private BranchGroup createSceneGraph(){

        branchG0 = new BranchGroup();

		BufferedImage someImage = null;
		try {
			someImage = ImageIO.read(new File("res/gameback_bokashi.png"));
		} catch (IOException e) {
			// TODO 自動生成された catch ブロック
			e.printStackTrace();
		}
    	ImageComponent2D image=new ImageComponent2D(
    			ImageComponent2D.FORMAT_RGB,someImage);
    	Background background=new Background(image);
        
        //Background background = new Background(new Color3f(0.8f, 0.9f, 1.0f));
        background.setApplicationBounds(bounds);
        branchG0.addChild(background);
        
        EndingCanvasBehavior myBehavior3 = new EndingCanvasBehavior(thanksTFG,cameraPointingGame);
        myBehavior3.setSchedulingBounds(bounds);
        branchG0.addChild(myBehavior3);


        //テキスト表示
        prepareText();


        branchG0.compile();

        return branchG0;
    }

    public void destroy(){

        simpleU.cleanup();
    }


}

 