package it.danieleverducci.openweddingapp.networking

import android.content.Context
import android.util.Log
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import it.danieleverducci.openweddingapp.entities.Jsonable
import org.json.JSONObject

open abstract class StaticItemNet<T: Jsonable> (val context: Context) {
    companion object {
        val TAG = "AuthenticatedItemNet"
    }

    val queue: RequestQueue

    init {
        queue = Volley.newRequestQueue(context)
    }

    abstract fun newItem(jo: JSONObject): T

    open fun getItem(url: String, listener: OnItemObtainedListener<T>) {
        val request = JsonObjectRequest(
            Request.Method.GET,
            url,
            null,
            {
                val item = newItem(it)
                listener.OnItemObtained(item)
            },
            {
                listener.OnError(it)
            })
        queue.add(request)
    }

    interface OnItemObtainedListener<T> {
        fun OnItemObtained(item: T)
        fun OnError(error: VolleyError)
    }

}