package com.design.wallpapergallery

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.design.wallpapergallery.data.MainViewModel
import com.design.wallpapergallery.databinding.ActivityBasicBinding
import com.design.wallpapergallery.view.ClassificationFragment
import com.design.wallpapergallery.view.LikeFragment
import com.design.wallpapergallery.view.FindByNameActivity

class BasicActivity : AppCompatActivity() {
    private lateinit var binding: ActivityBasicBinding
    private var isFragmentTransitionRunning = false
    private var lastClickTime = 0L
    private val CLICK_THROTTLE = 300L
    private lateinit var classificationFragment: Fragment
    private lateinit var likeFragment: Fragment
    private lateinit var mainViewModel: MainViewModel
    private var currentFragment: Fragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityBasicBinding.inflate(layoutInflater)
        setContentView(binding.root)
        mainViewModel = ViewModelProvider(this)[MainViewModel::class.java]
        initFragments()
        mainViewModel.debugDatabase()
        initViews()
    }

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

            1 -> {
                binding.imgLike.isSelected = true
                binding.tvEnd.setTextColor(selectColor)
                binding.view1.visibility = View.INVISIBLE
                binding.view2.visibility = View.VISIBLE
            }
        }
    }

    private fun initFragments() {
        classificationFragment = ClassificationFragment()
        likeFragment = LikeFragment()
    }

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

    private fun switchFragment(position: Int) {
        if (isFragmentTransitionRunning) return
        var targetFragment = when (position) {
            0 -> {
                if (currentFragment is ClassificationFragment) return
                classificationFragment
            }

            1 -> {
                if (currentFragment is LikeFragment) return
                likeFragment
            }

            else -> null
        }
        if (targetFragment == null || binding.fragmentIn.id == View.NO_ID) return
        updateTab(position)
        isFragmentTransitionRunning = 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)
            }
            currentFragment?.takeIf { it.isAdded }?.let { hide(it) }
            currentFragment = targetFragment
            commit()
            supportFragmentManager.executePendingTransactions()
            isFragmentTransitionRunning = false
        }
    }
}