package com.apps.pixwall.view

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.apps.pixwall.R
import com.apps.pixwall.data.MainViewModel
import com.apps.pixwall.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    private var isFragmentTransitionRunning = false
    private var lastClickTime = 0L
    private val CLICK_THROTTLE = 300L
    private lateinit var categoryFragment: Fragment
    private lateinit var favoriteFragment: Fragment
    private lateinit var mainViewModel: MainViewModel
    private var currentFragment: Fragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.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.grey)
        val selectColor = getColor(R.color.dark_purple)
        binding.tvStart.setTextColor(defaultColor)
        binding.tvEnd.setTextColor(defaultColor)
        when (position) {
            0 -> {
                binding.imgHome.isSelected = true
                binding.tvStart.setTextColor(selectColor)
            }

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

    private fun initFragments() {
        categoryFragment = CategoryFragment()
        favoriteFragment = FavoriteFragment()
    }

    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 {
            startActivity(Intent(this, SearchActivity::class.java))
        }
    }

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

            1 -> {
                if (currentFragment is FavoriteFragment) return
                favoriteFragment
            }

            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
        }
    }
}