package com.fastest.pass.login.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.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.material3.SnackbarHostState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
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.compose.ui.res.stringResource
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.fastest.pass.app.BaseFragment
import com.fastest.pass.R
import com.fastest.pass.app.GenericLoader
import com.fastest.pass.app.MainActivity
import com.fastest.pass.app.Utils
import com.fastest.pass.helpers.BasePreferenceHelper
import com.fastest.pass.login.presentation.ui.components.ClickType
import com.fastest.pass.login.presentation.ui.components.LoginScreen
import com.fastest.pass.login.presentation.viewmodels.LoginViewModel
import com.fastest.pass.login.utils.LoginNavigation
import com.fastest.pass.login.utils.LoginRoute
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 LoginFragment : BaseFragment() {

    val viewmodel: LoginViewModel by viewModels()
    var emailLogin = ""
    var passwordLogin = ""

    @Inject
    lateinit var navigation: LoginNavigation
    @Inject
    lateinit var basePreferenceHelper: BasePreferenceHelper

    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.white))
                                .padding(paddingValues.calculateBottomPadding())
                        ) {
                            val snackBarStateRed = remember { SnackbarHostState() }
                            val loginResponse = viewmodel.loginResponse.value
                            val coroutineScope = rememberCoroutineScope()
                            val context = LocalContext.current
                            val message = loginResponse.response?.message

                            loginResponse.response?.data?.let {
                                if (loginResponse.response?.status == true) {
                                    basePreferenceHelper.setLoggedInState(true)
                                    basePreferenceHelper.saveToken(it.token)
                                    basePreferenceHelper.saveUserInfo(it.userinfo)
                                    val saltKey = Utils.customSaltKey(it.userinfo)
                                    basePreferenceHelper.saveCustomSaltKey(saltKey)
                                    val secretKey = Utils.customSecretKey(it.userinfo)
                                    basePreferenceHelper.saveCustomSecretKey(secretKey)
                                    basePreferenceHelper.savePassword(passwordLogin)
                                    viewmodel.navigateTo(LoginRoute.OpenDashBoardScreen)
                                }
                            }

                            LaunchedEffect(key1 = message) {
                                message?.let {
                                    coroutineScope.launch {
                                        snackBarStateRed.showSnackbar(it)
                                    }
                                }

                            }

                            LoginScreen(
                                clickType = { clickType ->
                                when (clickType) {
                                    ClickType.SIGNUP_CLICK -> {
                                        viewmodel.navigateTo(LoginRoute.OpenSignUp)
                                    }
                                    ClickType.FORGOT_PASSWORD_CLICK -> {
                                        viewmodel.navigateTo(LoginRoute.OpenForgotPassword)
                                    }
                                    ClickType.GO_BACK -> {
                                        viewmodel.navigateTo(LoginRoute.GoBack)
                                    }
                                    ClickType.MASTER_LOGIN -> {
                                        viewmodel.navigateTo(LoginRoute.OpenMasterLoginScreen)
                                    }
                                    ClickType.HOME_SCREEN -> {
                                        viewmodel.navigateTo(LoginRoute.OpenDashBoardScreen)
                                    }
                                    ClickType.LOGIN_CLICK -> {
//                                        viewmodel.login("eng.asix@gmail.com", "password")
                                    }
                                }
                            },
                                onLoginClickCredentials = { email , password, hashPassword ->
                                emailLogin = email
                                passwordLogin = password
                                Log.d("test_api_login", "onLoginClickCredentials => LoginFragment : $emailLogin , $passwordLogin, $hashPassword")
                                viewmodel.login(email, hashPassword)
                            },
                                onShowSnackBar = { text ->
                                    coroutineScope.launch {
                                        snackBarStateRed.showSnackbar(context.getString(text))
                                    }
                                }
                            )

                            ShowCustomSnackBar(snackBarStateRed, R.color.red_login_button, R.color.white)
                            GenericLoader(loginResponse.isLoading)

                        }
                    }
                }
            }
        }
    }
}