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

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
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.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalFocusManager
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.domain.model.CountryInfo

enum class ClickTypeNewItemForm {
    GO_BACK_ADD_NEW_ITEM
}

@Composable
fun NewItemFormScreen(
    screenName: String,
    screenNameType: ClickTypeAddNewItem,
    clickType: (ClickTypeNewItemForm) -> Unit,
    onSearchTextChanged: (String) -> Unit,
    onCountryList: (List<CountryInfo>, String) -> Unit,
    countryListDisplayed: List<CountryInfo>
) {
    val keyboardController = LocalSoftwareKeyboardController.current
    val focusManager = LocalFocusManager.current

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(top = 0.dp)
            .statusBarsPadding()
            .background(colorResource(id = R.color.white))
            .navigationBarsPadding()
            .pointerInput(Unit) {
                detectTapGestures {
                    focusManager.clearFocus()
                    keyboardController?.hide()
                }
            }
    ) {
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .height(25.dp)
                .background(colorResource(id = R.color.blue_login))
        ) { }

        Spacer(modifier = Modifier.height(20.dp))
        ShowNewItemFormHeader(text = screenName) { clickTypeNewItemForm ->
            clickType(clickTypeNewItemForm)
        }
        Spacer(modifier = Modifier.height(20.dp))
        GapLineANI()

        when (screenNameType) {
            ClickTypeAddNewItem.Password -> {
                AddPasswordFormScreen()
            }
            ClickTypeAddNewItem.Bank -> {
                AddBankAccountFormScreen()
            }
            ClickTypeAddNewItem.Payment -> {
                AddPaymentCardFormScreen()
            }
            ClickTypeAddNewItem.Wifi -> {
                AddWifiPasswordFormScreen()
            }
            ClickTypeAddNewItem.Note -> {
                AddSecureNoteFormScreen()
            }
            ClickTypeAddNewItem.Contact -> {
                AddContactInfoScreen(
                    onSearchTextChanged = { searchText ->
                        Log.d("searchText", "NIFS => searchText = $searchText")
                        onSearchTextChanged.invoke(searchText)
                    },
                    onCountryList = { list, text ->
                        Log.d("searchText", "NIFS => list = ${list.size}")
                        onCountryList.invoke(list, text)
                    },
                    countryListDisplayed = countryListDisplayed
                )
            }
            ClickTypeAddNewItem.Driver -> {
                AddDriverLicenseFormScreen(
                    onCountryList = { list, text ->
                        onCountryList.invoke(list, text)
                    },
                    countryListDisplayed = countryListDisplayed
                )
            }
            ClickTypeAddNewItem.Passport -> {
                AddPassportFormScreen(
                    onCountryList = { list, text ->
                        onCountryList.invoke(list, text)
                    },
                    countryListDisplayed = countryListDisplayed
                )
            }
            ClickTypeAddNewItem.GOTO_HOME -> {}
        }

    }
}

@Composable
fun ColumnScope.ShowNewItemFormHeader(text: String, clickType: (ClickTypeNewItemForm) -> Unit) {
    Row(
        modifier = Modifier
            .align(Alignment.Start)
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        IconButton(
            onClick = {
                clickType.invoke(ClickTypeNewItemForm.GO_BACK_ADD_NEW_ITEM)
            },
            modifier = Modifier
                .padding(start = 30.dp)
                .size(24.dp, 24.dp)
        ) {
            Image(
                painter = painterResource(id = R.drawable.arrow_left),
                contentDescription = "Arrow-Back",
                modifier = Modifier.size(24.dp, 24.dp),
                colorFilter = ColorFilter.tint(
                    color = colorResource(id = R.color.gray_splash)
                )
            )
        }
        Surface(
            modifier = Modifier
                .padding(start = 15.dp),
            color = colorResource(id = R.color.transparent)
        ) {
            Text(
                text = text,
                color = colorResource(id = R.color.gray_splash),
                style = MaterialTheme.typography.displayLarge.copy(
                    fontSize = 24.sp
                ),
            )
        }
    }
}