package com.adpro.pixelshow.look

import android.content.Intent
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.adpro.pixelshow.R
import com.adpro.pixelshow.data.MainViewModel
import com.adpro.pixelshow.databinding.ActivityFistSelBinding
import com.adpro.pixelshow.util.TopBarUtils

class FirstSelActivity : AppCompatActivity() {
    private lateinit var binding: ActivityFistSelBinding
    private var isTransitionInProgress = false
    private var lastClickTime = 0L
    private val TAP_THROTTLE = 300L
    private lateinit var classFragment: Fragment
    private lateinit var favFragment: Fragment
    private lateinit var mainViewModel : MainViewModel
    private var activeFragment: Fragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFistSelBinding.inflate(layoutInflater)
        setContentView(binding.root)
        mainViewModel  = ViewModelProvider(this)[MainViewModel::class.java]
        initFragments()
        TopBarUtils.setStatusBar(this.window)
        TopBarUtils.setLightStatusBar(this.window,true)
        mainViewModel .debugDatabase()
        initializeFragments()
    }

    private fun updateTab(position: Int) {
        binding.imgHome.isSelected = false
        binding.imgLike.isSelected = false
        val defaultColor = getColor(R.color.colorNotSelect)
        val selectColor = getColor(R.color.green_text)
        binding.tvStart.setTextColor(defaultColor)
        binding.tvEnd.setTextColor(defaultColor)
        when (position) {
            0 -> {
                binding.imgHome.isSelected = true
                binding.tvStart.setTextColor(selectColor)
                binding.rlSearch.visibility = View.VISIBLE
            }

            1 -> {
                binding.imgLike.isSelected = true
                binding.tvEnd.setTextColor(selectColor)
                binding.rlSearch.visibility = View.GONE
            }
        }
    }

    private fun initFragments() {
        classFragment = ClassificationFragment()
        favFragment = LikeFragment()
    }

    private fun initializeFragments() {
        switchFragment(0)
        binding.llHome.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime > TAP_THROTTLE) {
                switchFragment(0)
                lastClickTime = System.currentTimeMillis()
            }
        }
        binding.llLike.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime > TAP_THROTTLE) {
                switchFragment(1)
                lastClickTime = System.currentTimeMillis()
            }
        }
        binding.rlSearch.setOnClickListener {
            Log.d("ClickTest", "RelativeLayout被点击了");
            startActivity(Intent(this, SearchKeyActivity::class.java))
        }
    }

    private fun switchFragment(position: Int) {
        if (isTransitionInProgress) return
        var targetFragment = when (position) {
            0 -> {
                if (activeFragment is ClassificationFragment) return
                classFragment
            }

            1 -> {
                if (activeFragment is LikeFragment) return
                favFragment
            }

            else -> null
        }
        if (targetFragment == null || binding.fragmentIn.id == View.NO_ID) return
        updateTab(position)
        isTransitionInProgress = true
        supportFragmentManager.beginTransaction().apply {
            setCustomAnimations(
                android.R.anim.fade_in,
                android.R.anim.fade_out
            )
            if (targetFragment.isAdded) {
                show(targetFragment)
            } else {
                add(binding.fragmentIn.id, targetFragment)
            }
            activeFragment?.takeIf { it.isAdded }?.let { hide(it) }
            activeFragment = targetFragment
            commit()
            supportFragmentManager.executePendingTransactions()
            isTransitionInProgress = false
        }
    }
}