package com.zxhy.simplefilesmanager.simplefile

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.zxhy.simplefilesmanager.R
import com.zxhy.simplefilesmanager.databinding.FragmentSimpleFileBinding
import com.zxhy.simplefilesmanager.simpledata.JoJoRecentImage
import com.zxhy.simplefilesmanager.simplejoke.IntentEx.systemActionView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class SimpleFileFragment() : Fragment() {

    private lateinit var binding: FragmentSimpleFileBinding
    private val viewMode: SimpleFileViewModel by viewModels()
    private lateinit var adapter: SimpleRecentAdapter
    private val TAG = "SimpleFileFragment"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_simple_file, container, false)
        binding = FragmentSimpleFileBinding.bind(root)
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        Log.e(TAG, "onViewCreated")
        super.onViewCreated(view, savedInstanceState)
        adapter = SimpleRecentAdapter(requireContext()) {
            systemActionView(requireContext(), it)
        }
        binding.rv.adapter = adapter
        viewMode.joloadjoRecentImage(requireContext()) { list ->
            lifecycleScope.launch(Dispatchers.Main) {
                val datas = list.map { JoJoRecentImage(uri = it) }
                adapter.setData(datas)
            }
        }
        binding.ivImage.setOnClickListener {
            val args = Bundle().apply {
                putString("Tittle", "IMAGE")
            }
            findNavController().navigate(R.id.simpleGridActivity, args)
        }
        binding.ivVideo.setOnClickListener {
            val args = Bundle().apply {
                putString("Tittle", "VIDEO")
            }
            findNavController().navigate(R.id.simpleGridActivity, args)
        }
    }


    companion object {
        const val DEEPLINK_SIMPLE_FILE_URI = "https://www.zxhy.com/simplefile/"
    }

}