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

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
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.layout.width
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.Surface
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.paint
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
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 androidx.compose.ui.unit.sp
import com.fastest.pass.R
import com.fastest.pass.home.presentation.ui.components.GapLine
import com.fastest.pass.home.presentation.ui.components.SearchBarRow

@Composable
fun SecurityScreen() {
    Box(
        modifier = Modifier
            .fillMaxSize(),
        contentAlignment = Alignment.TopCenter
    ) {
        Column(
            modifier = Modifier,
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(25.dp)
                    .background(colorResource(id = R.color.blue_login))
            ) { }

            Spacer(modifier = Modifier.height(20.dp))
            SearchBarRowSS()
            Spacer(modifier = Modifier.height(20.dp))
            GapLine()
            Column(
                modifier = Modifier
                    .padding(horizontal = 30.dp)
                    .padding(top = 20.dp)
                    .fillMaxSize()
            ) {
                PremiumBox()
                Spacer(modifier = Modifier.height(25.dp))
                AddFeaturesRow(R.drawable.generate_password, R.string.generate_password, R.string.generate_robust)
                Spacer(modifier = Modifier.height(10.dp))
                AddFeaturesRow(R.drawable.sharing_center, R.string.sharing_center, R.string.keep_track_online)
            }
        }
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SearchBarRowSS() {
    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)
            ,
        ) {}

//        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 ColumnScope.PremiumBox() {
    Column(
        modifier = Modifier
            .border(
                border = BorderStroke(1.dp, colorResource(id = R.color.sky_green)),
                shape = RoundedCornerShape(15.dp)
            )
            .clip(RoundedCornerShape(15.dp))
            .background(Color.Transparent)
            .paint(
                painter = painterResource(id = R.drawable.gradient_green_premium),
                contentScale = ContentScale.FillBounds
            )
            .padding(vertical = 15.dp)
            .fillMaxWidth(),
        verticalArrangement = Arrangement.Top,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Image(
            painter = painterResource(id = R.drawable.crown),
            contentDescription = "Crown",
            modifier = Modifier
                .padding(top = 0.dp)
                .size(50.dp)
        )
        Text(
            text = stringResource(id = R.string.upgrade_to_premium),
            color = colorResource(id = R.color.white),
            style = MaterialTheme.typography.headlineLarge,
            lineHeight = 25.sp,
            modifier = Modifier
                .padding(top = 5.dp)
        )
        Text(
            text = stringResource(id = R.string.unlock_all_features),
            color = colorResource(id = R.color.white),
            style = MaterialTheme.typography.displaySmall,
            lineHeight = 25.sp,
            modifier = Modifier
                .padding(top = 5.dp)
        )
    }
}

@Composable
fun ColumnScope.AddFeaturesRow(icon: Int, title: Int, desc: Int) {
    Row(
        modifier = Modifier
            .border(
                border = BorderStroke(1.dp, colorResource(id = R.color.white)),
                shape = RoundedCornerShape(15.dp)
            )
            .clip(RoundedCornerShape(15.dp))
            .background(colorResource(id = R.color.white))
            .padding(vertical = 15.dp)
            .fillMaxWidth(),
        horizontalArrangement = Arrangement.Start,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Surface(
            modifier = Modifier.padding(start = 10.dp),
            color = Color.Transparent
        ) {
            Image(
                painter = painterResource(id = icon),
                contentDescription = "Icon",
                modifier = Modifier
                    .padding(start = 0.dp)
                    .size(34.dp)
                    .weight(1f),
                colorFilter = ColorFilter.tint(colorResource(id = R.color.gray_splash)),
            )
        }

        Surface(
            modifier = Modifier.padding(start = 15.dp, end = 5.dp),
            color = Color.Transparent
        ) {
            Column {
                Text(
                    text = stringResource(id = title),
                    style = MaterialTheme.typography.displayMedium,
                    color = colorResource(id = R.color.gray_splash),
                    maxLines = 1,
                    modifier = Modifier
//                        .weight(1F)
                )
                Spacer(modifier = Modifier.height(1.dp))
                Text(
                    text = stringResource(id = desc),
                    style = MaterialTheme.typography.displaySmall,
                    color = colorResource(id = R.color.gray_text),
                    modifier = Modifier
//                        .weight(1F)
                )
            }
        }

//        Spacer(modifier = Modifier.weight(1f))
//
//        Surface(
//            modifier = Modifier
//                .padding(end = 15.dp)
//                .align(Alignment.CenterVertically),
//            color = Color.Transparent
//        ) {
//            Image(
//                painter = painterResource(id = R.drawable.crown_premium),
//                contentDescription = "Front_Arrow",
//                modifier = Modifier
//                    .padding(start = 0.dp, end = 0.dp)
//                    .size(24.dp)
//                    .weight(1f),
//            )
//        }
    }
}