package com.sybotan.android.demo.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.TextView
import com.sybotan.android.demo.R
import com.sybotan.android.demo.entitiies.Build
import com.sybotan.android.demo.entitiies.Floor


class ExpandAdapter(
    context: Context,
    groups: List<Build>,
    items: List<List<Floor>>
) : BaseExpandableListAdapter() {
    private var mContext: Context = context
    private var mGroups: List<Build> = groups
    private var mItems: List<List<Floor>> = items
    private var mLayoutInflater: LayoutInflater = LayoutInflater.from(context)

    override fun getGroupCount(): Int {
        return mGroups.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return mItems[groupPosition].size
    }

    override fun getGroup(groupPosition: Int): Build {
        return mGroups[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Floor {
        return mItems[groupPosition][childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return true
    }

    override fun hasStableIds(): Boolean {
        return false
    }

    override fun getGroupView(groupPosition: Int, isExpanded: Boolean, convertView: View?, parent: ViewGroup?): View {
        var holder = convertView
        if (holder == null) {
            holder = mLayoutInflater.inflate(R.layout.group_expand_space, parent, false)
        }
        val group: Build = mGroups[groupPosition]
        val tvGroup = holder?.findViewById(R.id.groupSpaceTV) as TextView
        tvGroup.text = group.localName
        return holder
    }

    override fun getChildView(groupPosition: Int, childPosition: Int, isLastChild: Boolean, convertView: View?, parent: ViewGroup?): View {
        var holder = convertView
        if (holder == null) {
            holder = mLayoutInflater.inflate(R.layout.child_expand_space, parent, false)
        }
        val itemTv = holder?.findViewById(R.id.itemSpaceTV) as TextView
        val item = mItems[groupPosition][childPosition]
        itemTv.text = item.localName
        return holder
    }
}