package com.apps.sketchmaster.view


import com.apps.sketchmaster.data.MainViewModel
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.ad.maxlibrary.MaxManager
import com.apps.sketchmaster.R
import com.apps.sketchmaster.databinding.ActivityMainViewBinding


class MainViewActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainViewBinding
    private var isFragmentTransitionRunning = false
    private var lastClickTime = 0L
    private val CLICK_THROTTLE = 300L
    private lateinit var categoryFragment: Fragment
    private lateinit var uploadFragment: Fragment
    private lateinit var collectFragment: Fragment
    private lateinit var mainViewModel: MainViewModel
    private var currentFragment: Fragment? = null

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

    override fun onResume() {
        super.onResume()
        mainViewModel.refreshBases()
    }

    private fun updateTab(position: Int) {
        binding.imgCategory.isSelected = false
        binding.imgUpload.isSelected = false
        binding.imgCollect.isSelected = false
        val defaultColor = getColor(R.color.grey)
        val selectColor = getColor(R.color.primary_text)
        binding.tvCategory.setTextColor(defaultColor)
        binding.tvCollect.setTextColor(defaultColor)
        binding.tvUpload.setTextColor(defaultColor)
        when (position) {
            0 -> {
                binding.imgCategory.isSelected = true
                binding.tvCategory.setTextColor(selectColor)
            }
            1 -> {
                binding.imgUpload.isSelected = true
                binding.tvUpload.setTextColor(selectColor)
            }
            2 -> {
                binding.imgCollect.isSelected = true
                binding.tvCollect.setTextColor(selectColor)
            }
        }
    }

    private fun initFragments() {
        categoryFragment = PageShowFragment()
        uploadFragment = PageUploadFragment()
        collectFragment = PageLikeFragment()
    }

    private fun initViews() {
        switchFragment(0,true)
        binding.llCategory.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime > CLICK_THROTTLE) {
                switchFragment(0)
                lastClickTime = System.currentTimeMillis()
            }
        }
        binding.llUpload.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime > CLICK_THROTTLE) {
                switchFragment(1)
                lastClickTime = System.currentTimeMillis()
            }
        }
        binding.llCollect.setOnClickListener {
            if (System.currentTimeMillis() - lastClickTime > CLICK_THROTTLE) {
                switchFragment(2)
                lastClickTime = System.currentTimeMillis()
            }
        }
    }

    private fun switchFragment(position: Int, isFirstTime: Boolean = false) {
        if (isFragmentTransitionRunning) return
        var targetFragment = when (position) {
            0 -> {
                if (currentFragment is PageShowFragment) return
                categoryFragment
            }
            1 -> {
                if (currentFragment is PageUploadFragment) return
                uploadFragment
            }
            2 -> {
                if (currentFragment is PageLikeFragment) return
                collectFragment
            }

            else -> null
        }
        if (targetFragment == null || binding.fragmentIn.id == View.NO_ID) return
        updateTab(position)
        isFragmentTransitionRunning = true
        supportFragmentManager.beginTransaction().apply {
            if (isFirstTime) {
                // 首次加载只使用进入动画
                setCustomAnimations(
                    android.R.anim.fade_in,
                    0,              // 无退出动画（因为没有前一个Fragment）
                )
            } else {
                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
        }
    }
}