package com.zxhy.fastfilemanagerpro.kokohome

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.google.android.gms.ads.nativead.NativeAd
import com.zxhy.fastfilemanagerpro.MainActivity.Companion.ADMOB_AD_UNIT_ID
import com.zxhy.fastfilemanagerpro.R
import com.zxhy.fastfilemanagerpro.databinding.FragmentKoHomekoBinding
import com.zxhy.fastfilemanagerpro.kokoInternalstorage.KoInternalkoActivity
import com.zxhy.fastfilemanagerpro.kokoads.AdNativeAdvanceEx.refreshAd
import com.zxhy.fastfilemanagerpro.kokodup.KoDupFilekoActivity.Companion.DUP_FILE
import com.zxhy.fastfilemanagerpro.kokodup.KoDupFilekoActivity.Companion.DUP_IMAGE
import com.zxhy.fastfilemanagerpro.kokolistfile.KoListFilekoActivity.Companion.EMPTY_FILE
import com.zxhy.fastfilemanagerpro.kokolistfile.KoListFilekoActivity.Companion.LARGE_FILE
import com.zxhy.fastfilemanagerpro.kokotools.DiskHelper

class KoHomekoFragment : Fragment() {

    private lateinit var binding: FragmentKoHomekoBinding
    private lateinit var context: Context
    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_homeko, container, false)
        binding = FragmentKoHomekoBinding.bind(root)
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        context = requireContext()
        val disk = DiskHelper.getMountInfoList(context)[0]
        binding.tvFree.text = disk.getFreeContent(context)
        binding.tvUsed.text = disk.getUsedContent(context)

        val outPercent = disk.free.toFloat() / disk.total.toFloat()
        val inPercent = 1 - outPercent
        binding.ss.setPercent(outPercent, inPercent)


        binding.cardEmpty.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", EMPTY_FILE)
            }
            findNavController().navigate(R.id.koListFilekoActivity, bundle)
        }
        binding.cardLarge.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", LARGE_FILE)
            }
            findNavController().navigate(R.id.koListFilekoActivity, bundle)
        }
        binding.cardDupImage.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", DUP_IMAGE)
            }
            findNavController().navigate(R.id.koDupFilekoActivity, bundle)
        }
        binding.cardDupFile.setOnClickListener {
            val bundle = Bundle().apply {
                putString("Tittle", DUP_FILE)
            }
            findNavController().navigate(R.id.koDupFilekoActivity, bundle)
        }
        binding.flStorage.setOnClickListener {
            startActivity(Intent(context, KoInternalkoActivity::class.java))
        }
        refreshAd(requireActivity(), binding.flAd, ADMOB_AD_UNIT_ID) {
            currentNativeAd?.destroy()
            currentNativeAd = it
        }
    }

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


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

}