package com.fastest.pass.signup.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.ui.platform.ComposeView
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import com.fastest.pass.app.BaseFragment
import com.fastest.pass.app.GenericLoader
import com.fastest.pass.app.Utils
import com.fastest.pass.helpers.BasePreferenceHelper
import com.fastest.pass.login.presentation.viewmodels.LoginViewModel
import com.fastest.pass.signup.presentation.ui.components.ClickType
import com.fastest.pass.signup.presentation.ui.components.SignUpScreen
import com.fastest.pass.signup.presentation.viewmodels.SignUpViewModel
import com.fastest.pass.signup.utils.SignUpNavigation
import com.fastest.pass.signup.utils.SignUpRoute
import com.fastest.pass.ui.theme.FastestPassTheme
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject

@AndroidEntryPoint
class SignUpFragment : BaseFragment() {

    val viewmodel: SignUpViewModel by viewModels()
    val viewmodelLogin: LoginViewModel by viewModels()
    var emailSU = ""
    var passwordSU = ""
    var hashPasswordSU = ""

    @Inject
    lateinit var navigation: SignUpNavigation
    @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 {

                    val signupResponse = viewmodel.signupResponse.value
                    val loginResponse = viewmodelLogin.loginResponse.value
                    Log.d("test_signup_api", "signupResponse = ${signupResponse.response?.message}")
                    Log.d("test_signup_api", "loginResponse = ${loginResponse.response?.message}")

                    if (signupResponse.response?.status == true) {
                        viewmodelLogin.login(emailSU, hashPasswordSU)
                    }

                    loginResponse.response?.let { it ->
                        if (it.status) {
                            basePreferenceHelper.setLoggedInState(true)
                            basePreferenceHelper.saveToken(it.data?.token ?: "")
                            it.data?.userinfo?.let {
                                it1 -> basePreferenceHelper.saveUserInfo(it1)
                                val saltKey = Utils.customSaltKey(it1)
                                basePreferenceHelper.saveCustomSaltKey(saltKey)
                                val secretKey = Utils.customSecretKey(it1)
                                basePreferenceHelper.saveCustomSecretKey(secretKey)
                                basePreferenceHelper.savePassword(passwordSU)
                            }
                            viewmodel.navigateTo(SignUpRoute.OpenDashBoardScreen)
                        }
                    }


                    SignUpScreen(
                        clickType = { clickType ->
                            when (clickType) {
                                ClickType.GO_BACK -> {
                                    viewmodel.navigateTo(SignUpRoute.GoBack)
                                }
                                ClickType.GO_TO_MASTER_SIGNUP -> {
                                    viewmodel.navigateTo(SignUpRoute.OpenMasterSignupScreen)
                                }
                                ClickType.GO_TO_LOGIN -> {
                                    viewmodel.navigateTo(SignUpRoute.OpenLoginScreen)
                                }
                            }
                        },
                        onSignupClickCredentials = { email, password, hashPassword ->
                            emailSU = email
                            passwordSU = password
                            hashPasswordSU = hashPassword
                            Log.d("test_signup_api", "onSignupClickCredentials => $emailSU, $passwordSU")
                            viewmodel.signup(email, hashPassword)
                        }
                    )

                    GenericLoader(signupResponse.isLoading || loginResponse.isLoading)
                }
            }
        }
    }
}