package com.sunzones.ui.main.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sunzones.R
import com.sunzones.domain.model.MonthDaylight
import com.sunzones.ui.theme.NightBottom
import com.sunzones.ui.theme.SunGold
import com.sunzones.ui.theme.SunOrange
import com.sunzones.ui.theme.TextOnDark
import com.sunzones.ui.theme.TextOnDarkSecondary
import com.sunzones.ui.theme.TextShadowDefault
import java.time.LocalDate
import java.time.Month
import java.time.format.TextStyle
import java.util.Locale

@Composable
fun YearlyDaylightChart(
    monthlyDaylight: List<MonthDaylight>,
    modifier: Modifier = Modifier
) {
    val currentMonth = LocalDate.now().month
    val locale = Locale.getDefault()

    Column(
        modifier = modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp)
            .clip(RoundedCornerShape(16.dp))
            .background(Color.Black.copy(alpha = 0.3f))
            .padding(horizontal = 16.dp, vertical = 20.dp)
    ) {
        Text(
            text = stringResource(R.string.yearly_daylight),
            style = MaterialTheme.typography.titleMedium.copy(shadow = TextShadowDefault),
            color = TextOnDark,
            fontWeight = FontWeight.SemiBold,
            modifier = Modifier.padding(bottom = 16.dp)
        )

        monthlyDaylight.forEach { data ->
            val isCurrent = data.month == currentMonth
            MonthRow(
                data = data,
                isCurrent = isCurrent,
                locale = locale
            )
        }
    }
}

@Composable
private fun MonthRow(
    data: MonthDaylight,
    isCurrent: Boolean,
    locale: Locale
) {
    val barHeight = if (isCurrent) 16.dp else 12.dp
    val textColor = if (isCurrent) TextOnDark else TextOnDarkSecondary
    val fontWeight = if (isCurrent) FontWeight.Bold else FontWeight.Normal
    val monthAbbr = data.month.getDisplayName(TextStyle.SHORT, locale).uppercase()

    Row(
        modifier = Modifier
            .fillMaxWidth()
            .let { mod ->
                if (isCurrent) {
                    mod
                        .clip(RoundedCornerShape(8.dp))
                        .background(Color.White.copy(alpha = 0.08f))
                        .padding(vertical = 8.dp, horizontal = 6.dp)
                } else {
                    mod.padding(vertical = 6.dp, horizontal = 6.dp)
                }
            },
        verticalAlignment = Alignment.CenterVertically
    ) {
        Text(
            text = monthAbbr,
            style = MaterialTheme.typography.bodySmall.copy(shadow = TextShadowDefault),
            color = textColor,
            fontWeight = fontWeight,
            fontSize = 12.sp,
            textAlign = TextAlign.End,
            modifier = Modifier.width(40.dp)
        )

        Spacer(modifier = Modifier.width(8.dp))

        DaylightBar(
            sunriseMinute = data.sunriseMinuteOfDay,
            sunsetMinute = data.sunsetMinuteOfDay,
            barHeight = barHeight,
            modifier = Modifier.weight(1f)
        )

        Spacer(modifier = Modifier.width(8.dp))

        Text(
            text = data.dayLengthFormatted,
            style = MaterialTheme.typography.bodySmall.copy(shadow = TextShadowDefault),
            color = textColor,
            fontWeight = fontWeight,
            fontSize = 12.sp,
            textAlign = TextAlign.Start,
            modifier = Modifier.width(56.dp)
        )
    }
}

@Composable
private fun DaylightBar(
    sunriseMinute: Int,
    sunsetMinute: Int,
    barHeight: androidx.compose.ui.unit.Dp,
    modifier: Modifier = Modifier
) {
    val nightColor = NightBottom
    val sunrise = sunriseMinute.coerceIn(0, 1440).toFloat()
    val sunset = sunsetMinute.coerceIn(0, 1440).toFloat()

    val leftWeight = (sunrise / 1440f).coerceAtLeast(0.001f)
    val dayWeight = ((sunset - sunrise) / 1440f).coerceAtLeast(0.001f)
    val rightWeight = ((1440f - sunset) / 1440f).coerceAtLeast(0.001f)

    Row(
        modifier = modifier
            .height(barHeight)
            .clip(RoundedCornerShape(barHeight / 2))
    ) {
        // Night left
        Box(
            modifier = Modifier
                .weight(leftWeight)
                .height(barHeight)
                .background(nightColor)
        )
        // Daylight
        Box(
            modifier = Modifier
                .weight(dayWeight)
                .height(barHeight)
                .background(
                    Brush.horizontalGradient(
                        colors = listOf(SunOrange, SunGold, SunOrange)
                    )
                )
        )
        // Night right
        Box(
            modifier = Modifier
                .weight(rightWeight)
                .height(barHeight)
                .background(nightColor)
        )
    }
}
