package melody.offline.music.activity

import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import melody.offline.music.R
import melody.offline.music.databinding.ActivityPrimaryBinding
import melody.offline.music.fragment.MoHomeFragment
import melody.offline.music.fragment.MoMeFragment
import melody.offline.music.fragment.SearchFragment
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select

class PrimaryActivity : MoBaseActivity(), SearchFragment.SearchFragmentCancelClickListener {
    /**
     * musicResponsiveListItemRenderer
     * musicTwoRowItemRenderer
     */

    private lateinit var binding: ActivityPrimaryBinding
    private val mFragments: MutableList<Fragment> = ArrayList()
    private var currentIndex: Int = 0
    private var mCurrentFragment: Fragment? = null

    fun getCurrentFragment(): Fragment? {
        if (mCurrentFragment != null) {
            return mCurrentFragment
        }
        return null
    }

    override suspend fun main() {
        binding = ActivityPrimaryBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initView()
        onReceive()
    }

    private fun initView() {
        initClick()
        initFragment()
    }

    private fun initClick() {
        binding.homeBtn.setOnClickListener {
            updateBtnState(0)
            changeFragment(0)
        }
        binding.searchBtn.setOnClickListener {
            updateBtnState(1)
            changeFragment(1)
        }
        binding.meBtn.setOnClickListener {
            updateBtnState(2)
            changeFragment(2)
        }
    }

    private fun initFragment() {
        mFragments.clear()
        mFragments.add(MoHomeFragment())
        val searchFragment = SearchFragment()
        searchFragment.setButtonClickListener(this)
        mFragments.add(searchFragment)
        mFragments.add(MoMeFragment())
        updateBtnState(0)
        changeFragment(0)
    }

    private fun changeFragment(index: Int) {
        currentIndex = index
        val ft: FragmentTransaction = supportFragmentManager.beginTransaction()
        if (null != mCurrentFragment) {
            ft.hide(mCurrentFragment!!)
        }
        var fragment = supportFragmentManager.findFragmentByTag(
            mFragments[currentIndex].javaClass.name
        )
        if (null == fragment) {
            fragment = mFragments[index]
        }
        mCurrentFragment = fragment

        if (!fragment.isAdded) {
            ft.add(R.id.frame_layout, fragment, fragment.javaClass.name)
        } else {
            ft.show(fragment)
        }
        ft.commit()
    }

    private fun updateBtnState(index: Int) {
        binding.apply {
            homeImg.setImageResource(
                when (index) {
                    0 -> {
                        R.drawable.home_select_icon
                    }

                    else -> R.drawable.home_unselect_icon
                }
            )
            searchImg.setImageResource(
                when (index) {
                    1 -> {
                        binding.tabLayout.visibility = View.GONE
                        R.drawable.search_select_icon
                    }

                    else -> {
                        binding.tabLayout.visibility = View.VISIBLE
                        R.drawable.search_unselect_icon
                    }
                }
            )
            meImg.setImageResource(
                when (index) {
                    2 -> R.drawable.me_select_icon
                    else -> R.drawable.me_unselect_icon
                }
            )
        }
    }


    private suspend fun onReceive() {
        while (isActive) {
            select<Unit> {
                events.onReceive {
                    when (it) {
                        Event.ActivityOnResume -> {
                            activityOnResume()
                        }

                        else -> {}
                    }
                }
            }
        }
    }

    private fun activityOnResume() {
        addMusicPlayerViewToLayout(binding.playMusicLayout)
    }

    override fun onFragmentClick() {
        onBackPressed()
    }


    private var backPressedTime: Long = 0
    private val backToast: Toast by lazy {
        Toast.makeText(baseContext, getString(R.string.exit_main_text), Toast.LENGTH_SHORT)
    }

    override fun onBackPressed() {
        if (currentIndex == 1) {//等于搜索返回响应先退回home
            changeFragment(0)
            updateBtnState(0)
        } else {
            if (backPressedTime + 2000 > System.currentTimeMillis()) {
                super.onBackPressed()
                backToast.cancel()
                return
            } else {
                backToast.show()
            }
            backPressedTime = System.currentTimeMillis()
        }
    }
}