package com.soundapp.soundgags.ui

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.soundapp.soundgags.databinding.ActivityCompleteListBinding
import com.soundapp.soundgags.datame.MainViewModel
import com.soundapp.soundgags.setting.TopBarUtils

class CompleteListActivity : AppCompatActivity() {
    private lateinit var binding: ActivityCompleteListBinding
    //    private val mainViewModel:MainViewModel by viewModels()
    private var isFragmentTransitionRunning=false
    private var lastClickTime=0L
    private val CLICK_THROTTLE=300L
    private lateinit var viewModel: MainViewModel

    private lateinit var showFragment: ExploreStyleFragment
    private lateinit var collFragment : SavedItemsFragment
    private lateinit var recordFragment: SoundFragment
    private var currentFragment: Fragment?=null

    private sealed class Tab {
        data object Show:Tab()
        data object Collect:Tab()
        data object Record:Tab()

    }
    private var currentTab:Tab=Tab.Record

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        TopBarUtils.setStatusBar(this.window)
        TopBarUtils.setLightStatusBar(this.window, true)
        binding= ActivityCompleteListBinding.inflate(layoutInflater)
        viewModel=ViewModelProvider(this)[MainViewModel::class.java]
        showFragment=ExploreStyleFragment()
        collFragment=SavedItemsFragment(viewModel)
        recordFragment=SoundFragment(viewModel)
        setContentView(binding.root)
        initViews()
    }

    private fun initViews() {
        switchFragment(Tab.Show)
        binding.llCategory.setOnClickListener { handleTabClick(Tab.Show) }
        binding.llRecord.setOnClickListener { handleTabClick(Tab.Record) }
        binding.llCollect.setOnClickListener { handleTabClick(Tab.Collect) }
    }

    private fun switchFragment(tab: Tab) {
        if (isFragmentTransitionRunning || currentTab == tab) return
        val targetFragment = when (tab) {
            is Tab.Show -> showFragment
            is Tab.Collect -> collFragment
            is Tab.Record -> recordFragment
        }
        Log.d("MainViewActivity", "switchFragment: $tab")
        if (binding.fragmentIn.id == View.NO_ID) return
        Log.d("MainViewActivity", "switchFragment: $tab")
        updateTabUI(tab)
        performFragmentTransaction(targetFragment, tab)
    }
    private fun updateTabUI(tab: Tab) {
//        val defaultColor = getColor(R.color.white)
//        val selectColor = getColor(R.color.text_blue)

        binding.imgCategory.isSelected = tab is Tab.Show
        binding.llCategory.isSelected = tab is Tab.Show
        binding.ltHome.visibility= if (tab is Tab.Show) View.VISIBLE else View.GONE
        binding.imgCollect.isSelected = tab is Tab.Collect
        binding.llCollect.isSelected = tab is Tab.Collect
        binding.ltFavorite.visibility= if (tab is Tab.Collect) View.VISIBLE else View.GONE
        binding.imgRecord.isSelected = tab is Tab.Record
        binding.llRecord.isSelected = tab is Tab.Record
        binding.ltRecord.visibility= if (tab is Tab.Record) View.VISIBLE else View.GONE
    }

    private fun performFragmentTransaction(targetFragment: Fragment, tab: Tab) {
        isFragmentTransitionRunning = true

        supportFragmentManager.beginTransaction().apply {
            setCustomAnimations(
                android.R.anim.fade_in,
                android.R.anim.fade_out
            )

            currentFragment?.takeIf { it.isAdded }?.let {
                hide(it)
            }

            if (targetFragment.isAdded) {
                show(targetFragment)
            } else {
                add(binding.fragmentIn.id, targetFragment, targetFragment::class.java.simpleName)
            }

            setReorderingAllowed(true)
//            addToBackStack(null)
            commit()
        }

        currentFragment = targetFragment
        currentTab = tab
        isFragmentTransitionRunning = false
    }

    private fun handleTabClick(tab: Tab) {
        val currentTime = System.currentTimeMillis()
        if (currentTime - lastClickTime > CLICK_THROTTLE) {
            switchFragment(tab)
            lastClickTime = currentTime
        }
    }

}