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

import ru.fincode.tsudesk.core.network.model.NetworkMeta
import ru.fincode.tsudesk.feature.schedule.data.remote.dto.LessonDto
import ru.fincode.tsudesk.feature.schedule.data.remote.dto.ScheduleDto
import ru.fincode.tsudesk.feature.schedule.domain.model.LessonEntity
import ru.fincode.tsudesk.feature.schedule.domain.model.LessonType
import ru.fincode.tsudesk.feature.schedule.domain.model.ScheduleEntity
import javax.inject.Inject

class ScheduleNetworkMapper @Inject constructor() {

    operator fun invoke(dto: ScheduleDto, meta: NetworkMeta): ScheduleEntity =
        ScheduleEntity(
            lessons = dto.map(::mapLesson),
            timestamp = meta.receivedAtMillis ?: meta.finishedAtMillis
        )

    private fun mapLesson(item: LessonDto): LessonEntity =
        LessonEntity(
            date = item.date.trim(),
            time = item.time.trim(),
            name = item.name.trim(),
            typeName = item.typeName.trim(),
            room = item.room.trim(),
            teacher = item.teacher.trim(),
            groupIds = item.groups.map { it.groupCode.trim() },
            type = LessonType.Companion.fromApi(item.type)
        )
}