package com.prodhack.moscow2025.presentation.components

import android.util.Log
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
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.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.layout.positionInParent
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.prodhack.moscow2025.R
import com.prodhack.moscow2025.presentation.theme.MoscowHackatonTemplateTheme
import com.prodhack.moscow2025.presentation.theme.Paddings
import com.prodhack.moscow2025.presentation.theme.Shapes
import com.prodhack.moscow2025.presentation.utils.ui.noRippleClickable

@Composable
fun TBottomNavigation(modifier: Modifier = Modifier, selectedPage: Int, onSelect: (Int) -> Unit) {
	Box(
		modifier = modifier
			.fillMaxWidth()
			.background(MaterialTheme.colorScheme.surfaceContainer)
			.padding(vertical = Paddings.small),
		contentAlignment = Alignment.Center
	) {

		val homeIconPos = remember { mutableFloatStateOf(0f) }
		val profileIconPos = remember { mutableFloatStateOf(0f) }

		val indicatorOffset =
			with(LocalDensity.current) {
				val hasPositions = homeIconPos.floatValue != 0f || profileIconPos.floatValue != 0f
				if (!hasPositions) return@with null
				val center = (homeIconPos.floatValue + profileIconPos.floatValue) / 2f
				val target = when (selectedPage) {
					1 -> homeIconPos.floatValue
					2 -> profileIconPos.floatValue
					else -> null
				}
				target?.let { (it - center).toDp() }
			}
		indicatorOffset?.let {
			Box(
				modifier = Modifier
					.size(85.dp, 45.dp)
					.offset(x = animateDpAsState(it).value)
					.background(
						MaterialTheme.colorScheme.primary,
						shape = Shapes.smallRoundedBox
					)
			)
		}
		Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceEvenly) {
			Icon(
				modifier = Modifier
					.size(30.dp)
					.onGloballyPositioned {
						it.parentCoordinates?.positionInParent()?.let {
							homeIconPos.floatValue = it.x
						}
					}
					.noRippleClickable {
						onSelect(0)
					},
				painter = painterResource(R.drawable.ic_home),
				tint = animateColorAsState(if (selectedPage == 1) MaterialTheme.colorScheme.surfaceVariant else MaterialTheme.colorScheme.onSurfaceVariant).value,
				contentDescription = "open tasks screen"
			)

			Icon(
				modifier = Modifier
					.size(30.dp)
					.onGloballyPositioned {
						it.parentCoordinates?.positionInParent()?.let {
							profileIconPos.floatValue = it.x
						}
					}
					.noRippleClickable {
						onSelect(1)
					},
				painter = painterResource(R.drawable.ic_profile),
				tint = animateColorAsState(if (selectedPage == 2) MaterialTheme.colorScheme.surfaceVariant else MaterialTheme.colorScheme.onSurfaceVariant).value,
				contentDescription = "open profile screen"
			)
		}

	}
}

@Preview
@Composable
fun TBottomNavigationPreview() {
	MoscowHackatonTemplateTheme {
		Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Bottom) {
			val page = remember { mutableIntStateOf(0) }
			TBottomNavigation(selectedPage = page.intValue) {
				Log.d("click", it.toString())
				page.intValue = it
			}
		}
	}
}
