function findRoomListComponents(data, result = []) {
  if (!Array.isArray(data)) data = [data];
  for (const item of data) {
    if ("components" in item) {
      findRoomListComponents(item.components, result);
    }
    if ("children" in item) {
      findRoomListComponents(item.children, result);
    }
    if (item.name === "RoomList") {
      result.push(item);
    }
  }

  return result;
}

export const buildRoomListImports = (fragment,configuration) => {
  if (findRoomListComponents(fragment).length == 0) return "";

  return `

import ${configuration.name}.databinding.FragmentRoomListBinding
import android.view.Menu
import android.view.MenuInflater
import android.view.View
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import ${configuration.packageName}.adapters.ChatRoomAdapter
import ${configuration.packageName}.data.loadDataData
import ${configuration.packageName}.data.local.AppPreferences
import ${configuration.packageName}.extensions.viewBinding
import ${configuration.packageName}.viewmodels.BaasViewModel
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ext.android.viewModel
 
     `;
};

export const getRoomListFields = (fragment) => {
  if (findRoomListComponents(fragment).length == 0) return "";

  return `
  private val binding by viewBinding(FragmentRoomListBinding::bind)
  private val baasViewModel: BaasViewModel by viewModel()
  private val pref by inject<AppPreferences>()
  var userId = pref.userId

  private lateinit var adapterData: ChatRoomAdapter
    `;
};

export const generateRoomListFragmentBinding = (fragment) => {
  if (findRoomListComponents(fragment).length == 0) return "";

  return `
  val recyclerview637093b5 = binding.recyclerview637093b5
  adapterData = ChatRoomAdapter(emptyList())
  recyclerview637093b5.layoutManager = LinearLayoutManager(requireContext())


  loadDataData(viewLifecycleOwner, baasViewModel, userId!!, adapterData)
  recyclerview637093b5.adapter = adapterData


  adapterData.onItemClick = { roomModel ->

      val args = Bundle()
      args.putString("roomid", ""+roomModel.id)


      findNavController().navigate(R.id.action_loginFragment_to_chatFragment, args)
  }
    `;
};

// export const generateRoomListFunction = (fragment, configuration) => {
//   if (findRoomListComponents(fragment).length == 0) return "";

//   return `
//     private fun setupControls() {
//         barcodeDetector =
//             BarcodeDetector.Builder(activity).setBarcodeFormats(Barcode.ALL_FORMATS).build()

//         cameraSource = CameraSource.Builder(activity, barcodeDetector)
//             .setRequestedPreviewSize(1920, 1080)
//             .setAutoFocusEnabled(true) //you should add this feature
//             .build()

//         binding.cameraSurfaceView.getHolder().addCallback(object : SurfaceHolder.Callback {
//             @SuppressLint("MissingPermission")
//             override fun surfaceCreated(holder: SurfaceHolder) {
//                 try {
//                     //Start preview after 1s delay
//                     cameraSource.start(holder)
//                 } catch (e: IOException) {
//                     e.printStackTrace()
//                 }
//             }

//             @SuppressLint("MissingPermission")
//             override fun surfaceChanged(
//                 holder: SurfaceHolder,
//                 format: Int,
//                 width: Int,
//                 height: Int
//             ) {
//                 try {
//                     cameraSource.start(holder)
//                 } catch (e: IOException) {
//                     e.printStackTrace()
//                 }
//             }

//             override fun surfaceDestroyed(holder: SurfaceHolder) {
//                 cameraSource.stop()
//             }
//         })


//         barcodeDetector.setProcessor(object : Detector.Processor<Barcode> {
//             override fun release() {
//                 Toast.makeText(activity, "Scanner has been closed", Toast.LENGTH_SHORT)
//                     .show()
//             }

//             override fun receiveDetections(detections: Detector.Detections<Barcode>) {
//                 val barcodes = detections.detectedItems
//                 if (barcodes.size() == 1) {
//                     scannedValue = barcodes.valueAt(0).rawValue

//                     activity?.runOnUiThread {
//                         cameraSource.stop()
//                         Toast.makeText(activity, "value- $scannedValue", Toast.LENGTH_SHORT).show()
//                     }
//                 }else
//                 {
//                     Toast.makeText(activity, "value- else", Toast.LENGTH_SHORT).show()

//                 }
//             }
//         })
//     }

//     override fun onDestroy() {
//         super.onDestroy()
//         cameraSource.stop()
//     }
//     `;
// };
