package com.base.easyfilemanager.activity.home

import android.graphics.Color
import android.graphics.Typeface
import android.os.Environment
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.base.easyfilemanager.databinding.ActivityMainBinding
import com.base.easyfilemanager.fragment.FileCategoryFragment
import com.base.easyfilemanager.fragment.HomeFragment
import com.base.easyfilemanager.fragment.ToolsFragment
import com.base.easyfilemanager.helps.BaseActivity
import com.base.easyfilemanager.helps.EventHelper
import com.base.easyfilemanager.utils.BarUtils
import com.base.easyfilemanager.view.RateStarPop
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.json.JSONObject


class NewMainActivity : BaseActivity<ActivityMainBinding>() {
    override val isLightMode: Boolean = true
    private val homeFragment by lazy {
        HomeFragment()
    }
    private val toolsFragment by lazy {
        ToolsFragment()
    }
    private val managerFragment by lazy {
        FileCategoryFragment()
    }

    private val fragments by lazy {
        mutableListOf(homeFragment, toolsFragment, managerFragment)
    }


    override val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    override fun initView() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)

        binding.idVp.run {
            adapter = object : FragmentStateAdapter(this@NewMainActivity) {
                override fun getItemCount(): Int {
                    return fragments.size
                }

                override fun createFragment(position: Int): Fragment {
                    return fragments[position]
                }
            }
        }

        binding.idVp.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                setPage(position)
            }
        })

        binding.idBottom1.setOnClickListener {
            binding.idVp.currentItem = 0
        }
        binding.idBottom2.setOnClickListener {
            binding.idVp.currentItem = 1
        }
        binding.idBottom3.setOnClickListener {
            binding.idVp.currentItem = 2
        }
        com.base.easyfilemanager.helps.MediaStoreUtils.updateMediaStore(
            this,
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).listFiles()?.map { it.absolutePath }
                ?.toTypedArray()
        )
//        testEvent()
    }

    private fun setPage(p: Int) {
        binding.idBottom1.isSelected = false
        binding.idBottom2.isSelected = false
        binding.idBottom3.isSelected = false
        binding.idTvHome.typeface = Typeface.DEFAULT
        binding.idTvTools.typeface = Typeface.DEFAULT
        when (p) {
            0 -> {
                binding.idBottom1.isSelected = true
                binding.idTvHome.typeface = Typeface.DEFAULT_BOLD
            }

            1 -> {
                binding.idBottom2.isSelected = true
                binding.idTvTools.typeface = Typeface.DEFAULT_BOLD
            }

            2 -> {
                binding.idBottom3.isSelected = true
                binding.idTvFile.typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        RateStarPop.dismiss(this)
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launch(Dispatchers.Main) {
            delay(1000)
            RateStarPop.show(this@NewMainActivity)
        }
    }

//    fun testEvent() = lifecycleScope.launch(Dispatchers.IO) {
//        EventHelper.event("click_start_to_use")
//        EventHelper.event("install_referrer")
//        EventHelper.event("ad_pull_start")
//        EventHelper.event("ad_prepare_show")
//        EventHelper.event("notification_jump")
//        EventHelper.event("ad_pull")
//        EventHelper.event("ad_show")
//        EventHelper.event("ad_click")
//        EventHelper.event("bigimage_ad_click")
//        EventHelper.event("bigimage_ad_show")
//        EventHelper.event("ad_price")
//        EventHelper.event("fcm_receiver")
//        EventHelper.event("fcm_message_received")
//    }
}
