package com.atmob.keyboard_android.component

import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.atmob.keyboard_android.R
import com.atmob.keyboard_android.component.adapter.AiKeyboardPagerAdapter
import com.atmob.keyboard_android.component.base.BaseUIComponent
import com.atmob.keyboard_android.ext.setGone
import com.atmob.keyboard_android.widget.NoSwipeViewPager
import com.atmob.keyboard_android.widget.indicator.TabPagerTitleView
import com.blankj.utilcode.util.ConvertUtils
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator

/**
 * AI键盘组件
 */
class AiKeyboardComponent @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : BaseUIComponent(context, attrs, defStyleAttr) {
    private lateinit var vTabBarLayout: View
    private lateinit var vMagicIndicator: MagicIndicator
    private lateinit var vViewPager: NoSwipeViewPager

    /**
     * Tab列表
     */
    private lateinit var mTabList: List<String>

    override fun onInflateViewId(): Int {
        return R.layout.component_ai_keyboard
    }

    override fun findView(view: View) {
        vTabBarLayout = view.findViewById(R.id.tar_bar_layout)
        vMagicIndicator = view.findViewById(R.id.magic_indicator)
        vViewPager = view.findViewById(R.id.view_pager)
    }

    override fun bindView(view: View) {
        setupTabBar()
        setupViewPager()
    }

    /**
     * 配置Tab栏
     */
    private fun setupTabBar() {
        // TODO: 隐藏Tab栏，用于测试
        vTabBarLayout.setGone()

        mTabList = listOf(
            "发起聊天",
            "邀约话术",
            "如何告白",
            "关系升温"
        )

        // 配置指示器
        vMagicIndicator.setNavigator(CommonNavigator(context).apply {
            scrollPivotX = 0.35f
            setAdapter(object : CommonNavigatorAdapter() {
                override fun getCount(): Int {
                    return mTabList.size
                }

                override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                    val titleView = object : TabPagerTitleView(context) {
                        override fun onSelected(index: Int, totalCount: Int) {
                            super.onSelected(index, totalCount)
                            // 选中Tab，设置为粗体
                            setBoldStyle(true)
                        }

                        override fun onDeselected(index: Int, totalCount: Int) {
                            super.onDeselected(index, totalCount)
                            // 不选中Tab，设置为普通体
                            setBoldStyle(false)
                        }
                    }
                    // Tab文字
                    titleView.setText(mTabList[index])
                    // Tab未选中时的字体颜色
                    titleView.setTextColor(context.resources.getColor(R.color.text_color_secondary))
                    // Tab选中时的字体颜色
                    titleView.setClipColor(context.resources.getColor(R.color.text_color_primary))
                    // 字体大小
                    titleView.textSize = ConvertUtils.dp2px(12f).toFloat()
                    // 设置padding，让文字距离背景有一定间距
                    val paddingHorizontal = ConvertUtils.dp2px(18f)
                    titleView.setPadding(paddingHorizontal, 0, paddingHorizontal, 0)
                    // 点击Tab，切换页面
                    titleView.setOnClickListener(object : OnClickListener {
                        override fun onClick(v: View?) {
                            vViewPager.setCurrentItem(index, false)
                        }
                    })
                    return titleView
                }

                override fun getIndicator(context: Context): IPagerIndicator {
                    val indicator = LinePagerIndicator(context)
                    val navigatorHeight =
                        context.resources.getDimension(R.dimen.common_navigator_height)
                    val borderWidth = UIUtil.dip2px(context, 1.0).toFloat()
                    val lineHeight = navigatorHeight - 2 * borderWidth
                    indicator.lineHeight = lineHeight
                    indicator.roundRadius = lineHeight / 2
                    indicator.yOffset = borderWidth
                    // 指示器的背景颜色
                    indicator.setColors(context.resources.getColor(R.color.bg_tab_indicator_selected))
                    return indicator
                }
            })
        })
    }

    /**
     * 配置ViewPager
     */
    private fun setupViewPager() {
        // 配置ViewPager
        vViewPager.setSwipeEnabled(false)
        vViewPager.adapter = AiKeyboardPagerAdapter(mTabList)
        // 绑定ViewPager和MagicIndicator
        ViewPagerHelper.bind(vMagicIndicator, vViewPager)
    }
}