package com.led.scroller.barrage.banner.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import com.led.scroller.barrage.banner.R
import com.led.scroller.barrage.banner.bean.FontColorBean
import com.led.scroller.barrage.banner.databinding.FragmentBackgroundBinding
import com.led.scroller.barrage.banner.databinding.FragmentTextBinding
import com.led.scroller.barrage.banner.sp.AppSP
import com.led.scroller.barrage.banner.ui.adapter.FontColorAdapter

class BackgroundFragment : Fragment() {
    private lateinit var binding: FragmentBackgroundBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentBackgroundBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    override fun onResume() {
        super.onResume()
        // 确保监听器在Fragment可见时重新设置
        if (backgroundFragmentClickListener == null) {
            Log.d("BackgroundFragment", "监听器为空，可能需要重新设置")
        }
    }

    private fun initView() {
        initBgColorAdapter()
    }


    private fun initBgColorAdapter() {
        val sp = AppSP(requireActivity())
        val bgColorId = sp.bgColorResId
        val bgColorList: MutableList<FontColorBean> = mutableListOf()
        // 获取颜色资源数组
        val colorArray = resources.obtainTypedArray(R.array.BgColorArray)
        // 遍历颜色资源数组
        for (i in 0 until colorArray.length()) {
            val colorResId = colorArray.getResourceId(i, 0)
            val colorValue = ContextCompat.getColor(requireContext(), colorResId)
            // 将颜色值转换为十六进制字符串
            val hexColor = String.format("#%06X", 0xFFFFFF and colorValue)
            val fontColor = FontColorBean(colorResId, hexColor, colorResId == bgColorId)
            bgColorList.add(fontColor)
        }
        colorArray.recycle()

        val fontColorAdapter = FontColorAdapter(requireActivity(), bgColorList)
        fontColorAdapter.setOnItemClickListener(object : FontColorAdapter.OnItemClickListener {
            @SuppressLint("NotifyDataSetChanged")
            override fun onItemClick(position: Int) {
                val bean = bgColorList[position]
                for (fontColor in bgColorList) {
                    fontColor.select = false
                }
                bgColorList[position].select = true
                AppSP(requireActivity()).bgColorResId = bean.id
                fontColorAdapter.notifyDataSetChanged()
                backgroundFragmentClickListener?.onBgColorClick()
            }
        })
        binding.colorRv.layoutManager = GridLayoutManager(requireActivity(), 7)
        binding.colorRv.adapter = fontColorAdapter
    }


    private var backgroundFragmentClickListener: BackgroundFragmentClickListener? = null
    fun setBackgroundFragmentClickListener(listener: BackgroundFragmentClickListener) {
        backgroundFragmentClickListener = listener
    }

    interface BackgroundFragmentClickListener {
        fun onBgColorClick()
    }
}