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.dialog.showLoadingDialog
import com.zxhy.hfilemanagermaster.image.ImageShowActivity
import com.zxhy.hfilemanagermaster.knife.recentPhoto
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 */
class FilesFragment : Fragment() {
    private lateinit var binding: FragmentFilesBinding
    private lateinit var imageAdapter: ImageSelectAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
        }

    }

    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)
                })
            }
        }
        (activity as OverviewActivity).setStatusBarColor(R.color.white)
        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)
        }
        loadData()
    }


    private fun loadData() {
        val dialog = requireContext().showLoadingDialog()
        lifecycleScope.launch(Dispatchers.IO) {
            val list = requireContext().recentPhoto()
            launch(Dispatchers.Main) {
                dialog.dismiss()
                binding.rv.adapter = imageAdapter
                imageAdapter.setData(list)
            }
        }
    }

}