package ru.durakscore.app

import java.time.ZoneId
import java.time.format.DateTimeFormatter

private val moscowZone =
    ZoneId.of("Europe/Moscow")

private val moscowDateTimeFormatter =
    DateTimeFormatter.ofPattern(
        "dd.MM.yyyy, HH:mm"
    )

fun formatMoscowDateTime(
    timestamp: Timestamp
): String {

    return timestamp
        .toDate()
        .toInstant()
        .atZone(moscowZone)
        .format(moscowDateTimeFormatter) +
            " МСК"
}

fun formatMoscowHistoryPeriod(
    startedAt: Timestamp,
    finishedAt: Timestamp
): String {

    val start =
        startedAt.toDate()
            .toInstant()
            .atZone(moscowZone)

    val finish =
        finishedAt.toDate()
            .toInstant()
            .atZone(moscowZone)

    return if (
        start.toLocalDate() ==
        finish.toLocalDate()
    ) {

        val date =
            start.format(
                DateTimeFormatter.ofPattern(
                    "dd.MM.yyyy"
                )
            )

        val startTime =
            start.format(
                DateTimeFormatter.ofPattern(
                    "HH:mm"
                )
            )

        val finishTime =
            finish.format(
                DateTimeFormatter.ofPattern(
                    "HH:mm"
                )
            )

        "$date • $startTime–$finishTime МСК"

    } else {

        val formatter =
            DateTimeFormatter.ofPattern(
                "dd.MM.yyyy HH:mm"
            )

        "${start.format(formatter)} → " +
                "${finish.format(formatter)} МСК"
    }
}