package ru.fincode.tsudesk.feature.schedule.data.remote

import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import ru.fincode.tsudesk.feature.schedule.data.remote.dto.LessonDto

class ScheduleJsonParser {

    private val moshi = Moshi.Builder()
        .add(KotlinJsonAdapterFactory())
        .build()

    private val type = Types.newParameterizedType(
        List::class.java,
        LessonDto::class.java
    )

    private val adapter = moshi.adapter<List<LessonDto>>(type)
    fun parse(json: String): List<LessonDto> =
        adapter.fromJson(json)
            ?: throw IllegalStateException("Schedule JSON is null/invalid")
}
