package com.lloydm.geosword;

import java.io.InputStream;

import android.content.res.AssetManager;
import android.util.Log;

import com.threed.jpct.Loader;
import com.threed.jpct.Object3D;
import com.threed.jpct.SimpleVector;
import com.threed.jpct.Texture;
import com.threed.jpct.TextureManager;

public class Mesh
{
     private final static String TAG = "com.lloydm.geosword.Mesh";
     public Object3D obj;

     public int basehp;
     public int index = -1;
     public int team = 0;
     public float[] meshbox;
     public SimpleVector centre = null;

     public Mesh()
     {

     }

     public boolean loadencryptedserializedobj(AssetManager assets, String meshfile, String texfile, int index, int basehp)
     {
                     Log.i(TAG, "Loading:" + meshfile + ", " + texfile + " index:" + index);

                     InputStream in = null; // model mesh
                     InputStream intex = null; // texture file
                     EncryptedInputStream encin = null;
                     EncryptedInputStream encintex = null;
                     boolean successflag = true;
                     try
                     {
                                     in = assets.open(meshfile);
                                     intex = assets.open(texfile);
                                     try
                                     {
                                                     // encin = new EncryptedInputStream(in);
                                                     // encintex = new EncryptedInputStream(intex);

                                                     this.index = index;
                                                     this.obj = Loader.loadSerializedObject(in);// Loader.load3DS(in,
                                                                                                                                                                                                                                 // 1.0f)[0];
                                                     Texture texture = new Texture(intex, true);
                                                     TextureManager.getInstance().addTexture("geosword_" + index, texture);

                                                     this.obj.setTexture("geosword_" + index);
                                                     this.obj.build();
                                                     if (meshfile.contains("cap1.dat"))
                                                     {
                                                                     this.obj.setOrigin(new SimpleVector(-43, 0, -27));
                                                                     this.obj.translateMesh();
                                                     }
                                                     meshbox = this.obj.getMesh().getBoundingBox();
                                                     Log.i(TAG, "Meshbox:minX:" + meshbox[0] + " maxX:" + meshbox[1] + " minY:" + meshbox[2] + " maxY:" + meshbox[3] + " minZ:" + meshbox[4] + " maxZ:" + meshbox[5]);
                                                     centre = this.obj.getCenter();
                                                     this.basehp = basehp;
                                                     Log.i(TAG, "Successfully loaded:" + meshfile);

                                     }
                                     catch (Exception e2)
                                     {
                                                     Log.e(TAG, "Error reading Serialised file");
                                                     successflag = false;
                                                     e2.printStackTrace();
                                     }
                     }
                     catch (Exception e1)
                     {
                                     Log.e(TAG, "Error opening serialised asset file");
                                     e1.printStackTrace();
                                     successflag = false;
                     }
                     finally
                     {
                                     try
                                     {
                                                     if (in != null)
                                                     {
                                                                     in.close();
                                                     }
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing serialised assets stream");
                                                     e.printStackTrace();
                                                     successflag = false;
                                     }
                                     try
                                     {
                                                     if (encin != null)
                                                     {
                                                                     encin.close();
                                                     }
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing encin stream");
                                                     e.printStackTrace();
                                                     successflag = false;
                                     }

                                     try
                                     {
                                                     if (intex != null)
                                                     {
                                                                     intex.close();
                                                     }
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing serialised assets stream");
                                                     e.printStackTrace();
                                                     successflag = false;
                                     }
                                     try
                                     {
                                                     if (encintex != null)
                                                     {
                                                                     encintex.close();
                                                     }
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing encintex stream");
                                                     e.printStackTrace();
                                                     successflag = false;
                                     }

                                     try
                                     {
                                                     // if(assets!=null){assets.close();return true;}
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing serialised assets stream");
                                                     e.printStackTrace();
                                                     successflag = false;
                                     }

                     }

                     return successflag;
     }

     public boolean loadobj(AssetManager assets, String meshfile, String texfile, int index, int basehp)
     {
                     Log.i(TAG, "Loading:" + meshfile + ", " + texfile + " index:" + index);

                     InputStream in = null; // model mesh
                     InputStream intex = null; // texture file
                     boolean successflag = true;
                     try
                     {
                                     in = assets.open(meshfile);
                                     intex = assets.open(texfile);
                                     try
                                     {
                                                     this.index = index;
                                                     this.obj = Loader.load3DS(in, 1.0f)[0];
                                                     Texture texture = new Texture(intex);
                                                     TextureManager.getInstance().addTexture("geosword_" + index, texture);

                                                     this.obj.setTexture("geosword_" + index);
                                                     this.obj.build();
                                                     // this.hitbox = this.obj.cloneObject();
                                                     // this.hitbox.setVisibility(false);
                                                     // this.hitbox.build();
                                                     this.basehp = basehp;
                                                     Log.i(TAG, "Successfully loaded:" + meshfile);

                                     }
                                     catch (Exception e2)
                                     {
                                                     Log.e(TAG, "Error reading 3DS file");
                                                     e2.printStackTrace();
                                                     successflag = false;
                                     }
                     }
                     catch (Exception e1)
                     {
                                     Log.e(TAG, "Error opening asset file");
                                     e1.printStackTrace();
                                     successflag = false;
                     }
                     finally
                     {
                                     try
                                     {
                                                     if (in != null)
                                                     {
                                                                     in.close();
                                                     }
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing assets stream");
                                                     e.printStackTrace();
                                                     successflag = false;
                                     }

                                     try
                                     {
                                                     if (intex != null)
                                                     {
                                                                     intex.close();
                                                     }
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing assets stream");
                                                     e.printStackTrace();
                                                     successflag = false;
                                     }
                                     try
                                     {
                                                     // if(assets!=null){assets.close();return true;}
                                     }
                                     catch (Exception e)
                                     {
                                                     Log.e(TAG, "Error closing assets stream");
                                                     e.printStackTrace();
                                                     successflag = false;
                                     }
                     }

                     return successflag;
     }

}