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

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Scaffold
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.res.colorResource
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.ViewModelProvider
import com.fastest.pass.BaseFragment
import com.fastest.pass.R
import com.fastest.pass.home.presentation.ui.components.ClickTypeAddNewItem
import com.fastest.pass.home.presentation.ui.components.ClickTypeNewItemForm
import com.fastest.pass.home.presentation.ui.components.NewItemFormScreen
import com.fastest.pass.home.presentation.viewmodels.AddNewItemsViewModel
import com.fastest.pass.home.presentation.viewmodels.NewItemFormViewModel
import com.fastest.pass.home.utils.AddNewItemsRoute
import com.fastest.pass.home.utils.NewItemFormNavigation
import com.fastest.pass.home.utils.NewItemFormRoute
import com.fastest.pass.ui.theme.FastestPassTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class NewItemFormFragment : BaseFragment() {

    val viewmodel: NewItemFormViewModel by viewModels()
    val viewmodelAddNewItems: AddNewItemsViewModel by activityViewModels()

    @Inject
    lateinit var navigation: NewItemFormNavigation

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        navigation.navigate(this)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireActivity()).apply {
            setContent {
                FastestPassTheme {
                    Scaffold(
                        modifier = Modifier.fillMaxSize()
                    ) { paddingValues ->
                        Box(
                            modifier = Modifier
                                .fillMaxSize()
                                .background(colorResource(id = R.color.blue_login))
                                .padding(paddingValues.calculateBottomPadding())
                        ) {
                            val screenName = viewmodelAddNewItems.itemScreenName.collectAsState()
                            var headerName: String = ""
                            Log.d("test_screen_name", "NIFF::screenName = ${screenName.value}")

                            val countryList = viewmodel.countriesList.collectAsState()
                            Log.d("searchText", "NIFS => countryList = ${countryList.value.size}")

                            when (screenName.value) {
                                ClickTypeAddNewItem.Password -> {
                                   headerName = "Add Password"
                                }
                                ClickTypeAddNewItem.Bank -> {
                                    headerName = "Add Bank Account"
                                }
                                ClickTypeAddNewItem.Payment -> {
                                    headerName = "Add Payment Card"
                                }
                                ClickTypeAddNewItem.Wifi -> {
                                    headerName = "Add Wi-Fi Password"
                                }
                                ClickTypeAddNewItem.Note -> {
                                    headerName = "Add Secure Note"
                                }
                                ClickTypeAddNewItem.Contact -> {
                                    headerName = "Add Contact Info"
                                }
                                ClickTypeAddNewItem.Driver -> {
                                    headerName = "Add Driver's License"
                                }
                                ClickTypeAddNewItem.Passport -> {
                                    headerName = "Add Passport"
                                }
                                ClickTypeAddNewItem.GOTO_HOME -> {}
                            }

                            NewItemFormScreen(
                                headerName,
                                screenName.value,
                                clickType = { clickTypeNewItemForm ->
                                    when (clickTypeNewItemForm) {
                                        ClickTypeNewItemForm.GO_BACK_ADD_NEW_ITEM -> {
                                            viewmodel.navigateTo(NewItemFormRoute.GoBackAddNewItemsScreen)
                                        }
                                    }
                                },
                                onSearchTextChanged = { },
                                onCountryList = { list, text ->
                                    viewmodel.getCountries(text =  text, countryList = list)
                                },
                                countryListDisplayed = countryList.value
                            )
                        }
                    }
                }
            }
        }
    }
}