package com.airbiquity.hap;

import java.util.ArrayList;

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

import com.airbiquity.util_net.Json;

/**
 * Metadata for car and its subscriptions. (from BE call "User’s Vehicle Subscription")
 */
public class MetaCar
{
    public static final String ID_CAR_ID        = "vehicleId";
    public static final String ID_NICKNAME      = "nickname";
    public static final String ID_MODEL         = "model";
    public static final String ID_YEAR          = "year";
    public static final String ID_IS_ACTIVATED  = "activated";                                                                 
    public static final String ID_HUID          = "huID";
    public static final String ID_VIN           = "VIN";
    public static final String ID_SUBS          = "appSubscriptions";
    
    
    //public static final String KEY_META = "com.airbiquity.hap.MetaUserVehicleSubscription";
    
    public final int        idCar       ;
    public final String     nickname    ;
    public final String     model       ;
    public final int        year        ;
    public final boolean    isActivated ;
    public final String     huid        ;
    public final String     vin         ;
    public final String     subs        ;


    /**
     * Construct this object from JSONObject.
     * @param j : a JSONObject
     * @throws JSONException
     */
    public MetaCar( JSONObject j ) throws JSONException
    {
        idCar       = Json.gi( j, ID_CAR_ID         , 0     );
        nickname    = Json.gs( j, ID_NICKNAME       , ""    );
        model       = Json.gs( j, ID_MODEL          , ""    );
        year        = Json.gi( j, ID_YEAR           , 0     );
        isActivated = Json.gb( j, ID_IS_ACTIVATED   , false );
        huid        = Json.gs( j, ID_HUID           , ""    );
        vin         = Json.gs( j, ID_VIN            , ""    );
        subs        = Json.gs( j, ID_SUBS           , ""    );        
    }

    public MetaCar( 
            int        idCar       ,
            String     nickname    ,
            String     modl        ,
            int        year        ,
            boolean    isActivated ,
            String     huid        ,
            String     vin         ,
            String     subs     
            )
    {
        this.idCar       = idCar      ;
        this.nickname    = nickname   ;
        this.model       = modl       ;
        this.year        = year       ;
        this.isActivated = isActivated;
        this.huid        = huid       ;
        this.vin         = vin        ;
        this.subs        = subs       ;
    }
    

    /**
     * Parse string containing JSON representation of this object.
     * @param strJson : string that contains this object in JSON format.
     * @return this object.
     * @throws JSONException if the content is invalid JSON.
     */
    static public MetaCar valueOf( String strJson ) throws JSONException
    {
        JSONObject obj = new JSONObject( strJson );
        return new MetaCar( obj );
    }


    /**
     * Parse a list of this objects.
     * @param strJson : string that contains a list of this objects in JSON format.
     * @return List of this objects.
     * @throws JSONException if the content is invalid JSON.
     */
    static public ArrayList<MetaCar> parseList( String strJson ) throws JSONException
    {
        JSONObject jObj = new JSONObject( strJson );
        JSONArray ja = jObj.getJSONArray( "vehicleSubscriptions" ); 
        ArrayList<MetaCar> metas = new ArrayList<MetaCar>( ja.length() );
        for( int i = 0; i < ja.length(); i++ )
        {
            JSONObject jo = ja.getJSONObject( i );
            MetaCar meta = new MetaCar( jo );
            metas.add( meta );
        }
        return metas;
    }


    @Override
    public String toString()
    {
        //return id + " " + name + " " + isNop + " " + isLinkAway + " " + isUserIdEmail;
        return nickname;
    }
}
