# OverviewFragment.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.overview

    import android.os.Bundle
    import android.view.LayoutInflater
    import android.view.View
    import android.view.ViewGroup
    import androidx.appcompat.app.AlertDialog
    import androidx.constraintlayout.motion.widget.MotionLayout
    import androidx.fragment.app.Fragment
    import androidx.fragment.app.viewModels
    import androidx.navigation.fragment.findNavController
    import androidx.recyclerview.widget.LinearLayoutManager
    import androidx.recyclerview.widget.RecyclerView
    import com.commonsense.android.kotlin.views.extensions.setup
    import dagger.hilt.android.AndroidEntryPoint
    import dk.nsp.mingraviditet.R
    import dk.nsp.mingraviditet.databinding.FragmentOverviewBinding
    import dk.nsp.mingraviditet.ui.main.MainActivity
    import dk.nsp.mingraviditet.ui.main.overview.widget.OverviewPregnancyState
    import dk.nsp.mingraviditet.util.android.motionlayout.MotionLayoutStateEnum
    import dk.nsp.mingraviditet.util.android.recycler.DelegatingListAdapter
    import dk.nsp.mingraviditet.util.extensions.createAndShowWith
    import dk.nsp.mingraviditet.util.extensions.handleToolbarState
    import dk.nsp.mingraviditet.util.extensions.navigateSafe
    import dk.nsp.mingraviditet.util.extensions.observeIfNotHandled
    import dk.nsp.mingraviditet.util.extensions.setupToolbarStateListener

    @AndroidEntryPoint
    class OverviewFragment : Fragment() {
        private lateinit var binding: FragmentOverviewBinding

        private val adapter by lazy {
            DelegatingListAdapter()
        }

        private val vm: OverviewFragmentViewModel by viewModels()

        private var state: MotionLayoutStateEnum = MotionLayoutStateEnum.Start
            set(value) {
                field = value
                (binding.root as? MotionLayout)?.handleToolbarState(value)
            }

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View {
            binding = FragmentOverviewBinding.inflate(LayoutInflater.from(context), null, false)

            vm.usersName.observe(viewLifecycleOwner) {
                binding.toolbarTitle.text = it
            }

            binding.changeDataCard.setOnClickListener {
                findNavController().navigateSafe(OverviewFragmentDirections.actionOverviewFragmentToChangeDataCardBottomSheet())
            }

            binding.recyclerview.setup(
                adapter,
                LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            )

            vm.rowsToShow.observe(viewLifecycleOwner) {
                adapter.submitList(it)
            }

            vm.onFeedbackClicked.observeIfNotHandled(viewLifecycleOwner) {
                findNavController().navigateSafe(OverviewFragmentDirections.actionOverviewFragmentToRateTheAppBottomSheet())
            }

            vm.onUpdateDateOfBirthClicked.observeIfNotHandled(viewLifecycleOwner) {
                findNavController().navigateSafe(
                    OverviewFragmentDirections.actionOverviewFragmentToUpdateDateOfBirthBottomSheet(
                        it
                    )
                )
            }

            vm.onSearchArticleClicked.observeIfNotHandled(viewLifecycleOwner) {
                findNavController().navigateSafe(
                    OverviewFragmentDirections.actionOverviewFragmentToKnowledgeArticleFragment2(
                        it.first,
                        it.second
                    )
                )
            }

            vm.navigateToKnowledge.observeIfNotHandled(viewLifecycleOwner) {
                (activity as? MainActivity)?.navigateToKnowledge()
            }

            vm.navigateToDocumentation.observeIfNotHandled(viewLifecycleOwner) {
                (activity as? MainActivity)?.navigateToDocumentation()
            }

            vm.pregnancyProgress.observe(viewLifecycleOwner) {
                when (it) {
                    is OverviewPregnancyState.ActiveAndGaveBirth,
                    is OverviewPregnancyState.ActiveAndWaitingForBirth,
                    OverviewPregnancyState.None -> {
                        binding.backgroundLayout.setBackgroundResource(R.drawable.ic_bg_circles_01)
                        binding.pregnancyStatusWidget.visibility = View.VISIBLE

                        binding.pregnancyStatusWidget.updateWith(it)
                    }
                    is OverviewPregnancyState.PregnancyEnded -> {
                        binding.backgroundLayout.background = null
                        binding.pregnancyStatusWidget.visibility = View.GONE
                    }
                }
            }

            vm.updateContentListener.observe(viewLifecycleOwner) {
                //This LiveData job updates the state of the pregnancy, but needs to be observed to work.
            }

            vm.dataCardWarning.observeIfNotHandled(viewLifecycleOwner) {
                AlertDialog.Builder(binding.root.context).createAndShowWith(it)
            }

            setupToolbarState()

            return binding.root
        }

        private fun setupToolbarState() {
            setupToolbarStateListener(binding.root) { newState ->
                state = newState
            }

            (binding.root as? MotionLayout)?.handleToolbarState(state)
        }
    }