package xyz.magicalbits.smsremote.sim

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.navigation.NavController
import androidx.navigation.findNavController
import xyz.magicalbits.smsremote.MainViewModel
import xyz.magicalbits.smsremote.R
import xyz.magicalbits.smsremote.components.JetchatAppBar
import xyz.magicalbits.smsremote.theme.JetchatTheme

class SimFragment : Fragment() {
    private val viewModel: SimViewModel by viewModels()
    private val activityViewModel: MainViewModel by activityViewModels()

    override fun onAttach(context: Context) {
        super.onAttach(context)
        // Consider using safe args plugin
        val phoneNumber = arguments?.getString("phoneNumber")
        viewModel.setSimData(phoneNumber)
    }

    @OptIn(ExperimentalComposeUiApi::class, ExperimentalMaterial3Api::class)
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val rootView: View = inflater.inflate(R.layout.fragment_profile, container, false)

        rootView.findViewById<ComposeView>(R.id.toolbar_compose_view).apply {
            setContent {
                JetchatTheme {
                    JetchatAppBar(
                        // Reset the minimum bounds that are passed to the root of a compose tree
                        modifier = Modifier.wrapContentSize(),
                        onNavIconPressed = { activityViewModel.openDrawer() },
                        title = { },
                    )
                }
            }
        }

        rootView.findViewById<ComposeView>(R.id.profile_compose_view).apply {
            setContent {
                val simData by viewModel.simData.observeAsState()
                val nestedScrollInteropConnection = rememberNestedScrollInteropConnection()

                JetchatTheme {
                    // TODO flip if condition after integrating API call
                    if (simData != null) {
                        SimError()
                        println("calling sim error")
                    } else {
                        val navController: NavController = rootView.findNavController()
                        SimScreen(
//                            simData = simData!!,
                            // TODO remove fake data after integrating API call
                            simData = SimScreenState(
                                phoneNumber = "+420123456789",
                                conversations = listOf(
                                    ConversationRowState("+15558880000", "last msg", 12345),
                                    ConversationRowState("+15558880111", "last msg", 12345),
                                    ConversationRowState("+15558880333", "last msg", 12345),
                                )
                            ),
                            nestedScrollInteropConnection = nestedScrollInteropConnection,
                            onConversationClicked = {
                                val args = Bundle(1)
                                args.putString("phoneNumber", it)
                                navController.navigate(R.id.action_sim_to_conversation, args)
                            },
                        )
                    }
                }
            }
        }

        return rootView
    }
}