package com.sambath.kunkhmer.app

import android.Manifest
import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.sambath.kunkhmer.R
import com.sambath.kunkhmer.remote.User
import com.sambath.kunkhmer.screen.fighter.FighterFragment
import com.sambath.kunkhmer.util.Const
import com.sambath.kunkhmer.util.ModelPreferencesManager
import kotlinx.android.synthetic.main.app_bar_boxing.iv_account

class MainActivity : AppCompatActivity(), FighterFragment.TabSelectionListener {

    lateinit var navView: BottomNavigationView
    private lateinit var user: User

    // Define a constant at the top of your class or in a companion object
    companion object {
        private const val STORAGE_PERMISSION_REQUEST_CODE = 123
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        navView =  findViewById<BottomNavigationView>(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_home,
                R.id.navigation_dashboard,
                R.id.navigation_live,
                R.id.navigation_event,
                R.id.navigation_shop
            )
        )

        //setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)

        iv_account.setOnClickListener{
            findNavController(R.id.nav_host_fragment_activity_main).navigate(R.id.navigation_account)
        }

        user = ModelPreferencesManager.get<User>(Const.USER_KEY)!!

        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) == PackageManager.PERMISSION_GRANTED
        ) {
            // Permission already granted, proceed with your logic
        } else {
            // Permission not granted, request it
            ActivityCompat.requestPermissions(
                this,
                arrayOf(
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ),
                STORAGE_PERMISSION_REQUEST_CODE
            )
        }
    }

    override fun onTabSelected(tabIndex: Int) {
        val fragment = supportFragmentManager.findFragmentById(R.id.navigation_home)
        if (fragment is FighterFragment) {
            fragment.selectTab(tabIndex)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            STORAGE_PERMISSION_REQUEST_CODE -> {
                // Check if the permission was granted
                if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission granted, proceed with your logic
                } else {
                    // Permission denied, inform the user or take appropriate action
                }
            }
            // Handle other permissions if needed
        }
    }
}