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

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.GradientDrawable
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.bean.FontBean
import com.led.scroller.barrage.banner.bean.FontSizeBean
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
import com.led.scroller.barrage.banner.ui.adapter.FontAdapter
import com.led.scroller.barrage.banner.ui.adapter.FontSizeAdapter

class TextFragment : Fragment() {
    private lateinit var binding: FragmentTextBinding

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

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

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

    private fun initView() {
        initFontAdapter()
        initColorAdapter()
        initFontSizeAdapter()
    }

    private fun initFontAdapter() {
        val fontList: MutableList<FontBean> = mutableListOf()
        val sp = AppSP(requireActivity())
        val fontID = sp.fontResId
        fontList.add(FontBean(R.font.font_medium, R.font.font_medium == fontID))
        fontList.add(FontBean(R.font.header, R.font.header == fontID))
        fontList.add(FontBean(R.font.damion, R.font.damion == fontID))
        fontList.add(FontBean(R.font.grandma, R.font.grandma == fontID))
        fontList.add(FontBean(R.font.playball, R.font.playball == fontID))
        fontList.add(FontBean(R.font.pork, R.font.pork == fontID))
        fontList.add(FontBean(R.font.silver, R.font.silver == fontID))

        val fontAdapter = FontAdapter(requireActivity(), fontList)
        fontAdapter.setOnItemClickListener(object : FontAdapter.OnItemClickListener {
            @SuppressLint("NotifyDataSetChanged")
            override fun onItemClick(position: Int) {
                val bean = fontList[position]
                for (font in fontList) {
                    font.select = false
                }
                fontList[position].select = true
                AppSP(requireActivity()).fontResId = bean.id
                fontAdapter.notifyDataSetChanged()
                Log.d("TextFragment", "触发onFontItemClick, 监听器: $textFragmentClickListener")
                textFragmentClickListener?.onFontItemClick()
            }
        })
        binding.fontRv.layoutManager = GridLayoutManager(requireActivity(), 7)
        binding.fontRv.adapter = fontAdapter
    }

    private fun initColorAdapter() {
        val sp = AppSP(requireActivity())
        val fontColorId = sp.fontColorResId
        val fontColorList: MutableList<FontColorBean> = mutableListOf()
        // 获取颜色资源数组
        val colorArray = resources.obtainTypedArray(R.array.FontColorArray)
        // 遍历颜色资源数组
        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 == fontColorId)
            fontColorList.add(fontColor)
        }
        colorArray.recycle()

        val fontColorAdapter = FontColorAdapter(requireActivity(), fontColorList)
        fontColorAdapter.setOnItemClickListener(object : FontColorAdapter.OnItemClickListener {
            @SuppressLint("NotifyDataSetChanged")
            override fun onItemClick(position: Int) {
                val bean = fontColorList[position]
                for (fontColor in fontColorList) {
                    fontColor.select = false
                }
                fontColorList[position].select = true
                AppSP(requireActivity()).fontColorResId = bean.id
                fontColorAdapter.notifyDataSetChanged()
                Log.d("TextFragment", "触发onFontColorItemClick, 监听器: $textFragmentClickListener")
                textFragmentClickListener?.onFontColorItemClick()
            }
        })
        binding.colorRv.layoutManager = GridLayoutManager(requireActivity(), 7)
        binding.colorRv.adapter = fontColorAdapter
    }

    private fun initFontSizeAdapter() {
        val sp = AppSP(requireActivity())
        val fontSize = sp.fontSize
        val fontSizeList: MutableList<FontSizeBean> = mutableListOf()
        // 获取字符串资源数组
        val fontSizeNameArray = resources.getStringArray(R.array.FontSizeNameArray)
        val fontSizeArray = resources.getIntArray(R.array.FontSizeArray)
        // 确保两个数组的长度相同
        if (fontSizeNameArray.size == fontSizeArray.size) {
            for (i in fontSizeNameArray.indices) {
                val name = fontSizeNameArray[i]
                val size = fontSizeArray[i]
                val fontSizeBean = FontSizeBean(name, size, fontSize == size)
                fontSizeList.add(fontSizeBean)
            }
        }
        val fontSizeAdapter = FontSizeAdapter(requireActivity(), fontSizeList)
        fontSizeAdapter.setOnItemClickListener(object : FontSizeAdapter.OnItemClickListener {
            @SuppressLint("NotifyDataSetChanged")
            override fun onItemClick(position: Int) {
                val bean = fontSizeList[position]
                for (size in fontSizeList) {
                    size.select = false
                }
                fontSizeList[position].select = true
                AppSP(requireActivity()).fontSize = bean.size
                fontSizeAdapter.notifyDataSetChanged()
                Log.d("TextFragment", "触发onFontSizeItemClick, 监听器: $textFragmentClickListener")
                textFragmentClickListener?.onFontSizeItemClick()
            }
        })
        binding.sizeRv.layoutManager = GridLayoutManager(requireActivity(), fontSizeList.size)
        binding.sizeRv.adapter = fontSizeAdapter
    }

    private var textFragmentClickListener: TextFragmentClickListener? = null
    fun setTextFragmentClickListener(listener: TextFragmentClickListener) {
        Log.d("TextFragment", "设置监听器: $listener")
        textFragmentClickListener = listener
    }

    interface TextFragmentClickListener {
        fun onFontItemClick()
        fun onFontColorItemClick()
        fun onFontSizeItemClick()
    }
}