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.RelConfigEntity
import com.framework.mvvm.model.db.entity.RelationEntity
import com.sybotan.android.demo.R
import java.lang.StringBuilder


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

    override fun getChild(groupPosition: Int, childPosition: Int): RelationEntity {
        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: RelConfigEntity = mGroups[groupPosition]
        val tvGroup = holder?.findViewById(R.id.title) as TextView
        tvGroup.text = group.graphName + "/" + group.relName
        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_relation, parent, false)
        }
        val itemTv = holder?.findViewById(R.id.title) as TextView
        val item = mItems[groupPosition][childPosition]
        var sb = StringBuilder()
        sb.append(item.objFromLocalId)
        sb.append("  —>  ")
        sb.append(item.objToLocalId)
        itemTv.text = sb.toString()
        holder?.findViewById<ImageView>(R.id.delete).setOnClickListener {
            deleteRel?.ondelete(item)
        }
        return holder
    }

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

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

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

    fun setOnDelete(deleteRel: DeleteRel) {
        this.deleteRel = deleteRel
    }
}