package com.aiosman.riderpro

import android.app.Activity
import android.content.Intent
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathEffect
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
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 com.google.accompanist.systemuicontroller.rememberSystemUiController


@Preview
@Composable
fun NewPostScreen() {
    val model = NewPostViewModel
    val systemUiController = rememberSystemUiController()
    LaunchedEffect(Unit) {
        systemUiController.setNavigationBarColor(color = Color.Transparent)
    }
    StatusBarMaskLayout(
        darkIcons = true,
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
        ) {
            NewPostTopBar()
            NewPostTextField("Share your adventure…", model.textContent) {
                model.textContent = it
            }
            AddImageGrid()
            AdditionalPostItem()
        }
    }

}

@Composable
fun NewPostTopBar() {
    val navController = LocalNavController.current
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 18.dp, vertical = 10.dp)
    ) {
        Row(
            modifier = Modifier.align(Alignment.CenterStart),
        ) {
            Image(
                painter = painterResource(id = R.drawable.rider_pro_close),
                contentDescription = "Back",
                modifier = Modifier.size(24.dp).clickable {
                    navController.popBackStack()
                }
            )
            Spacer(modifier = Modifier.weight(1f))
            Image(
                painter = painterResource(id = R.drawable.rider_pro_send_post),
                contentDescription = "Send",
                modifier = Modifier.size(24.dp)
            )
        }
    }

}

@Composable
fun NewPostTextField(hint: String, value: String, onValueChange: (String) -> Unit) {

    Box(modifier = Modifier.fillMaxWidth()) {
        BasicTextField(
            value = value,
            onValueChange = onValueChange,
            modifier = Modifier
                .fillMaxWidth()
                .heightIn(200.dp)
                .padding(horizontal = 18.dp, vertical = 10.dp)

        )
        if (value.isEmpty()) {
            Text(
                text = hint,
                color = Color.Gray,
                modifier = Modifier.padding(horizontal = 18.dp, vertical = 10.dp)
            )
        }
    }
}

@OptIn(ExperimentalLayoutApi::class)
@Composable
fun AddImageGrid() {
    val context = LocalContext.current
    var imageUriList by remember { mutableStateOf(listOf<String>()) }
    val pickImageLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            val uri = result.data?.data
            if (uri != null) {
                imageUriList = imageUriList + uri.toString()
            }
        }
    }
    val stroke = Stroke(
        width = 2f,
        pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
    )
    Box(
        modifier = Modifier.fillMaxWidth()
    ) {
        FlowRow(
            modifier = Modifier
                .fillMaxWidth()
                .padding(18.dp),
            verticalArrangement = Arrangement.spacedBy(8.dp),
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            imageUriList.forEach {
                Image(
                    painter = painterResource(id = R.drawable.rider_pro_new_post_add_pic),
                    contentDescription = "Add Image",
                    modifier = Modifier
                        .size(110.dp)
                        .background(Color(0xFFFFFFFF))
                        .drawBehind {
                            drawRoundRect(color = Color(0xFF999999), style = stroke)
                        }
                )
            }
            Box(
                modifier = Modifier
                    .size(110.dp)
                    .background(Color(0xFFFFFFFF))
                    .drawBehind {
                        drawRoundRect(color = Color(0xFF999999), style = stroke)
                    }
                    .clickable {
                        Intent(Intent.ACTION_PICK).apply {
                            type = "image/*"
                            pickImageLauncher.launch(this)
                        }
                    },
            ) {
                Image(
                    painter = painterResource(id = R.drawable.rider_pro_new_post_add_pic),
                    contentDescription = "Add Image",
                    modifier = Modifier
                        .size(48.dp)
                        .align(Alignment.Center)
                )
            }

        }
    }

}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun AdditionalPostItem() {
    val model = NewPostViewModel
    val navController = LocalNavController.current
    var isShowLocationModal by remember { mutableStateOf(false) }
    fun onSelectLocationClick() {
        isShowLocationModal = true
    }
    if (isShowLocationModal) {
        ModalBottomSheet(
            onDismissRequest = {
                isShowLocationModal = false
            },
            containerColor = Color.White

        ) {
            // Sheet content
            SelectLocationModal(
                onClose = {
                    isShowLocationModal = false
                }
            ) {
                isShowLocationModal = false
                model.searchPlaceAddressResult = it
            }
        }
    }
    Column(
        modifier = Modifier.fillMaxWidth()
    ) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 16.dp, horizontal = 24.dp)
                .clickable {
                    onSelectLocationClick()
                }
        ) {
            model.searchPlaceAddressResult?.let {
                SelectedLocation(it) {
                    model.searchPlaceAddressResult = null
                }
            } ?: Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = painterResource(id = R.drawable.rider_pro_add_location),
                    contentDescription = "Location",
                    modifier = Modifier.size(24.dp)
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text("Add Location", color = Color(0xFF333333))
                Spacer(modifier = Modifier.weight(1f))
                Image(
                    painter = painterResource(id = R.drawable.rider_pro_nav_next),
                    contentDescription = "Add Location",
                    modifier = Modifier.size(24.dp)
                )
            }
        }
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(vertical = 16.dp, horizontal = 24.dp)
                .clickable {
                    navController.navigate("EditModification")
                }
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                Image(
                    painter = painterResource(id = R.drawable.rider_pro_modification),
                    contentDescription = "Modification List",
                    modifier = Modifier.size(24.dp)
                )
                Spacer(modifier = Modifier.width(12.dp))
                Text("Modification List", color = Color(0xFF333333))
                Spacer(modifier = Modifier.weight(1f))
                Image(
                    painter = painterResource(id = R.drawable.rider_pro_nav_next),
                    contentDescription = "Modification List",
                    modifier = Modifier.size(24.dp)
                )
            }

        }
    }
}

@Composable
fun SelectedLocation(
    searchPlaceAddressResult: SearchPlaceAddressResult,
    onRemoveLocation: () -> Unit
) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(vertical = 16.dp)
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
        ) {
            Column(
                modifier = Modifier
                    .weight(1f)
                    .padding(end = 16.dp)
            ) {
                Text(searchPlaceAddressResult.name, fontWeight = FontWeight.Bold)
                Spacer(modifier = Modifier.height(4.dp))
                Text(searchPlaceAddressResult.address, color = Color(0xFF9a9a9a))
            }
            Image(
                painter = painterResource(id = R.drawable.rider_pro_close),
                contentDescription = "Next",
                modifier = Modifier
                    .size(24.dp)
                    .clickable {
                        onRemoveLocation()
                    }
            )

        }
    }
}