import { StringCaser } from "@/utils/utils"
import { findComponentsByKeyValue, getComponentViewBindingId } from "./helpers"

export const wrapBottomSheet = (children) => {
    return `<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    ${children}

</LinearLayout>
    `
}

export const getBottomSheetFragmentName = (component, fragment) => {
    const name = StringCaser(component.props?.name ?? "", { casetype: "capitalize", separator: "" })
    const _fragment = StringCaser(fragment.name ?? "", { casetype: "capitalize", separator: "" })

    return `${name ?? ""}${_fragment}SheetFragment`
}

export const getBottomSheetLayoutName = (component, fragment) => {
    const _fragment = StringCaser(fragment.name ?? "", { casetype: "lowercase", separator: "_" })
  const _name = StringCaser(component.props?.name ?? "", { casetype: "lowercase", separator: "_" })

    return `${_name ?? ""}${_fragment}_bottom_sheet`
}


export const generateBottomSheetFragment = (component, fragment, config) => {
    
    return `package ${config.packageName}.ui
import ${config.name}.R

    import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class ${getBottomSheetFragmentName(component, fragment)} : BottomSheetDialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.${getBottomSheetLayoutName(component, fragment)}, container, false)
    }
}

    `
}

export const generateBottomSheetFragmentInteractions = (fragment) => {
    let sheets = findComponentsByKeyValue(fragment, "name", "BottomSheet")
    if (sheets.length == 0) return "";

    return  sheets.map(sheet => {
        let sheetcaps = StringCaser(getBottomSheetFragmentName(sheet, fragment), {casetype: "capitalize"})

        return` 
        binding.${getComponentViewBindingId(sheet)}.setOnClickListener {
            show${sheetcaps}()
          } 
    `}).join("\n")
}


export const generateBottomSheetFragmentTrigger = (fragment) => {
    let sheets = findComponentsByKeyValue(fragment, "name", "BottomSheet")
    if (sheets.length == 0) return "";

    return  sheets.map(sheet => {
        let sheetVar = StringCaser(getBottomSheetFragmentName(sheet, fragment), {casetype: "camelCase"})
        let sheetcaps = StringCaser(getBottomSheetFragmentName(sheet, fragment), {casetype: "capitalize"})

        return` 
        private fun show${sheetcaps}() {
            val ${sheetVar} = ${getBottomSheetFragmentName(sheet, fragment)}()
            ${sheetVar}.show(requireActivity().supportFragmentManager, ${sheetVar}.tag)
        }  
    `}).join("\n")
}

export const generateBottomSheetFragmentImports = (fragment, config) => {
    let sheets = findComponentsByKeyValue(fragment, "name", "BottomSheet")
    if (sheets.length == 0) return "";

    return  sheets.map(sheet => {
        return`import ${config.packageName}.ui.${getBottomSheetFragmentName(sheet, fragment)}`})
.join("\n")
}

