package com.dreamteam.timelapse.data

import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.Date

class ProjectRepository(private val apiService: ApiService) {

    fun fetchProjects(onSuccess: (List<Project>) -> Unit, onError: (String) -> Unit) {
        val call = apiService.getProjets()
        call.enqueue(object : Callback<List<Project>> {
            override fun onResponse(call: Call<List<Project>>, response: Response<List<Project>>) {
                if (response.isSuccessful) {
                    val projects = response.body()
                    projects?.let {
                        onSuccess(it)
                    } ?: onError("Aucun projet trouvé")
                } else {
                    onError("Erreur : ${response.code()}")
                }
            }

            override fun onFailure(call: Call<List<Project>>, t: Throwable) {
                onError("Échec de l'appel API : ${t.message}")
            }
        })
    }
    fun fetchProject(id:Int, onSuccess: (Project) -> Unit, onError: (String) -> Unit) {
        val call = apiService.getProjet(id)
        call.enqueue(object : Callback<Project> {
            override fun onResponse(call: Call<Project>, response: Response<Project>) {
                if (response.isSuccessful) {
                    val project = response.body()
                    project?.let {
                        onSuccess(it)
                    } ?: onError("Le projet ${id} n'existe pas")
                } else {
                    onError("Erreur : ${response.code()}")
                }
            }

            override fun onFailure(call: Call<Project>, t: Throwable) {
                onError("Échec de l'appel API : ${t.message}")
            }
        })
    }

    fun fetchMeasurementsOfProject(pid: Int, onSuccess: (List<Measurement>) -> Unit, onError: (String) -> Unit) {
        val call = apiService.getMeasurements(pid)
        call.enqueue(object : Callback<List<Measurement>> {
            override fun onResponse(call: Call<List<Measurement>>, response: Response<List<Measurement>>) {
                if (response.isSuccessful) {
                    val measures = response.body()
                    measures?.let {
                        onSuccess(it)
                    } ?: onError("Aucune mesure trouvé")
                } else {
                    onSuccess(emptyList())
                    //onError("Erreur reception measures: ${response.code()}")
                }
            }

            override fun onFailure(call: Call<List<Measurement>>, t: Throwable) {
                onError("Échec de l'appel API : ${t.message}")
            }
        })
    }
    fun createProject(name: String, description: String, onSuccess: () -> Unit, onError: (String) -> Unit) {
        val call = apiService.createProject(Project(0, name, description, Date(), 0))
        call.enqueue(object : Callback<Confirmation> {
            override fun onResponse(call: Call<Confirmation>, response: Response<Confirmation>) {
                if (response.isSuccessful) {
                    onSuccess()
                } else {
                    onError("Erreur creation projet : ${response.code()}, ${response.body()}")
                }
            }

            override fun onFailure(call: Call<Confirmation>, t: Throwable) {
                onError("Échec de l'appel API : ${t.message}")
            }
        })
    }

    fun deleteProject(id : Int, onSuccess: ()->Unit, onError: (String) -> Unit){
        val call = apiService.deleteProject(id)
        call.enqueue(object : Callback<Void> {
            override fun onResponse(call: Call<Void>, response: Response<Void>) {
                if (response.isSuccessful) {
                    onSuccess()
                } else {
                    onError("Erreur : ${response.code()}")
                }
            }

            override fun onFailure(call: Call<Void>, t: Throwable) {
                onError("Échec de l'appel API : ${t.message}")
            }
        })
    }

}