package com.fastest.pass.browse.presentation.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
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.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.DockedSearchBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.fastest.pass.R
import com.fastest.pass.home.domain.model.AddNewItem
import com.fastest.pass.home.presentation.ui.components.AddNewItemRow
import com.fastest.pass.home.presentation.ui.components.GapLine

enum class ClickTypeItemBrowse {
    Password,
    Bank,
    Payment,
    Wifi,
    Note,
    Contact,
    Driver,
    Passport
}

@Composable
fun BrowseScreen(clickTypeItemBrowse: (ClickTypeItemBrowse) -> Unit) {
    Box(
        modifier = Modifier
            .fillMaxSize(),
    ) {
        Column(
            modifier = Modifier,
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(25.dp)
                    .background(colorResource(id = R.color.blue_login))
            ) { }

            Spacer(modifier = Modifier.height(20.dp))
            SearchBarRowBS()
            Spacer(modifier = Modifier.height(20.dp))
            GapLine()
            Spacer(modifier = Modifier.height(0.dp))
            AddNewItemListBS { clickTypeItemBrowseId ->
                clickTypeItemBrowse(clickTypeItemBrowseId)
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ColumnScope.SearchBarRowBS() {
    var searchText by remember { mutableStateOf("") }
    val isActive by remember { mutableStateOf(false) }
    val keyboardController = LocalSoftwareKeyboardController.current

    Row(
        modifier = Modifier
            .fillMaxWidth()
    ) {
        DockedSearchBar(
            query = searchText,
            onQueryChange = {
                searchText = it
            },
            onSearch = {
                keyboardController?.hide()
            },
            active = isActive,
            onActiveChange = {},
            placeholder = {
                Text(
                    text = stringResource(R.string.search),
                    style = MaterialTheme.typography.displayMedium,
                    color = colorResource(id = R.color.gray_splash)
                )
            },
            leadingIcon = {
                Icon(
                    painter = painterResource(id = R.drawable.searchicon),
                    contentDescription = "Search Icon",
                    tint = colorResource(id = R.color.gray_splash),
                    modifier = Modifier
                        .size(21.dp)
                )
            },
            colors = SearchBarDefaults.colors(
                containerColor = colorResource(id = R.color.white),
                dividerColor = Color.Transparent,
                inputFieldColors = TextFieldDefaults.colors(
                    focusedTextColor = colorResource(id = R.color.gray_splash),
                    unfocusedTextColor = colorResource(id = R.color.gray_splash),
                    unfocusedIndicatorColor = colorResource(id = R.color.gray_splash),
                    focusedIndicatorColor = colorResource(id = R.color.gray_splash),
                    disabledIndicatorColor = colorResource(id = R.color.gray_splash),
                    cursorColor = colorResource(id = R.color.gray_splash),
                )
            ),
            modifier = Modifier
                .padding(horizontal = 30.dp)
                .height(50.dp)
                .weight(1F)
                .border(
                    1.dp,
                    color = colorResource(id = R.color.white),
                    shape = RoundedCornerShape(25.dp)
                ),
            shape = RoundedCornerShape(25.dp),
        ) {}

//        Spacer(Modifier.width(10.dp))
//
//        Image(
//            painter = painterResource(id = R.drawable.notification_bell),
//            contentDescription = null,
//            colorFilter = ColorFilter.tint(
//                colorResource(id = R.color.gray_splash)
//            ),
//            modifier = Modifier
//                .padding(end = 30.dp)
//                .height(30.dp)
//                .align(Alignment.CenterVertically)
//        )
    }
}

@Composable
fun AddNewItemListBS(clickType: (ClickTypeItemBrowse) -> Unit) {
    val itemList by remember {
        mutableStateOf(
            listOf(
                AddNewItem("Password","Passwords", R.drawable.password_lock),
                AddNewItem("Bank","Bank Account", R.drawable.bank),
                AddNewItem("Payment","Payment Card", R.drawable.cards),
                AddNewItem("Wifi","Wi-Fi Password", R.drawable.wifi),
                AddNewItem("Note","Secure Note", R.drawable.secure_note),
                AddNewItem("Contact","Contact Info", R.drawable.contact_info),
                AddNewItem("Driver","Driver's License", R.drawable.driver_license),
                AddNewItem("Passport","Passport", R.drawable.passport),
            )
        )
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(horizontal = 30.dp)
            .padding(vertical = 15.dp)
            .background(Color.Transparent)
    ) {
        LazyColumn(
            contentPadding = PaddingValues(top = 0.dp)
        ) {
            items(itemList, itemContent = { item ->
                AddNewItemBrowseRow(item = item) {
                    clickType(it)
                }
            })
        }

//        LazyVerticalGrid(
//            columns = GridCells.Fixed(3),
//            modifier = Modifier.weight(1F),
//            contentPadding = PaddingValues(6.dp)
//        ) {
//            items(itemList) { item ->
//                AddNewItemCard(item = item) { itemName ->
//                    clickType(itemName)
//                }
//            }
//        }
    }

}