package com.bearya.kids.analysis

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.observe
import androidx.navigation.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.viewpager2.widget.ViewPager2
import com.bearya.data.entity.Analysis
import com.bearya.kids.databinding.FragmentAnalysisBinding
import kotlinx.coroutines.*
import library.ext.setData

class AnalysisFragment : Fragment() {

    private lateinit var bindView: FragmentAnalysisBinding
    private val viewModel: AnalysisViewModel by viewModels()
    private val args: AnalysisFragmentArgs by navArgs()

    private val playAdapter: AnalysisAdapter by lazy { AnalysisAdapter(args.chapterName) }
    private val previewAdapter: AnalysisPreviewAdapter by lazy { AnalysisPreviewAdapter(args.chapterName) }

    private val pageChangeCallback by lazy {
        object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                previewAdapter.setSelectedIndex(position)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.sectionId.setData(args.sectionId)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        bindView = FragmentAnalysisBinding.inflate(inflater, container, false)
        bindView.lifecycleOwner = viewLifecycleOwner
        return bindView.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        bindView.back.setOnClickListener { it.findNavController().navigateUp() }

        bindView.playPager.adapter = playAdapter
        bindView.playPager.registerOnPageChangeCallback(pageChangeCallback)
        playAdapter.onItemClickListener = { _: View, _: Analysis?, _: Int ->

            cancelHideSectionPreview()

            val visibility = bindView.previewRecyclerView.visibility.takeIf { it == View.VISIBLE }
                    ?.let { View.GONE } ?: View.VISIBLE
            bindView.previewRecyclerView.visibility = visibility
            bindView.back.visibility = visibility

            hideSectionPreview()

        }

        bindView.previewRecyclerView.layoutManager =
                LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        bindView.previewRecyclerView.adapter = previewAdapter
        previewAdapter.onItemClickListener = { _: View, _: Analysis?, position: Int ->
            bindView.playPager.currentItem = position
        }

        viewModel.analysis.observe(viewLifecycleOwner) {
            playAdapter.submitList(it)
            previewAdapter.submitList(it)
        }
    }

    override fun onResume() {
        super.onResume()
        hideSectionPreview()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        bindView.playPager.unregisterOnPageChangeCallback(pageChangeCallback)
    }

    private var launchDelayHide: Job? = null

    private fun hideSectionPreview() {
        launchDelayHide = lifecycleScope.launch {
            delay(3000)
            withContext(Dispatchers.Main) {
                bindView.playPager.visibility.takeIf { it == View.VISIBLE }?.also {
                    bindView.playPager.visibility = View.GONE
                    bindView.back.visibility = View.GONE
                }
            }
        }
    }

    private fun cancelHideSectionPreview() = launchDelayHide?.cancel()

}