package com.zxhy.hfilemanagermaster.files

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.example.hfilemanagermaster.R
import com.example.hfilemanagermaster.databinding.FragmentFilesBinding
import com.zxhy.hfilemanagermaster.OverviewActivity
import com.zxhy.hfilemanagermaster.image.ImageShowActivity
import com.zxhy.hfilemanagermaster.knife.recentPhoto
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * A simple [Fragment] subclass.
 * Use the [FilesFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class FilesFragment : Fragment() {
    private lateinit var binding: FragmentFilesBinding
    private lateinit var imageAdapter: ImageSelectAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }
        (activity as OverviewActivity).setStatusBarColor(R.color.white)
    }

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

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        imageAdapter = ImageSelectAdapter {
            if (it.exists()) {
                startActivity(Intent(requireContext(), ImageShowActivity::class.java).apply {
                    val bundle = Bundle().apply {
                        putString("Path", it.absolutePath)
                    }
                    putExtras(bundle)
                })
            }
        }
        binding.ivImage.setOnClickListener {
            findNavController().navigate(R.id.imageActivity)

        }
        binding.ivVideo.setOnClickListener {
            findNavController().navigate(R.id.videoActivity)
        }
        binding.ivAudio.setOnClickListener {
            val bundle = Bundle().apply {
                putString("tittle", "Audio")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }
        binding.ivApk.setOnClickListener {
            val bundle = Bundle().apply {
                putString("tittle", "Apk")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }
        binding.ivZip.setOnClickListener {
            val bundle = Bundle().apply {
                putString("tittle", "Zip")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }
        binding.ivLog.setOnClickListener {
            val bundle = Bundle().apply {
                putString("tittle", "Log File")
            }
            findNavController().navigate(R.id.fileBrowseActivity, bundle)
        }

    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch(Dispatchers.IO) {
            val list = requireContext().recentPhoto()
            launch(Dispatchers.Main) {
                binding.rv.adapter = imageAdapter
                imageAdapter.setData(list)
            }
        }
    }

    companion object {
        @JvmStatic
        fun newInstance() =
            FilesFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }
}