package com.aiosman.riderpro

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
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.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Preview
@Composable
fun LikePage() {
    val model = LikePageViewModel
    val coroutineScope = rememberCoroutineScope()
    val listState = rememberLazyListState()

    // observe list scrolling
    val reachedBottom: Boolean by remember {
        derivedStateOf {
            val lastVisibleItem = listState.layoutInfo.visibleItemsInfo.lastOrNull()
            lastVisibleItem?.index != 0 && lastVisibleItem?.index == listState.layoutInfo.totalItemsCount - 3
        }
    }
    LaunchedEffect(Unit) {
        model.loader.loadData()
    }
    LaunchedEffect(reachedBottom) {
        if (reachedBottom) model.loader.loadMore()
    }
    StatusBarMaskLayout(
        darkIcons = true,
        maskBoxBackgroundColor = Color(0xFFFFFFFF)
    ) {
        Column(
            modifier = Modifier
                .weight(1f)
                .background(color = Color(0xFFFFFFFF))
                .padding(horizontal = 16.dp)
        ) {
            NoticeScreenHeader("LIKES")
            Spacer(modifier = Modifier.height(28.dp))
            LazyColumn(
                modifier = Modifier.weight(1f),
                state = listState,

                ) {
                items(model.loader.list, key = { it.id }) {
                    LikeItem(it)
                }
                item {
                    BottomNavigationPlaceholder()
                }
            }


        }
    }


}


@Composable
fun LikeItem(itemData: LikeItemData) {
    Box(
        modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp)
    ) {
        Row(
            modifier = Modifier.fillMaxWidth(),
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Image(
                painter = painterResource(id = R.drawable.default_avatar),
                contentDescription = "Like",
                modifier = Modifier.size(40.dp)
            )
            Spacer(modifier = Modifier.width(12.dp))
            Column(
                modifier = Modifier.weight(1f)
            ) {
                Text(itemData.name, fontWeight = FontWeight.Bold, fontSize = 16.sp)
                Spacer(modifier = Modifier.height(5.dp))
                Text("Username", fontSize = 12.sp, color = Color(0x99000000))
            }
            Box {
                Image(
                    painter = painterResource(id = R.drawable.follow_bg),
                    contentDescription = "Like",
                    modifier = Modifier
                        .width(79.dp)
                        .height(24.dp)
                )
                Text(
                    "FOLLOW",
                    fontSize = 14.sp,
                    color = Color(0xFFFFFFFF),
                    modifier = Modifier.align(
                        Alignment.Center
                    )
                )
            }

        }
    }
}