package com.audio.record.screen.test.fragment.child

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.audio.record.screen.test.adapter.ImageGroupAdapter
import com.audio.record.screen.test.base.BaseFragment
import com.audio.record.screen.test.data.ImageGroup
import com.audio.record.screen.test.databinding.FragmentImageBinding
import com.audio.record.screen.test.tool.Common
import com.audio.record.screen.test.tool.VideoFileHelper

class ImageFragment : BaseFragment<FragmentImageBinding>() {

    private lateinit var imageGroupAdapter:ImageGroupAdapter
    companion object {

        @JvmStatic
        fun newInstance() =
            ImageFragment()
    }

    override fun initBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentImageBinding =
        FragmentImageBinding.inflate(inflater, container, false)

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


          imageGroupAdapter = ImageGroupAdapter(requireContext()) {
        }

        binding.videoRecycler.run {
            adapter = imageGroupAdapter
            layoutManager = LinearLayoutManager(requireContext())
        }


    }

    override fun onResume() {
        super.onResume()
        getInfo()?.let {
            imageGroupAdapter.updateData(it)
        }

    }

    private fun getInfo():List<ImageGroup>?{
        val queryVideoInfoListInFolder =
            VideoFileHelper.queryGroupedImagesByDay(requireContext(),Common.imagesFolderDir)

        if (queryVideoInfoListInFolder.isEmpty()||queryVideoInfoListInFolder[0].images.isEmpty()) {
            binding.run {
                layoutEmpty.isVisible = true
            }
            Common.showLog("IMag  isEmpty()")
            return null
        } else {
            binding.run {
                layoutEmpty.isVisible = false
            }
        }
        return queryVideoInfoListInFolder
    }


}