package com.zxhy.simplefilesmanager

import android.net.Uri
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.fragment.NavHostFragment
import com.google.android.material.navigation.NavigationBarView
import com.zxhy.simplefilesmanager.databinding.ActivityMainBinding
import com.zxhy.simplefilesmanager.simplefile.SimpleFileFragment.Companion.DEEPLINK_SIMPLE_FILE_URI
import com.zxhy.simplefilesmanager.simplejoke.JoActivityJoLauncher
import com.zxhy.simplefilesmanager.simplejoke.permission.JoPermissionJoCheckEx.jostorejoPermissionCheck
import com.zxhy.simplefilesmanager.simplejoke.permission.JoStoreJoPermissionEx.jorequestjoStoreFollow
import com.zxhy.simplefilesmanager.simplemanager.SimpleManagerFragment.Companion.DEEPLINK_SIMPLE_MANAGER_URI

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private lateinit var navController: NavController
    private lateinit var joActivityJoLauncher: JoActivityJoLauncher
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        joActivityJoLauncher = JoActivityJoLauncher(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
        }
        binding.simpleBnv.itemIconTintList = null
        binding.simpleBnv.setItemIconSizeRes(R.dimen.dp_24)

        navController =
            (supportFragmentManager.findFragmentById(R.id.simple_container) as NavHostFragment).navController

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

            override fun onNothingSelected(parent: AdapterView<*>?) = Unit
            override fun onNavigationItemSelected(item: MenuItem): Boolean {

                when (item.itemId) {
                    R.id.menu_simple_manager -> {
                        runCatching {
                            val navOptions = NavOptions.Builder()
                                .setLaunchSingleTop(true)
                                .setPopUpTo(R.id.simpleFileFragment, true)
                                .build()
                            val deepLinkUri = Uri.parse(DEEPLINK_SIMPLE_MANAGER_URI)
                            navController.navigate(deepLinkUri, navOptions)
                        }

                    }

                    R.id.menu_simple_files -> {
                        runCatching {
                            val navOptions = NavOptions.Builder()
                                .setLaunchSingleTop(true)
                                .setPopUpTo(R.id.simpleManagerFragment, true)
                                .build()
                            val deepLinkUri = Uri.parse(DEEPLINK_SIMPLE_FILE_URI)
                            navController.navigate(deepLinkUri, navOptions)
                        }
                    }
                }
                return true
            }
        })
    }

    override fun onStart() {
        super.onStart()
        if (!jostorejoPermissionCheck()) {
            jorequestjoStoreFollow(joActivityJoLauncher, disAgreeAction = {}, agreeAction = {})
        }

    }
}