package com.paperapp.lenscape.preview

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.paperapp.lenscape.R
import com.paperapp.lenscape.element.MainViewModel
import com.paperapp.lenscape.databinding.ActivitySortBinding
import com.paperapp.lenscape.TopBarUtils

class SortActivity : AppCompatActivity() {
    private lateinit var binding: ActivitySortBinding
    private var isTransitionInProgress = false
    private var lastClickTime = 0L
    private val TAP_THROTTLE = 300L
    private lateinit var tagShowFragment: Fragment
    private lateinit var collectWallFragment: Fragment
    private lateinit var mainViewModel : MainViewModel
    private var activeFragment: Fragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySortBinding.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) {
        val defaultColor = getColor(R.color.black)
        val selectColor = getColor(R.color.green_5)
        binding.tvStart.setTextColor(defaultColor)
        binding.tvEnd.setTextColor(defaultColor)
        when (position) {
            0 -> {
                binding.tvStart.setTextColor(selectColor)
                binding.tvEnd.setTextColor(defaultColor)
                binding.rlSearch.visibility = View.VISIBLE
                binding.viewLine1.visibility = View.VISIBLE
                binding.viewLine2.visibility = View.GONE
                binding.img1.isSelected = true
                binding.img2.isSelected = false
            }

            1 -> {
                binding.tvEnd.setTextColor(selectColor)
                binding.tvStart.setTextColor(defaultColor)
                binding.rlSearch.visibility = View.GONE
                binding.viewLine1.visibility = View.GONE
                binding.viewLine2.visibility = View.VISIBLE
                binding.img1.isSelected = false
                binding.img2.isSelected = true
            }
        }
    }

    private fun initFragments() {
        tagShowFragment = FilterFragment()
        collectWallFragment = LikedWallpapersFragment()
    }

    private fun initializeFragments() {
        // 初始添加两个Fragment，但默认隐藏collectWallFragment
        supportFragmentManager.beginTransaction().apply {
            add(binding.fragmentIn.id, tagShowFragment, "classification")
            add(binding.fragmentIn.id, collectWallFragment, "like")
            hide(collectWallFragment)
            commit()
        }
        activeFragment = tagShowFragment
        updateTab(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, BrowseActivity::class.java))
        }
    }

    private fun switchFragment(position: Int) {
        if (isTransitionInProgress) return

        val targetFragment = when (position) {
            0 -> tagShowFragment
            1 -> collectWallFragment
            else -> return
        }

        // 如果目标Fragment已经是当前显示的Fragment，则直接返回
        if (targetFragment == activeFragment) return

        updateTab(position)
        isTransitionInProgress = true

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

            // 隐藏当前活动的Fragment
            activeFragment?.let { hide(it) }
            // 显示目标Fragment
            show(targetFragment)

            activeFragment = targetFragment
            commit()
        }

        // 使用postDelayed来重置标志位，确保动画完成
        binding.root.postDelayed({
            isTransitionInProgress = false
        }, 300)
    }
}