/*
 * Copyright 2026 MagicalBits
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package xyz.magicalbits.smsremote.conversation

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import androidx.compose.ui.platform.ComposeView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.findNavController
import xyz.magicalbits.smsremote.MainViewModel
import xyz.magicalbits.smsremote.R
import xyz.magicalbits.smsremote.data.exampleUiState
import xyz.magicalbits.smsremote.data.exampleUiState2
import xyz.magicalbits.smsremote.theme.JetchatTheme

class ConversationFragment : Fragment() {
    private val activityViewModel: MainViewModel by activityViewModels()

    var phoneNumber: String = ""

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

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View =
        ComposeView(inflater.context).apply {
            layoutParams = LayoutParams(MATCH_PARENT, MATCH_PARENT)

            val uiState =
                if (phoneNumber == "+420123456789") {
                    exampleUiState
                } else {
                    exampleUiState2
                }
            uiState.channelName = phoneNumber

            setContent {
                JetchatTheme {
                    ConversationContent(
                        uiState = uiState,
                        navigateToProfile = { user ->
                            // Click callback
                            val bundle = bundleOf("userId" to user)
                            findNavController().navigate(
                                R.id.nav_profile,
                                bundle,
                            )
                        },
                        onNavIconPressed = {
                            activityViewModel.openDrawer()
                        },
                    )
                }
            }
        }
}
