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

import android.content.Intent
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.activity.PlayActivity
import com.audio.record.screen.test.adapter.VideoGroupAdapter
import com.audio.record.screen.test.base.BaseFragment
import com.audio.record.screen.test.data.VideoInfo
import com.audio.record.screen.test.data.VideoGroup
import com.audio.record.screen.test.databinding.FragmentVideoBinding
import com.audio.record.screen.test.tool.Common
import com.audio.record.screen.test.tool.VideoFileHelper

class VideoFragment : BaseFragment<FragmentVideoBinding>() {
    private lateinit var videoGroupAdapter:VideoGroupAdapter
    companion object {

        @JvmStatic
        fun newInstance() =
            VideoFragment()
    }

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

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

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

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

    }


    private fun getInfo():MutableList<VideoGroup>?{
        val queryVideoInfoListInFolder =
            VideoFileHelper.queryVideoInfoListInFolder(requireContext(),Common.videosFolderDir)

        if (queryVideoInfoListInFolder.isEmpty()) {
            binding.run {
                layoutEmpty.isVisible = true
                layoutRecent.isVisible = false
            }
            Common.showLog("queryVideoInfoListInFolder.isEmpty()")
            return null
        } else {
            binding.run {
                layoutEmpty.isVisible = false
                layoutRecent.isVisible = true
            }
        }
        val grouped = VideoFileHelper.groupVideosByDay(queryVideoInfoListInFolder)
        val sortedGrouped = grouped.toSortedMap(compareByDescending { it })

        val listOf = mutableListOf<VideoGroup>()
        sortedGrouped.forEach { (date, videos) ->
            listOf.add(VideoGroup(date, videos))
        }
        initRecent(listOf[0].videos[0])

        val removeRecent = removeRecent(listOf)
        if (removeRecent.isEmpty()||removeRecent[0].videos.isEmpty()) {
            Common.showLog("removeRecent.isEmpty()")
            return null
        }
        return removeRecent
    }



    private fun removeRecent(list: MutableList<VideoGroup>): MutableList<VideoGroup> {
        val firstItem = list[0]
        if (firstItem.videos.isNotEmpty()) {
            val updatedFirstItem = firstItem.copy(
                videos = firstItem.videos.drop(1) // 去除 videos 的第一个元素
            )
            list[0] = updatedFirstItem
        }
        return list


    }

    private fun initRecent(videoInfo: VideoInfo) {
        binding.run {
            val duration = Common.formatDuration(videoInfo.duration)
            recentImg.setImageBitmap(videoInfo.thumbnail)
            recentName.text = videoInfo.displayName
            recentTime.text = duration


            layoutRecent.setOnClickListener {
                requireActivity().startActivity(
                    Intent(
                        requireContext(),
                        PlayActivity::class.java
                    ).apply {
                        putExtra(PlayActivity.KEY_URI, videoInfo.uri)
                        putExtra(PlayActivity.KEY_name,videoInfo.displayName)
                    })
            }
        }

    }

}