package com.fastest.pass.forgotpassword.presentation.ui

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.material3.SnackbarHostState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.fragment.app.viewModels
import com.fastest.pass.R
import com.fastest.pass.app.BaseFragment
import com.fastest.pass.app.GenericLoader
import com.fastest.pass.forgotpassword.presentation.ui.components.ClickType
import com.fastest.pass.forgotpassword.presentation.ui.components.ForgotPasswordScreen
import com.fastest.pass.forgotpassword.presentation.viewmodels.ForgotPasswordViewmodel
import com.fastest.pass.forgotpassword.utils.ForgotPasswordNavigation
import com.fastest.pass.forgotpassword.utils.ForgotPasswordRoute
import com.fastest.pass.ui.theme.FastestPassTheme
import com.fastest.pass.views.ShowCustomSnackBar
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class ForgotPasswordFragment : BaseFragment() {

    val viewmodel: ForgotPasswordViewmodel by viewModels()

    @Inject
    lateinit var navigation: ForgotPasswordNavigation

    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 {
                    val coroutineScope = rememberCoroutineScope()
                    val snackBarState = remember { SnackbarHostState() }
                    val snackBarStateRed = remember { SnackbarHostState() }
                    val forgotPasswordResponse = viewmodel.forgotPasswordResponse.value
                    val message = forgotPasswordResponse.response?.message

                    Log.d(
                        "forgotPasswordResponse",
                        "forgotPasswordResponse = ${forgotPasswordResponse.response?.message}"
                    )

                    LaunchedEffect(key1 = forgotPasswordResponse.response?.status) {
                        coroutineScope.launch {
                            message?.let {
                                if (forgotPasswordResponse.response?.status == true) {
                                    snackBarState.showSnackbar(it)
                                    viewmodel.navigateTo(ForgotPasswordRoute.GoBackToLogin)
                                } else if (forgotPasswordResponse.response?.status == false) {
                                    snackBarStateRed.showSnackbar(it)
                                }
                            }
                        }
                    }


                    ForgotPasswordScreen(
                        clickType = { clickType ->
                            when (clickType) {
                                ClickType.GO_BACK -> {
                                    viewmodel.navigateTo(ForgotPasswordRoute.GoBackToLogin)
                                }

                                ClickType.OPEN_FORGOT_VERIFY -> {
                                    viewmodel.navigateTo(ForgotPasswordRoute.OpenForgotVerifyScreen)
                                }

                                ClickType.FORGOT_API -> {}
                            }
                        },
                        onSendButtonClicked = { email ->
                            Log.d("forgotPasswordResponse", "onSendButtonClicked: email = $email")
                            viewmodel.forgotPassword(email)
                        }
                    )
                    ShowCustomSnackBar(snackBarState, R.color.green_text, R.color.white)
                    ShowCustomSnackBar(snackBarStateRed, R.color.red_login_button, R.color.white)
                    GenericLoader(loader = forgotPasswordResponse.isLoading)
                }
            }
        }
    }
}

