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.ImageView
import android.widget.TextView
import com.framework.mvvm.model.db.entity.task.ObjectEntity
import com.framework.mvvm.model.db.entity.task.RelationEntity
import com.sybotan.android.demo.R
import com.sybotan.android.demo.bean.ContainerParentBean
import com.sybotan.android.demo.bean.RelConfigEntityPackClass
import com.sybotan.android.demo.bean.RelationPackClass
import java.lang.StringBuilder


class ContainerExpandAdapter(
    context: Context,
    groups: List<ContainerParentBean>,
    items: MutableList<List<ObjectEntity>>
) : BaseExpandableListAdapter() {
    private var addRel: AddRel? = null
    private var deleteRel: DeleteRel? = null
    private var itemClick: ItemClick? = null
    private var mContext: Context = context
    private var mGroups: List<ContainerParentBean> = groups
    private var mItems: List<List<ObjectEntity>> = 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): ContainerParentBean {
        return mGroups[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): ObjectEntity {
        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_relation, parent, false)
        }
        val group: ContainerParentBean = mGroups[groupPosition]
        val tvGroup = holder?.findViewById(R.id.title) as TextView
        tvGroup.text = group.widgetName
        val add = holder?.findViewById(R.id.add) as ImageView
        add.setOnClickListener {
            addRel?.onAdd(group)
        }
        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_container, parent, false)
        }
        val itemTv = holder?.findViewById(R.id.title) as TextView
        val item = mItems[groupPosition][childPosition]
        itemTv.text = item.localId
        holder.findViewById<TextView>(R.id.delete).setOnClickListener {
            deleteRel?.ondelete(item)
        }
        holder.findViewById<TextView>(R.id.title).setOnClickListener {
            itemClick?.onclick(item)
        }
        return holder
    }

    public interface AddRel {
        fun onAdd(group: ContainerParentBean)
    }

    fun setOnAdd(addRel: AddRel) {
        this.addRel = addRel
    }

    public interface DeleteRel {
        fun ondelete(item: ObjectEntity)
    }

    fun setOnDelete(deleteRel: DeleteRel) {
        this.deleteRel = deleteRel
    }
    public interface ItemClick {
        fun onclick(item: ObjectEntity)
    }

    fun setOnItemClick(itemClick: ItemClick) {
        this.itemClick = itemClick
    }
}