package com.base.browserwhite.ui.activity

import android.annotation.SuppressLint
import android.app.usage.StorageStatsManager
import android.graphics.Color
import android.os.Build
import android.os.Environment
import android.os.StatFs
import android.os.storage.StorageManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isEmpty
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import com.base.browserwhite.R
import com.base.browserwhite.databinding.ActivityMainBinding
import com.base.browserwhite.ui.fragment.FileFragment
import com.base.browserwhite.ui.fragment.HomeFragment
import com.base.browserwhite.ui.views.DialogViews.showMyAccountDialog
import com.base.browserwhite.utils.BarUtils
import com.base.browserwhite.utils.KotlinExt.toFormatSize
import java.io.File

class MainActivity : BaseActivity<ActivityMainBinding>() {

    private val homeFragment: HomeFragment by lazy {
        HomeFragment()
    }
    private val fileFragment: FileFragment by lazy {
        FileFragment()
    }

    override val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }
    var usedBytes: Long = 0
    var totalBytes: Long = 0


    override fun initView() {
        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())


//        startActivity(Intent(this, LabelStackActivity::class.java))
    }

    override fun initListener() {
        super.initListener()
        binding.llHome.setOnClickListener {
            disSelect()
            binding.llHome.isSelected = true
            changeFragment(homeFragment)
        }
        binding.llFile.setOnClickListener {
            disSelect()
            binding.llFile.isSelected = true

            changeFragment(fileFragment)
        }

        binding.llHome.callOnClick()

        binding.llMy.setOnClickListener {
            showMyAccountDialog()
        }
    }

    private fun changeFragment(fragment: Fragment) {
        val transaction = supportFragmentManager.beginTransaction()
        if (binding.fragmentContainer.isEmpty()) {
            transaction.add(R.id.fragment_container, fragment)
        } else {
            transaction.replace(R.id.fragment_container, fragment)
        }
        transaction.commit()
    }


    private fun disSelect() {
        binding.llHome.isSelected = false
        binding.llFile.isSelected = false
    }

    override fun onResume() {
        super.onResume()
        showStorage()
    }

    private fun showStorage() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            val storageStatsManager = getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager

            Thread {
                totalBytes = storageStatsManager.getTotalBytes(StorageManager.UUID_DEFAULT)
                usedBytes = totalBytes - storageStatsManager.getFreeBytes(StorageManager.UUID_DEFAULT)
            }.start()

        } else {
            val path1: File = Environment.getDataDirectory()
            val stat1 = StatFs(path1.path)
            val path2: File = Environment.getExternalStorageDirectory()
            val stat2 = StatFs(path2.path)
            totalBytes = (stat1.totalBytes + stat2.totalBytes)
            usedBytes = ((stat1.totalBytes + stat2.totalBytes) - (stat1.availableBytes + stat2.availableBytes))
        }
    }

}