package com.kaixed.kchat.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import com.kaixed.kchat.databinding.ActivityLaunchBinding
import com.kaixed.kchat.ui.base.BaseActivity
import com.kaixed.kchat.utils.Constants.MMKV_COMMON_DATA
import com.kaixed.kchat.utils.Constants.STATUS_BAR_HEIGHT
import com.kaixed.kchat.utils.Constants.USER_LOGIN_STATUS
import com.tencent.mmkv.MMKV


class LaunchActivity : BaseActivity<ActivityLaunchBinding>() {

    override fun inflateBinding(): ActivityLaunchBinding {
        return ActivityLaunchBinding.inflate(layoutInflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        val mmkvData: MMKV = MMKV.mmkvWithID(MMKV_COMMON_DATA)
        mmkvData.putInt(STATUS_BAR_HEIGHT, getStatusBarHeight())

        val mmkv = MMKV.defaultMMKV()

        if (mmkv.decodeBool(USER_LOGIN_STATUS)) {
            navigateToMain()
        }

        setOnClickListener()
    }


    @SuppressLint("DiscouragedApi", "InternalInsetResource")
    private fun getStatusBarHeight(): Int {
        var result = 0
        val resourceId: Int =
            resources.getIdentifier("status_bar_height", "dimen", "android")
        if (resourceId > 0) {
            result = resources.getDimensionPixelSize(resourceId)
        }
        return result
    }


    private fun navigateToMain() {
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
        return
    }

    private fun setOnClickListener() {
        binding.tvLogin.setOnClickListener {
            startActivity(Intent(this, LoginActivity::class.java))
        }
        binding.tvRegister.setOnClickListener {
            startActivity(Intent(this, RegisterActivity::class.java))
        }
    }
}