package com.zxhy.fastfilemanagerpro.kokomanager

import android.content.Context
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.google.android.gms.ads.nativead.NativeAd
import com.zxhy.fastfilemanagerpro.MainActivity
import com.zxhy.fastfilemanagerpro.R
import com.zxhy.fastfilemanagerpro.databinding.FragmentKoManagerkoBinding
import com.zxhy.fastfilemanagerpro.kokoads.AdNativeAdvanceEx
import com.zxhy.fastfilemanagerpro.kokogridfile.KoGridFilekoActivity.Companion.IMAGE_FILE
import com.zxhy.fastfilemanagerpro.kokogridfile.KoGridFilekoActivity.Companion.VIDEO_FILE
import com.zxhy.fastfilemanagerpro.kokolistfile.KoListFilekoActivity.Companion.APK_FILE
import com.zxhy.fastfilemanagerpro.kokolistfile.KoListFilekoActivity.Companion.AUDIO_FILE
import com.zxhy.fastfilemanagerpro.kokolistfile.KoListFilekoActivity.Companion.DOCUMENTS_FILE
import com.zxhy.fastfilemanagerpro.kokolistfile.KoListFilekoActivity.Companion.LOG_FILE
import com.zxhy.fastfilemanagerpro.kokolistfile.KoListFilekoActivity.Companion.ZIP_FILE
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class KoManagerkoFragment : Fragment() {

    private lateinit var binding: FragmentKoManagerkoBinding
    private val viewModel: KoManagerkoViewModel by viewModels()
    private lateinit var context: Context
    private lateinit var adapter: KoRecentkoAdapter
    private var currentNativeAd: NativeAd? = null

    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_ko_managerko, container, false)
        binding = FragmentKoManagerkoBinding.bind(root)
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        context = requireContext()
        binding.llImage.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", IMAGE_FILE)
            }
            findNavController().navigate(R.id.koGridFilekoActivity, bundle)
        }
        binding.llVideo.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", VIDEO_FILE)
            }
            findNavController().navigate(R.id.koGridFilekoActivity, bundle)
        }
        binding.llAudio.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", AUDIO_FILE)
            }
            findNavController().navigate(R.id.koListFilekoActivity, bundle)
        }
        binding.llLogFile.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", LOG_FILE)
            }
            findNavController().navigate(R.id.koListFilekoActivity, bundle)
        }
        binding.llZip.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", ZIP_FILE)
            }
            findNavController().navigate(R.id.koListFilekoActivity, bundle)
        }
        binding.llApk.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", APK_FILE)
            }
            findNavController().navigate(R.id.koListFilekoActivity, bundle)
        }
        binding.llDocuments.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", DOCUMENTS_FILE)
            }
            findNavController().navigate(R.id.koListFilekoActivity, bundle)
        }

        adapter = KoRecentkoAdapter()
        binding.rvRecent.adapter = adapter
        viewModel.loadRecentImageVideo(context) {
            lifecycleScope.launch(Dispatchers.Main) {
                adapter.setData(it)
            }
        }

        AdNativeAdvanceEx.refreshAd(
            requireActivity(),
            binding.flAd,
            MainActivity.ADMOB_AD_UNIT_ID
        ) {
            currentNativeAd?.destroy()
            currentNativeAd = it
        }
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        currentNativeAd?.destroy()
    }

}