package com.fastest.pass.browse.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.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.fastest.pass.BaseFragment
import com.fastest.pass.R
import com.fastest.pass.browse.presentation.ui.components.ClickTypeItemBrowse
import com.fastest.pass.browse.presentation.ui.components.ClickTypeNewItemBrowse
import com.fastest.pass.browse.presentation.ui.components.NewItemBrowseScreen
import com.fastest.pass.browse.presentation.viewmodels.BrowseViewModel
import com.fastest.pass.browse.presentation.viewmodels.NewItemBrowseViewmodel
import com.fastest.pass.browse.utils.BrowseRoute
import com.fastest.pass.browse.utils.NewItemBrowseNavigation
import com.fastest.pass.browse.utils.NewItemBrowseRoute
import com.fastest.pass.ui.theme.FastestPassTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class NewItemBrowseFragment : BaseFragment() {

    val viewmodel: NewItemBrowseViewmodel by viewModels()
    val browseViewmodel: BrowseViewModel by activityViewModels()

    @Inject
    lateinit var navigation: NewItemBrowseNavigation

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

    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 = browseViewmodel.itemScreenName.collectAsState()
                            var headerName = ""

                            Log.d("screenNameBrowse", "screenName = ${screenName.value},")

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

                            NewItemBrowseScreen(headerName, screenName.value) { clickTypeNewItemBrowse ->
                                when (clickTypeNewItemBrowse) {
                                    ClickTypeNewItemBrowse.GO_BACK_BROWSE -> {
                                        viewmodel.navigateTo(NewItemBrowseRoute.GoBackToBrowseScreen)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }
}