package com.dreamteam.timelapse.data

import android.util.Log
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class VideoRepository(private val apiService: ApiService) {
    fun fetchRenderedVideos(onSuccess: (List<Video>) -> Unit, onError: (String) -> Unit) {
        apiService.getVideos().enqueue(object : Callback<List<VideoDTO>> {
            override fun onResponse(call: Call<List<VideoDTO>>, response: Response<List<VideoDTO>>) {
                if (response.isSuccessful) {
                    val videoDtos = response.body() ?: emptyList()
                    val videos = videoDtos.map { it.toVideo() } // Convert ugly data to clean Video objects
                    onSuccess(videos.filter { v -> v.video_file != null })
                } else {
                    onError("Erreur serveur : ${response.code()}")
                }
            }

            override fun onFailure(call: Call<List<VideoDTO>>, t: Throwable) {
                onError("Erreur réseau : ${t.message}")
            }
        })
    }
    fun fetchUnrenderedVideos(onSuccess: (List<Video>) -> Unit, onError: (String) -> Unit) {
        apiService.getVideos().enqueue(object : Callback<List<VideoDTO>> {
            override fun onResponse(call: Call<List<VideoDTO>>, response: Response<List<VideoDTO>>) {
                if (response.isSuccessful) {
                    val videoDtos = response.body() ?: emptyList()
                    val videos = videoDtos.map { it.toVideo() } // Convert ugly data to clean Video objects
                    onSuccess(videos.filter { v -> v.video_file == null })
                } else {
                    onError("Erreur serveur : ${response.code()}")
                }
            }

            override fun onFailure(call: Call<List<VideoDTO>>, t: Throwable) {
                onError("Erreur réseau : ${t.message}")
            }
        })
    }
    fun fetchRenderedVideosOfProject(id:Int, onSuccess: (List<Video>) -> Unit, onError: (String) -> Unit){
        apiService.getVideosOfProject(id).enqueue(object : Callback<List<VideoDTO>> {
            override fun onResponse(call: Call<List<VideoDTO>>, response: Response<List<VideoDTO>>) {
                if (response.isSuccessful) {
                    val videoDtos = response.body() ?: emptyList()
                    videoDtos.forEach {
                        Log.d("VideoRepository", it.toString())
                        it.toVideo()
                    }
                    val videos = videoDtos.map { it.toVideo() } // Convert ugly data to clean Video objects
                    onSuccess(videos.filter { v -> v.video_file != null })
                } else {
                    onSuccess(emptyList())
                    //onError("Erreur serveur : ${response.code()} ; id : $id ; ${response.body()}")
                }
            }

            override fun onFailure(call: Call<List<VideoDTO>>, t: Throwable) {
                onError("Erreur réseau : ${t.message}")
            }
        })
    }
    fun fetchUnrenderedVideosOfProject(id:Int, onSuccess: (List<Video>) -> Unit, onError: (String) -> Unit){
        apiService.getVideosOfProject(id).enqueue(object : Callback<List<VideoDTO>> {
            override fun onResponse(call: Call<List<VideoDTO>>, response: Response<List<VideoDTO>>) {
                if (response.isSuccessful) {
                    val videoDtos = response.body() ?: emptyList()
                    val videos = videoDtos.map { it.toVideo() } // Convert ugly data to clean Video objects
                    onSuccess(videos.filter { v -> v.video_file == null })
                } else {
                    onError("Erreur serveur : ${response.code()}")
                }
            }

            override fun onFailure(call: Call<List<VideoDTO>>, t: Throwable) {
                onError("Erreur réseau : ${t.message}")
            }
        })
    }

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

            override fun onFailure(call: Call<Void>, t: Throwable) {
                onError("Erreur réseau : ${t.message}")
            }
        })
    }
    fun createVideo(project_id:Int, measurements: List<Measurement>,
                    name:String, resolution: String, duration:Int,
                    onSuccess: (Confirmation?) -> Unit, onError: (String) -> Unit){
        Log.d("VideoRepository", measurements.map {m -> m.order_id}.toString())
        var v = VideoDTO(0, project_id, measurements.map {m -> m.order_id}.toString(), null, resolution, duration, 0, name)
        apiService.createVideo(v).enqueue(object : Callback<Confirmation> {
            override fun onResponse(call: Call<Confirmation>, response: Response<Confirmation>) {
                if (response.isSuccessful) {
                    onSuccess(response.body())
                } else {
                    onError("Erreur : ${response.code()} ; ${response.body()}")
                }
            }

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