package com.atmob.keyboard_android.component.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewpager.widget.PagerAdapter
import com.atmob.keyboard_android.R

/**
 * AI键盘Tab的adapter
 */
class AiKeyboardPagerAdapter(private val mDataList: List<String>) : PagerAdapter() {
    override fun getCount(): Int {
        return mDataList.size
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        // 创建每一页的键盘
        val context = container.context
        val contentView = LayoutInflater.from(context)
            .inflate(R.layout.pager_ai_keyboard_content, container, false)
        container.addView(contentView)
        return contentView
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View?)
    }

    override fun getItemPosition(`object`: Any): Int {
        val textView = `object` as TextView
        val text = textView.getText().toString()
        val index = mDataList.indexOf(text)
        if (index >= 0) {
            return index
        }
        return POSITION_NONE
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return mDataList[position]
    }
}
