package io.nexilis.alpha.ui.main

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.consumeWindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.imeNestedScroll
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Headset
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import coil.request.ImageRequest
import io.nexilis.alpha.R
import io.nexilis.alpha.ui.components.AttachmentItemList
import io.nexilis.alpha.ui.components.InputChat
import io.nexilis.alpha.ui.components.toArrayListUri
import io.nexilis.service.core.Orange
import io.nexilis.service.data.entities.Buddy

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun Sharing(
    navController: NavHostController,
    contentPadding: PaddingValues,
    pin: String,
    me: Buddy,
    attachments: AttachmentItemList
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .consumeWindowInsets(contentPadding)
            .imePadding()
            .imeNestedScroll()
    ) {
        LazyColumn(
            modifier = Modifier.weight(1.0f),
            reverseLayout = true,
            verticalArrangement = Arrangement.Top
        ) {
            attachments.list[0]?.let {
                item {
                    if (it.mimeType.startsWith("image")) {
                        AsyncImage(
                            model = ImageRequest.Builder(LocalContext.current)
                                .data(it.uri)
                                .addHeader("Cookie", "PHPSESSID=123;MOBILE=123")
                                .crossfade(true)
                                .build(),
                            placeholder = painterResource(R.drawable.ic_placeholder),
                            contentDescription = "",
                            contentScale = ContentScale.Fit,
                            modifier = Modifier.fillMaxSize()
                        )
                    } else if (it.mimeType.startsWith("video")) {

                    } else if (it.mimeType.startsWith("audio")) {
                        Icon(
                            imageVector = Icons.Default.Headset,
                            contentDescription = null,
                            modifier = Modifier.fillMaxSize(),
                            tint = Color.Orange
                        )
                    } else {

                    }
                }
            }
        }
        InputChat(
            navController = navController,
            pin = pin,
            me = me,
            placeHolderText = "Add a caption...",
            enableAttachment = false,
            enableEmpty = true,
            popAfterSent = true,
            attachments = attachments.toArrayListUri()
        )
    }
}