package macampcorp.macamp.game;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.util.Log;

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

import java.io.Serializable;
import java.util.ArrayList;

import macampcorp.macamp.exceptions.BadConfigFileException;
import macampcorp.macamp.exceptions.BadResourceConfigException;
import macampcorp.macamp.exceptions.ResourceFileNotFoundException;
import macampcorp.macamp.exceptions.ResourceNotFoundException;
import macampcorp.macamp.utils.L;
import macampcorp.macamp.utils.files.JSONAssetsManager;
/**
 * Created by ptitcois on 19/08/16.
 */
public class Resource implements Serializable {

    protected String mName;
    protected String mComment = "null";
    protected boolean isLoaded = false;
    protected boolean mIsDisplay = false;
    protected int   mStage;
    protected String mImage;
    protected String mType = ""; //3D Image Audio Video
    protected String mTitle= ""; //3D Image Audio Video
    private  static JSONObject mRessourcesList = null;
    public static ArrayList<String> mNotFound = new ArrayList<>();
    protected String mFile = null;

    public String getFile() {
        if(mFile!=null)
            return mFile;
        return mName;
    }

    public boolean isFile() {
        return mFile!=null;
    }

    public static void initResourceList(Activity act)
    {
        if(mRessourcesList!=null) return;
        L.info( "Chargement de la liste des ressources...");
        JSONObject obj = JSONAssetsManager.load(act, "ressources.res");
        if(obj==null)
        {
            L.warn("Attention il n'existe pas de fichier pour la liste des ressrouecs");
            return;
        }
        mRessourcesList=obj;
    }

    public static JSONObject getRessourceDataFromList(String name)
    {
        try {
            if(mRessourcesList!=null)
                return mRessourcesList.getJSONObject(name);
        } catch (JSONException e) {
            L.warn("Impossiblle de trouver la ressource '"+name+"' dans la liste des ressources");
        }
        return null;
    }


    public static void setNotFound(String name)
    {
        for(int i=0; i< mNotFound.size(); i++)
            if(mNotFound.get(i).compareTo(name)==0) return;
        mNotFound.add(name);
    }

    protected void loadResourceData(Activity act)
    {
        initResourceList(act);
        L.info( "--- Chargement de la ressource '"+mName+"' --- ");
        JSONObject obj = JSONAssetsManager.load(act, mName+".res");
        if(obj==null) obj = getRessourceDataFromList(mName);
        if(obj == null)
        {
            L.error("\tLa ressource '"+mName+"' est introuvable.");
            setNotFound(mName);
            throw new ResourceNotFoundException(mName);
        }

        //mandatory
        try {
            mType = obj.getString("type");
        } catch(JSONException e)
        {
            L.error("\tLa ressource '"+mName+"' n'a pas d'attribut 'type' valide (String)");
            e.printStackTrace();
            throw new BadResourceConfigException("Bad parameter 'type' ");
        }

        try {
            mTitle = obj.getString("title");
        } catch (JSONException e) {
        }

        try {
            mComment=obj.getString("comment");
        } catch (JSONException e) {
        }

        try {
            mImage=obj.getString("image");
        } catch (JSONException e) {
            mImage="appel.png";
        }

        try {
            mStage = obj.getInt("stage");
            isLoaded=true;
        } catch (JSONException e) {
        }

        try{
            mFile = obj.getString("file");
        }catch (JSONException e)
        {
            mFile=null;
        }

        try {
            mIsDisplay=(boolean) obj.getBoolean("display");
        } catch (JSONException e) {
            mIsDisplay=false;
        }
    }

    public boolean hasFile(){
        return mFile!=null;
    }


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


    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 String getimage()
    {
        return mImage;
    }


    public String toString()
    {
        return mTitle;
    }

    public static final String IMAGE = "Image";
    public static final String VIDEO = "Video";
    public static final String AUDIO = "Audio";


    public boolean isAudio()
    {
        return mType.toLowerCase().compareTo(AUDIO.toLowerCase())==0;
    }

    public boolean isVideo()
    {
        return mType.toLowerCase().compareTo(VIDEO.toLowerCase())==0;
    }

    public boolean isImage()
    {
        return mType.toLowerCase().compareTo(IMAGE.toLowerCase())==0;
    }

    public boolean isDisplay() {
        return mIsDisplay;
    }
}
