package com.kdanmobile.reader.screen.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView
import com.kdanmobile.kmpdfkit.pdfcommon.OutlineItem
import com.kdanmobile.reader.R

class OutlineAdapter(private val outlineListItems: List<OutlineItem>): BaseAdapter() {

    override fun getCount(): Int {
        return outlineListItems.size
    }

    override fun getItem(position: Int): OutlineItem? {
        return if (position >= 0 && position < outlineListItems.size) outlineListItems[position] else null
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, view: View?, parent: ViewGroup): View {
        var view1 = view
        val holderView: HolderView
        if (view1 == null) {
            view1 = LayoutInflater.from(parent.context).inflate(R.layout.outline_entry, null) as View
            holderView = HolderView(view1)
            view1.tag = holderView
        } else {
            holderView = view1.tag as HolderView
        }

        val item = getItem(position)
        /****** 大纲层次缩进  */
        var space = ""
        val null_str = parent.context.getString(R.string.null_str)
        for (i in 1 .. item!!.level) {
            space += null_str + null_str
        }

        holderView.tv_name.text = space + item.title
        holderView.tv_name.textSize = (15 - item.level * 1.2f)
        holderView.tv_page.text = (item.page + 1).toString()
        return view1
    }

    private class HolderView(view: View) {
        var tv_name: TextView
        var tv_page: TextView

        init {
            tv_name = view.findViewById<View>(R.id.tv_outlineEntry_title) as TextView
            tv_page = view.findViewById<View>(R.id.tv_outlineEntry_page) as TextView
        }
    }
}
