package org.foxarmy.barcodescannerforemployees

import android.content.Context
import android.widget.Toast
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley

class Requester constructor(var siteName: String, var endpoint: String) {
    var response = ""

    fun request(context: Context, barcode: String) {
        val url = "${siteName}/${endpoint}"

        val volleyQueue = Volley.newRequestQueue(context)
        val stringRequest = object: StringRequest(
            Method.POST, url, { resp ->
            run {
                response =
                if (resp == "") {
                    "Not found 404"
                } else {
                    resp
                }
            }
        },
            {
                Toast.makeText(context, "Cannot make request", Toast.LENGTH_LONG).show()
            }
        ) {
            override fun getHeaders(): Map<String, String> {
                return mapOf("referer" to "$siteName/")
            }

            public override fun getParams(): MutableMap<String, String> {
                return mutableMapOf("barcode" to barcode)
            }
        }
        volleyQueue.add(stringRequest)
    }
}
