package com.remax.visualnovel.ui.main.actor


import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.dylanc.loadingstateview.BgColorType
import com.remax.visualnovel.R
import com.remax.visualnovel.app.base.BaseBindingFragment
import com.remax.visualnovel.configs.NovelApplication
import com.remax.visualnovel.databinding.FragmentMainActorBinding
import com.remax.visualnovel.utils.Routers
import dagger.hilt.android.AndroidEntryPoint
import kotlin.getValue
import kotlin.math.max


@AndroidEntryPoint
@Route(path = Routers.ROUTE_FRAG_ACTORLIST)
class ActorListFragment : BaseBindingFragment<FragmentMainActorBinding>() {

    private lateinit var mActorAdapter: ActorsAdapter
    private val actorsViewModel by viewModels<ActorListViewModel>()

    override fun onCreated(bundle: Bundle?) {
        setUI()
    }

    override fun backgroundColorType(): BgColorType {
        return BgColorType.TRANSPARENT
    }

    private fun setUI() {
        initTagLayout()
        initRecyclerView()
        initTitleView()
    }

    private fun initTitleView() {
        with(binding) {
            avatarView.loadImageResourceAvatar(R.mipmap.sex_man_big)
            tvDiamondLevel.text = "150"
        }
    }

    private fun initRecyclerView() {
        with(binding) {
            val layoutManager = LayoutManagerHelper.createAdaptiveStaggeredGridLayoutManager()
            mActorsRv.layoutManager = layoutManager
            mActorsRv.addItemDecoration(GridSpacingItemDecoration(16))
            mActorsRv.setHasFixedSize(true)
            mActorsRv.itemAnimator = DefaultItemAnimator()
            mActorAdapter = ActorsAdapter()
            mActorsRv.adapter = mActorAdapter

            val characterList = createSampleData()
            mActorAdapter.setList(characterList)
        }
    }

    private fun initTagLayout() {
        with(binding) {
            val tags = listOf(
                ActorTagBean("1", "Youth"),
                ActorTagBean("2", "Lolita"),
                ActorTagBean("2", "Lolita2"),
                ActorTagBean("3", "Overbearing CEO ABCDEFG Overbearing CEO ABCDEFG Overbearing CEO ABCDEFG"),
                ActorTagBean("3", "ggggggggggg CEO ABCDEFG Overbearing CEO ABCDEFG Overbearing CEO ABCDEFG"),
                ActorTagBean("4", "Uncle"),
                ActorTagBean("5", "Character Status"),
                ActorTagBean("6", "Imouto"),
                ActorTagBean("7", "Fanwork"),
                ActorTagBean("8", "LastLine"),
            )

            tagFlowLayout.setTagDataList(tags)

            tagFlowLayout.setOnTagClickListener { tag ->
                Toast.makeText(context, "Clicked: ${tag.showStr}", Toast.LENGTH_SHORT).show()
            }

            tagFlowLayout.setOnExpandStateChangeListener { isExpanded ->
                Log.d("TagFlowLayout", "Expand state: $isExpanded")
            }
        }
    }

    private fun createSampleData(): List<ActorItem> {
        return listOf(
            ActorItem(
                id = 1,
                avatarRes = R.drawable.avatar1,
                rating = 9.5f,
                characterName = "Lin Feng",
                age = 18,
                tags = listOf("#Xianxia", "#Swordsmanship", "#Cultivation"),
                description = "glory through relentless effort and uncover the conspiracy that destroyed him.",
                workTitle = "The Last Oracle of Kael",
                imageRes = R.drawable.character1
            ),
            ActorItem(
                id = 2,
                avatarRes = R.drawable.avatar2,
                rating = 9.2f,
                characterName = "Yue Ling",
                age = 16,
                tags = listOf("#Fantasy", "#Magic", "#Adventure"),
                description = "A young mage with exceptional talent in elemental magic, on a journey to discover ancient magical artifacts.",
                workTitle = "Elemental Chronicles",
                imageRes = R.drawable.character2
            ),
            ActorItem(
                id = 1,
                avatarRes = R.drawable.avatar1,
                rating = 9.5f,
                characterName = "Lin Feng",
                age = 18,
                tags = listOf("#Xianxia", "#Swordsmanship", "#Cultivation"),
                description = "Once a prodigy, Lin Feng had his cultivation shattered and was cast out of his clan. Now he must reclaim his former glory through relentless effort and uncover the conspiracy that destroyed him.",
                workTitle = "The Last Oracle of Kael",
                imageRes = R.drawable.character1
            ),
            ActorItem(
                id = 2,
                avatarRes = R.drawable.avatar2,
                rating = 9.2f,
                characterName = "Yue Ling",
                age = 16,
                tags = listOf("#Fantasy", "#Magic", "#Adventure"),
                description = "magical artifacts.",
                workTitle = "Elemental Chronicles",
                imageRes = R.drawable.character2
            ),
            ActorItem(
                id = 1,
                avatarRes = R.drawable.avatar1,
                rating = 9.5f,
                characterName = "Lin Feng",
                age = 18,
                tags = listOf("#Xianxia", "#Swordsmanship", "#Cultivation"),
                description = "Once a prodigy, Lin Feng had his cultivation shattered and was cast out of his clan. Now he must reclaim his former glory through relentless effort and uncover the conspiracy that destroyed him.",
                workTitle = "The Last Oracle of Kael",
                imageRes = R.drawable.character1
            ),
            ActorItem(
                id = 2,
                avatarRes = R.drawable.avatar2,
                rating = 9.2f,
                characterName = "Yue Ling",
                age = 16,
                tags = listOf("#Fantasy", "#Magic", "#Adventure"),
                description = "A ancient magical artifacts.",
                workTitle = "Elemental Chronicles",
                imageRes = R.drawable.character2
            ),
            ActorItem(
                id = 1,
                avatarRes = R.drawable.avatar1,
                rating = 9.5f,
                characterName = "Lin Feng",
                age = 18,
                tags = listOf("#Xianxia", "#Swordsmanship", "#Cultivation"),
                description = "ory through relentless effort and uncover the conspiracy that destroyed him.",
                workTitle = "The Last Oracle of Kael",
                imageRes = R.drawable.character1
            ),
            ActorItem(
                id = 2,
                avatarRes = R.drawable.avatar2,
                rating = 9.2f,
                characterName = "Yue Ling",
                age = 16,
                tags = listOf("#Fantasy", "#Magic", "#Adventure"),
                description = "A young mage with exceptional talent in elemental magic, on a journey to discover ancient magical artifacts.",
                workTitle = "Elemental Chronicles",
                imageRes = R.drawable.character2
            )
        )
    }



    companion object {
        fun newInstance(): ActorListFragment {
            return ARouter.getInstance().build(Routers.ROUTE_FRAG_ACTORLIST)
                .navigation() as ActorListFragment
        }
    }

    object LayoutManagerHelper {
        fun createAdaptiveStaggeredGridLayoutManager(): StaggeredGridLayoutManager {
            val displayMetrics = NovelApplication.appContext().resources.displayMetrics
            val screenWidthPx = displayMetrics.widthPixels
            val screenWidthDp = screenWidthPx / displayMetrics.density
            // 计算列数
            val columnCount = when {
                screenWidthDp >= 600 -> {
                    max(3, (screenWidthDp / 200).toInt())
                }
                else -> {
                    2
                }
            }
            return StaggeredGridLayoutManager(columnCount, StaggeredGridLayoutManager.VERTICAL)
        }
    }

}