package com.khmer9.yuki.app

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.constraintlayout.widget.Group
import androidx.core.view.isVisible
import com.khmer9.yuki.R
import com.khmer9.yuki.base.BaseActivity
import com.khmer9.yuki.remote.LoginData
import com.khmer9.yuki.screen.main.MainFragment
import com.khmer9.yuki.util.Const
import com.khmer9.yuki.util.ModelPreferencesManager
import com.khmer9.yuki.util.PrefHelper

class ShareActivity : BaseActivity() {
    lateinit var settingImageView: ImageView
    lateinit var reportImageView: ImageView
    lateinit var soundImageView: ImageView
    lateinit var userNameTextView: TextView
    lateinit var balanceTextView: TextView
    lateinit var refreshImageView: ImageView
    lateinit var loginData: LoginData
    lateinit var groupToolbarIcons: Group
    lateinit var sharePref: PrefHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        ModelPreferencesManager.get<LoginData>(Const.USER_KEY)?.let {
            loginData = it
        }

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        sharePref = App.injectPrefHelper()

        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .add(
                    R.id.container,
                    MainFragment().apply {
                        arguments =
                            Bundle().apply {
                                //putInt(InventoryFragment.COM_INTO_ID_KEY,10)
                            }

                    })
                .addToBackStack(null)
                .commit()
        }

        reportImageView = findViewById<ImageView>(R.id.iv_report)
        settingImageView = findViewById<ImageView>(R.id.iv_setting)
        soundImageView = findViewById<ImageView>(R.id.iv_sound)
        refreshImageView = findViewById<ImageView>(R.id.iv_auto_new)
        groupToolbarIcons = findViewById<Group>(R.id.group_toolbar_icons)
        userNameTextView = findViewById<TextView>(R.id.tv_user_id)
        balanceTextView = findViewById<TextView>(R.id.tv_amount)

        soundImageView.setOnClickListener {
            if (sharePref.getIsMute()) {
                sharePref.setIsMute(false)
                soundImageView.setImageResource(R.drawable.ic_unmute)
            } else {
                sharePref.setIsMute(true)
                soundImageView.setImageResource(R.drawable.ic_mute)
            }
        }

        if (sharePref.getIsMute()) {
            soundImageView.setImageResource(R.drawable.ic_mute)
        } else {
            soundImageView.setImageResource(R.drawable.ic_unmute)
        }
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        //Log.d("Window", "hase focus")
        if (hasFocus) hideSystemUI()
    }

    private fun hideSystemUI() {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                // Set the content to appear under the system bars so that the
                // content doesn't resize when the system bars hide and show.
                or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                // Hide the nav bar and status bar
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }

    override fun onBackPressed() {
        val count = supportFragmentManager.backStackEntryCount
        when (count) {
            1 -> {
                finish()
            }
            else -> {
                groupToolbarIcons.isVisible = true
                super.onBackPressed()
            }
        }
    }

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

    private var confirmDialog: AlertDialog? = null

    override fun onPause() {
        super.onPause()
        App.onAppStop()
    }

    override fun onResume() {
        super.onResume()
        App.onAppStart(token = loginData.token.toString())
        //Log.d("Lifecycle","onResume")
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
    }
}
