package com.aiosman.ravenow.ui.post

import androidx.compose.foundation.ExperimentalFoundationApi
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.WindowInsets
import androidx.compose.foundation.layout.asPaddingValues
import androidx.compose.foundation.layout.fillMaxSize
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.systemBars
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.Icon
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Delete
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import coil.compose.rememberAsyncImagePainter
import com.aiosman.ravenow.LocalNavController
import com.aiosman.ravenow.ui.modifiers.noRippleClickable
import com.google.accompanist.systemuicontroller.rememberSystemUiController

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun NewPostImageGridScreen() {
    val model = NewPostViewModel
    val imageList = model.imageList
    val pagerState = rememberPagerState(pageCount = { imageList.size })
    val systemUiController = rememberSystemUiController()
    val paddingValues = WindowInsets.systemBars.asPaddingValues()
    val navController = LocalNavController.current
    val title = "${pagerState.currentPage + 1}/${imageList.size}"

    LaunchedEffect(Unit) {
        systemUiController.setStatusBarColor(Color.Transparent, darkIcons = false)
    }
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.Black)
    ) {
        Column {
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .background(Color(0xFF2e2e2e))
                    .padding(horizontal = 16.dp, vertical = 16.dp)
            ) {
                Column {
                    Spacer(modifier = Modifier.height(paddingValues.calculateTopPadding()))
                    Row(
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        Icon(
                            Icons.AutoMirrored.Default.ArrowBack,
                            contentDescription = "back",
                            modifier = Modifier
                                .size(24.dp)
                                .noRippleClickable {
                                    navController.popBackStack()
                                },
                            tint = Color.White
                        )
                        Text(
                            title,
                            color = Color.White,
                            modifier = Modifier.weight(1f),
                            textAlign = TextAlign.Center,
                            fontSize = 18.sp,
                        )
                        Icon(
                            Icons.Default.Delete,
                            contentDescription = "delete",
                            modifier = Modifier
                                .size(24.dp)
                                .noRippleClickable {
                                    model.deleteImage(pagerState.currentPage)
                                },
                            tint = Color.White
                        )
                    }

                }

            }
            HorizontalPager(
                state = pagerState,
            ) { page ->
                val imageUrl = imageList[page]
                Image(
                    painter = rememberAsyncImagePainter(model = imageUrl.bitmap),
                    contentDescription = "Image $page",
                    modifier = Modifier.fillMaxSize()
                )
            }
        }


    }


}