package com.radiola.deeplink

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.widget.Toast
import com.radiola.domain.model.DeeplinkService
import com.radiola.domain.model.Track

object DeeplinkNavigator {

    fun openSearch(context: Context, track: Track, service: DeeplinkService) {
        Log.d("DeeplinkNavigator", "openSearch: artist=${track.artist}, song=${track.song}, service=${service.displayName}")
        val url = service.buildSearchUrl(track.artist, track.song)
        Log.d("DeeplinkNavigator", "url=$url")
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
        try {
            context.startActivity(Intent.createChooser(intent, "Открыть в..."))
        } catch (e: Exception) {
            Log.e("DeeplinkNavigator", "Failed to open deeplink", e)
            Toast.makeText(context, "Не удалось открыть ссылку", Toast.LENGTH_SHORT).show()
        }
    }
}
