package com.kaixed.kchat.ui.activity

import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import androidx.fragment.app.Fragment
import com.kaixed.kchat.R
import com.kaixed.kchat.databinding.ActivityMainBinding
import com.kaixed.kchat.ui.fragment.ContactFragment
import com.kaixed.kchat.ui.fragment.DiscoveryFragment
import com.kaixed.kchat.ui.fragment.HomeFragment
import com.kaixed.kchat.ui.fragment.MineFragment

class MainActivity : BaseActivity(), View.OnClickListener {
    private lateinit var binding: ActivityMainBinding
    private var colorMain = 0
    private var colorBlack = 0

    companion object {
        private const val KEY_HOME_FRAGMENT = 10001
        private const val KEY_CONTACT_FRAGMENT = 10002
        private const val KEY_DISCOVERY_FRAGMENT = 10003
        private const val KEY_MINE_FRAGMENT = 10004
    }

    private val fragments = mutableMapOf<Int, Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        // 设置透明状态栏
        WindowCompat.setDecorFitsSystemWindows(window, false)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        colorMain = ContextCompat.getColor(this, R.color.green)
        colorBlack = ContextCompat.getColor(this, R.color.black)

        initView()
        initFragment()

        updateSelection(KEY_HOME_FRAGMENT)
    }

    private fun initFragment() {
        fragments[KEY_HOME_FRAGMENT] = HomeFragment()
        fragments[KEY_CONTACT_FRAGMENT] = ContactFragment()
        fragments[KEY_DISCOVERY_FRAGMENT] = DiscoveryFragment()
        fragments[KEY_MINE_FRAGMENT] = MineFragment()

        supportFragmentManager.beginTransaction().apply {
            fragments.values.forEach { fragment ->
                add(R.id.fl_main, fragment, fragment.javaClass.simpleName)
                hide(fragment)
            }
            commit()
        }

        showFragment(KEY_HOME_FRAGMENT)
    }

    private fun showFragment(key: Int) {
        supportFragmentManager.beginTransaction().apply {
            fragments.values.forEach { fragment ->
                hide(fragment)
                if (fragments[key] == fragment) {
                    show(fragment)
                }
            }
            commit()
        }
    }


    private fun initView() {
        binding.clHome.setOnClickListener(this)
        binding.clHome.tag = KEY_HOME_FRAGMENT

        binding.clContact.setOnClickListener(this)
        binding.clContact.tag = KEY_CONTACT_FRAGMENT

        binding.clDiscovery.setOnClickListener(this)
        binding.clDiscovery.tag = KEY_DISCOVERY_FRAGMENT

        binding.clMine.setOnClickListener(this)
        binding.clMine.tag = KEY_MINE_FRAGMENT
    }


    override fun onClick(view: View) {
        val key = view.tag as Int
        showFragment(key)
        updateSelection(key)
    }

    private fun updateSelection(selectedKey: Int) {
        clearSelection()
        when (selectedKey) {
            KEY_HOME_FRAGMENT -> {
                binding.ivHome.isSelected = true
                binding.tvHome.setTextColor(colorMain)
            }

            KEY_CONTACT_FRAGMENT -> {
                binding.ivContact.isSelected = true
                binding.tvContact.setTextColor(colorMain)
            }

            KEY_DISCOVERY_FRAGMENT -> {
                binding.ivDiscovery.isSelected = true
                binding.tvDiscovery.setTextColor(colorMain)
            }

            KEY_MINE_FRAGMENT -> {
                binding.ivMine.isSelected = true
                binding.tvMine.setTextColor(colorMain)
            }
        }
    }

    private fun clearSelection() {
        binding.ivHome.isSelected = false
        binding.ivContact.isSelected = false
        binding.ivDiscovery.isSelected = false
        binding.ivMine.isSelected = false

        binding.tvHome.setTextColor(colorBlack)
        binding.tvContact.setTextColor(colorBlack)
        binding.tvDiscovery.setTextColor(colorBlack)
        binding.tvMine.setTextColor(colorBlack)
    }
}
