# ChecklistCard.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.main.knowledge.checklist

    import com.commonsense.android.kotlin.views.extensions.gone
    import com.commonsense.android.kotlin.views.extensions.visible
    import dk.nsp.domain.knowledge.ChecklistItem
    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.databinding.ChecklistCardBinding
    import dk.nsp.mingraviditet.domain.ExpandCollapseEnum
    import dk.nsp.mingraviditet.ui.widgets.AsyncCheckBoxViewModel
    import dk.nsp.mingraviditet.util.android.recycler.BindingViewHolder
    import dk.nsp.mingraviditet.util.android.recycler.DelegationListAdapterInflater
    import dk.nsp.mingraviditet.util.android.recycler.RecyclerAdapterView

    data class ChecklistCardData(
        val item: ChecklistItem,
        val expanded: Boolean,
        val onExpandCollapse: (ExpandCollapseEnum) -> Unit,
        val onCheck: () -> Unit
    )

    class ChecklistCard(val data: ChecklistCardData) : RecyclerAdapterView<ChecklistCardBinding> {

        private val checkboxVm: AsyncCheckBoxViewModel = AsyncCheckBoxViewModel()

        private var _expanded: ExpandCollapseEnum = ExpandCollapseEnum.Collapsed

        override fun bindData(viewHolder: BindingViewHolder<ChecklistCardBinding>) {
            viewHolder.binding.vm = data
            viewHolder.binding.checkbox.vm = checkboxVm

            _expanded = if (data.expanded) ExpandCollapseEnum.Expanded else ExpandCollapseEnum.Collapsed

            checkboxVm.setChecked(data.item.value)

            checkboxVm.onModeChanged = {
                data.onCheck()
            }

            //Enable click on entire card
            viewHolder.binding.root.setOnClickListener {
                checkboxVm.onCheckedToggle()
            }

            viewHolder.binding.chevron.setOnClickListener {
                _expanded = _expanded.toggle()

                data.onExpandCollapse.invoke(_expanded)

                updateExpandCollapsed(viewHolder)
            }

            updateExpandCollapsed(viewHolder)
        }

        private fun updateExpandCollapsed(viewHolder: BindingViewHolder<ChecklistCardBinding>) {
            when (_expanded) {
                ExpandCollapseEnum.Expanded -> {
                    viewHolder.binding.description.visible()
                    viewHolder.binding.chevron.setImageResource(R.drawable.ic_chevron_up)
                }
                ExpandCollapseEnum.Collapsed -> {
                    viewHolder.binding.description.gone()
                    viewHolder.binding.chevron.setImageResource(R.drawable.ic_chevron_down)
                }
            }
        }

        override val createHolderFunction: DelegationListAdapterInflater<ChecklistCardBinding> =
            ChecklistCardBinding::inflate
    }