package app.brest.utils.app.brest.game;

import android.app.Activity;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

import app.brest.utils.JSONLoader;
import min3d.core.Object3d;
import min3d.parser.IParser;
import min3d.parser.Parser;
import min3d.vos.Number3d;

/**
 * Created by ptitcois on 19/08/16.
 */
public class Resource implements Serializable {

    protected String mName;
    protected transient Object3d  m3DModel = null;
    protected Number3d mPosition = new Number3d(0,0,0);
    protected Number3d mRotation = new Number3d(0,0,0);
    protected Number3d mScale = new Number3d(1,1,1);
    protected String mComment = "null";
    protected boolean isLoaded = false;
    protected int   mStage;
    protected String mType = ""; //3D Image Audio Video
    protected String mTitle= ""; //3D Image Audio Video

    protected void loadResourceData(Activity act)
    {
        JSONObject obj = JSONLoader.load(act, mName+"_res");
        try {
            mComment=obj.getString("comment");
            mPosition.x =(float) obj.getJSONArray("position").getDouble(0);
            mPosition.y =(float) obj.getJSONArray("position").getDouble(1);
            mPosition.z =(float) obj.getJSONArray("position").getDouble(2);


            mRotation.x =(float) obj.getJSONArray("rotation").getDouble(0);
            mRotation.y =(float) obj.getJSONArray("rotation").getDouble(1);
            mRotation.z =(float) obj.getJSONArray("rotation").getDouble(2);


            mScale.x =(float) obj.getJSONArray("scale").getDouble(0);
            mScale.y =(float) obj.getJSONArray("scale").getDouble(1);
            mScale.z =(float) obj.getJSONArray("scale").getDouble(2);


            mType = obj.getString("type");
            mTitle = obj.getString("title");
            mStage = obj.getInt("stage");
            isLoaded=true;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public Number3d getPosition() {
        return mPosition;
    }

    public Number3d getRotation() {
        return mRotation;
    }

    public Number3d getScale() {
        return mScale;
    }

    public Resource(String name, Activity act)
    {
        mName=name;
        loadResourceData(act);
    }

    public Resource(JSONObject obj, Activity act)
    {
        try {
            mName=obj.getString("name");
            loadResourceData(act);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String getName() {
        return mName;
    }

    public String getType() {
        return mType;
    }

    public String getTitle() {
        return mTitle;
    }


    public String getComment() {
        return mComment;
    }

    public int getStage() {
        return mStage;
    }

    public  JSONObject getJson()
    {
        JSONObject jo = new JSONObject();
        try {
            jo.put("name", mName);
            jo.put("stage", mStage);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jo;
    }

    public Object3d get3DModel(Activity context)
    {
        if(m3DModel==null)
        {
            Log.e("resource.get3Model", "app.brest.testmin3d:raw/"+mName+"_obj");
            IParser myParser = Parser.createParser(Parser.Type.OBJ, context.getResources(), "app.brest.testmin3d:raw/"+mName+"_obj",true);
            myParser.parse();

            m3DModel = myParser.getParsedObject();
            m3DModel.position().x = mPosition.x; m3DModel.position().y = mPosition.y; m3DModel.position().z = mPosition.z;
            m3DModel.rotation().x = mRotation.x; m3DModel.rotation().y = mRotation.y; m3DModel.rotation().z = mRotation.z;
            Log.e("____","Rotation ("+m3DModel.rotation().x+","+m3DModel.rotation().y+","+m3DModel.rotation().z+")");
            //m3DModel.rotation().x+=90;
            //m3DModel.rotation().y+=-180;

            //m3DModel.rotation().y=90;
            m3DModel.scale().x = mScale.x; m3DModel.scale().y = mScale.y; m3DModel.scale().z = mScale.z;
            Log.e("MODEL", mName+" ("+m3DModel.scale().x+", "+m3DModel.scale().y+", "+m3DModel.scale().z+")");
        }
        return m3DModel;

    }



    public void delete3DModel()
    {
        m3DModel = null;
    }

    public boolean is3DModelLoaded()
    {
        return m3DModel!=null;
    }

    public void scale3DModel(float factor)
    {
        if(m3DModel!=null)
            m3DModel.scale().x = m3DModel.scale().y = m3DModel.scale().z = factor;
    }

    public String toString()
    {
        return mTitle;
    }

}
