# DefaultBottomSheetDialog.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.mingraviditet.ui.widgets

    import android.graphics.Color
    import android.os.Bundle
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import androidx.coordinatorlayout.widget.CoordinatorLayout
    import com.google.android.material.bottomsheet.BottomSheetBehavior
    import com.google.android.material.bottomsheet.BottomSheetDialogFragment
    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.databinding.DefaultBottomSheetBinding
    import dk.nsp.mingraviditet.util.extensions.observeIfNotHandled

    abstract class DefaultBottomSheetDialog : BottomSheetDialogFragment() {
        val binding: DefaultBottomSheetBinding by lazy {
            DefaultBottomSheetBinding.inflate(LayoutInflater.from(context), null, false)
        }

        abstract val viewModel: BottomSheetViewModel

        protected open val forceFullscreen: Boolean = false

        fun addToScrollView(view: View) {
            binding.scrollView.addView(view)
        }

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)

            setStyle(STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme)

            // If content has scrolled we show a shadow below the header
            binding.scrollView.setOnScrollChangeListener { view, _, _, _, _ ->
                binding.headerLayout.isSelected = view.canScrollVertically(-1)
            }
        }

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
            binding.viewModel = viewModel

            viewModel.onClose.observeIfNotHandled(viewLifecycleOwner) {
                dismiss()
            }
        }

        // Open fullscreen
        override fun onStart() {
            super.onStart()

            if (forceFullscreen) {
                if (dialog != null) {
                    val bottomSheet = dialog?.findViewById<View>(R.id.design_bottom_sheet)

                    (bottomSheet?.layoutParams as? CoordinatorLayout.LayoutParams)?.apply {
                        // Create top margin
                        setMargins(0, resources.getDimensionPixelSize(R.dimen.large_spacing), 0, 0)
                        height = ViewGroup.LayoutParams.MATCH_PARENT
                    }
                }

                view?.let {
                    it.post {
                        val parent = it.parent as View
                        val params = parent.layoutParams as CoordinatorLayout.LayoutParams
                        val behavior = params.behavior
                        val bottomSheetBehavior = behavior as BottomSheetBehavior<*>?
                        bottomSheetBehavior?.peekHeight = it.measuredHeight
                        (binding.bottomSheet.parent as View).setBackgroundColor(Color.TRANSPARENT)
                    }
                }
            }
        }

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return binding.root
        }
    }