package com.doverfuelingsolutions.issp.view

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import com.doverfuelingsolutions.issp.R
import com.doverfuelingsolutions.issp.data.DataStore
import com.doverfuelingsolutions.issp.utils.ActivityUtil
import com.doverfuelingsolutions.issp.utils.PermissionUtil
import com.doverfuelingsolutions.issp.utils.StringUtil
import com.doverfuelingsolutions.issp.utils.log.DFSLog
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedListener {

    private val globalViewModel = GlobalViewModel()

    private val mNavController: NavController by lazy {
        val navHostFragment = supportFragmentManager.findFragmentById(R.id.navActivity) as NavHostFragment
        navHostFragment.navController
    }

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

        PermissionUtil.requestPermissions(this)

        setSupportActionBar(materialToolbar)
        mNavController.addOnDestinationChangedListener(this)
        val appBarConfiguration = AppBarConfiguration(mNavController.graph)
        materialToolbar.setupWithNavController(mNavController, appBarConfiguration)
    }

    override fun onResume() {
        super.onResume()
        ActivityUtil.setFullscreen(this)
    }

    override fun onDestroy() {
        super.onDestroy()

        mNavController.removeOnDestinationChangedListener(this)
    }

    override fun onNavigateUp(): Boolean {
        return super.onNavigateUp()
    }

    override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {
        if (destination.label == StringUtil.get(R.string.app_name)) {
            supportActionBar?.hide()
        } else {
            supportActionBar?.show()
        }
    }

    class GlobalViewModel : ViewModel() {

        val isLogin = MutableLiveData(System.currentTimeMillis() < DataStore.accessTokenExpire)
    }
}