/**
  *  This function gets the animation of a heroe for a skill
  *
  *  Animation heroe_animation: heroe animation
  *  Animation player_animation: player animation
  *
  *  return: Statistics
  *
  **/
 public Statistics computeComparison(Animation_Replay heroe_animation, Animation_Replay player_animation)
 {
     //
     return new Statistics ();
 }
    /**
     *  This function gets the animation from its identifier
     *
     *  int animation_id: animation identifier
     *
     *  return: Animation
     *
     **/
    public Animation_Replay getAnimation(int animation_id)
    {
        IDbConnection dbConnection;
        IDbCommand dbCommand;
        IDataReader reader;

        int user_id = GlobalVariables.user_id;

        connectionString = "URI=file:"+Application.dataPath + "/"+GlobalVariables.user_database;
        dbConnection = new SqliteConnection (connectionString);
        dbConnection.Open ();

        Animation_Replay animation = new Animation_Replay();

        // Select
        string sql = "SELECT * FROM animation AS a ";
        sql += "WHERE a.id ="+animation_id;

        //Debug.Log (sql);
        dbCommand = dbConnection.CreateCommand ();
        dbCommand.CommandText = sql;

        reader = dbCommand.ExecuteReader ();
        while(reader.Read()) {
            int _id = Int32.Parse(reader.GetString(0));
            string _path = reader.GetString(1);
            string _creationDate = reader.GetString(2);
            int _hero_id = Int32.Parse(reader.GetString(3));
            int _skill_id = Int32.Parse(reader.GetString(4));
            int _active = Int32.Parse(reader.GetString(5));
            int _sent = Int32.Parse(reader.GetString(6));
            int _is_new = Int32.Parse(reader.GetString(7));

            animation = new Animation_Replay(_id,_path,_creationDate,_hero_id,_skill_id,_active,_sent,_is_new);

            //Debug.Log ("activity_id:"+aniamtion.id);
        }

        if (dbCommand != null) {
            dbCommand.Dispose ();
        }
        dbCommand = null;
        if (reader != null) {
            reader.Dispose ();
        }
        reader = null;
        if (dbConnection != null) {
            dbConnection.Close ();
        }
        dbConnection = null;

        return animation;
    }
    /**
     *  This function creates a new animation or updates an existing one
     *
     *  Animation animation: animation to create or to update
     *
     *  return: void
     *
     **/
    public Animation_Replay saveAnimation(string database, Animation_Replay animation)
    {
        IDbConnection dbConnection;
        IDbCommand dbCommand;
        IDataReader reader;

        connectionString = "URI=file:"+Application.dataPath + "/"+database;
        dbConnection = new SqliteConnection (connectionString);
        dbConnection.Open ();
        int id = animation.id;
        string sql="";

        dbCommand = dbConnection.CreateCommand ();

        if (id == 0)
        {
            // Insert new Activity

            sql= "INSERT INTO animation (path,creationDate,hero_id,active,sent,is_new) VALUES ('"+animation.path+"','"+animation.creationDate+"',"+animation.hero_id+","+animation.active+",1,1)";
            Debug.Log (sql);
            dbCommand.CommandText = sql;
            reader = dbCommand.ExecuteReader ();

            // Select the last temporal index inserted to know
            sql= "SELECT last_insert_rowid()";

            Debug.Log (sql);
            dbCommand.CommandText = sql;
            reader = dbCommand.ExecuteReader ();
            while(reader.Read()) {
                int _id = Int32.Parse(reader.GetString(0));
                animation.id = _id;

            }

        }
        else
        {
            // Update animation
            sql= "UPDATE animation SET path="+animation.path+",creationDate="+animation.creationDate+",hero_id="+animation.hero_id.ToString()+",active="+animation.active+",sent=1,is_new=0"+
                " WHERE id="+id.ToString();
            Debug.Log (sql);
            dbCommand.CommandText = sql;
            reader = dbCommand.ExecuteReader ();
        }

        if (dbCommand != null) {
            dbCommand.Dispose ();
        }
        dbCommand = null;
        if (reader != null) {
            reader.Dispose ();
        }
        reader = null;
        if (dbConnection != null) {
            dbConnection.Close ();
        }
        dbConnection = null;

        return animation;
    }
 /**
  *  This function saves the animation
  *
  *
  *  return: bool
  *
  **/
 public bool saveAnimationData(Animation_Replay animation)
 {
     // Saves the animation data
     return true;
 }
    /**
     *  This function gets the animation from an activity
     *
     *  int animation_id: animation identifier
     *
     *  return: Animation
     *
     **/
    public Animation_Replay getActivityAnimation(int activity_id)
    {
        IDbConnection dbConnection;
        IDbCommand dbCommand;
        IDataReader reader;

        int user_id = GlobalVariables.user_id;
        Activity activity = getActivity (activity_id);
        int animation_id = activity.animation_id;

        connectionString = "URI=file:"+Application.dataPath + "/"+GlobalVariables.user_database;
        dbConnection = new SqliteConnection (connectionString);
        dbConnection.Open ();

        Animation_Replay animation = new Animation_Replay();

        // Select
        string sql = "SELECT * FROM animation AS a ";
        sql += "WHERE a.id ="+animation_id;

        //Debug.Log (sql);
        dbCommand = dbConnection.CreateCommand ();
        dbCommand.CommandText = sql;

        reader = dbCommand.ExecuteReader ();
        while(reader.Read()) {
            int _id = Int32.Parse(reader.GetString(0));

            animation = getAnimation(_id);
            //Debug.Log ("activity_id:"+statistics.id);
        }

        if (dbCommand != null) {
            dbCommand.Dispose ();
        }
        dbCommand = null;
        if (reader != null) {
            reader.Dispose ();
        }
        reader = null;
        if (dbConnection != null) {
            dbConnection.Close ();
        }
        dbConnection = null;

        return animation;
    }