// Hilt-Dagger
implementation "com.google.dagger:hilt-android:2.44"
kapt "com.google.dagger:hilt-compiler:2.44"

// Retrofit and GSON Factory & livedata
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'


plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
// id 'com.google.gms.google-services'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}

buildscript {
dependencies {
// classpath 'com.google.gms:google-services:4.3.15'
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.44'
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.0.1' apply false
id 'com.android.library' version '8.0.1' apply false
id 'org.jetbrains.kotlin.android' version '1.8.20' apply false
}

@HiltAndroidApp
class MainApplication : Application()

// Interface
interface ApiInterface {
@GET("theme.json")
fun getThemeData(): Call<ThemeModel>
}

//Object
@Module
@InstallIn(SingletonComponent::class)
object RetrofitInstance {

@Singleton
@Provides
fun retrofit(): Retrofit {
return Retrofit.Builder().baseUrl("https://applicationsjson.s3.amazonaws.com/ApplicationMaster/Constitution+of+India/")
.addConverterFactory(GsonConverterFactory.create()).build()
}

@Singleton
@Provides
fun getApiInterface(): ApiInterface {
return retrofit().create(ApiInterface::class.java)
}
}

// MainActivity
@AndroidEntryPoint
class MainActivity : AppCompatActivity()
private lateinit var themeViewModel: ThemeViewModel
themeViewModel = ViewModelProvider(this@SplashActivity)[ThemeViewModel::class.java]
themeViewModel.apiResponse.observe(this@SplashActivity) { authModel ->
authModel?.let {

}
}
themeViewModel.fetchThemeData()

// ViewModel
@HiltViewModel
class ThemeViewModel @Inject constructor(private val apiService: ApiInterface) : ViewModel() {

private val _apiResponse = MutableLiveData<ThemeModel>()
val apiResponse: LiveData<ThemeModel> = _apiResponse

fun fetchThemeData() {
apiService.getThemeData().enqueue(object : retrofit2.Callback<ThemeModel?> {
override fun onResponse(call: Call<ThemeModel?>, response: Response<ThemeModel?>) {
if (response.isSuccessful) {
response.body()?.let {
_apiResponse.postValue(it)
}
} else {
Log.e("TAG", "onResponse: ${response.errorBody()}")
}
}

override fun onFailure(call: Call<ThemeModel?>, t: Throwable) {
Log.e("TAG", "onFailure: ${t.message}")
}
})
}
}