package com.zxhy.fastfilemanagerpro

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import androidx.activity.enableEdgeToEdge
import androidx.annotation.ColorRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdLoader
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.VideoController
import com.google.android.gms.ads.VideoOptions
import com.google.android.gms.ads.nativead.NativeAd
import com.google.android.gms.ads.nativead.NativeAdOptions
import com.google.android.material.navigation.NavigationBarView
import com.zxhy.fastfilemanagerpro.databinding.ActivityMainBinding
import com.zxhy.fastfilemanagerpro.databinding.AdNativeAdvanceBinding
import com.zxhy.fastfilemanagerpro.kokotools.ActivityLauncher
import com.zxhy.fastfilemanagerpro.kokotools.permission.PermissionCheck.storePermissionCheck
import com.zxhy.fastfilemanagerpro.kokotools.permission.StorePermissionEx.requestStoreFollow

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var activityLauncher: ActivityLauncher

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        activityLauncher = ActivityLauncher(this)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        //关键去除itemIconTint影响
        binding.bnv.itemIconTintList = null
        binding.bnv.setItemIconSizeRes(R.dimen.dp_24)

        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.container) as NavHostFragment


        binding.bnv.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener,
            NavigationBarView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {

            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

            override fun onNavigationItemSelected(item: MenuItem): Boolean {

                when (item.itemId) {
                    R.id.nav_manager -> {
                        runCatching {
                            val navOptions = NavOptions.Builder()
                                .setLaunchSingleTop(true)
                                .setPopUpTo(R.id.koHomekoFragment, true)
                                .build()
                            navHostFragment.navController.navigate(
                                R.id.action_koHomekoFragment_to_koManagerkoFragment, null,
                                navOptions
                            )
                        }
                    }

                    R.id.nav_home -> {
                        runCatching {
                            val navOptions = NavOptions.Builder()
                                .setLaunchSingleTop(true)
                                .setPopUpTo(R.id.koManagerkoFragment, true)
                                .build()
                            navHostFragment.navController.navigate(
                                R.id.action_koManagerkoFragment_to_koHomekoFragment,
                                null,
                                navOptions
                            )
                        }
                    }
                }
                return true
            }
        })

        if (!storePermissionCheck()) {
            requestStoreFollow(activityLauncher,
                disAgreeAction = {
                },
                agreeAction = {
                })
        }
    }


    override fun onRestart() {
        super.onRestart()
        (application as MyApplication).kokoOpenAdManager.showAdIfAvailable(this)
    }

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

    companion object {
        const val ADMOB_AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110"
    }
}