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.RelationEntity
import com.sybotan.android.demo.R
import com.sybotan.android.demo.bean.RelConfigEntityPackClass
import com.sybotan.android.demo.bean.RelationPackClass
import java.lang.StringBuilder


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

    override fun getChild(groupPosition: Int, childPosition: Int): RelationPackClass {
        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: RelConfigEntityPackClass = mGroups[groupPosition]
        val tvGroup = holder?.findViewById(R.id.title) as TextView
        tvGroup.text =
            group.entity.graphName + "/" + group.entity.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.entity.objFromLocalId)
        if (item.targetPipeType.direction.equals("none")) {
            sb.append("  —-  ")
        } else {
            sb.append("  —>  ")
        }
        sb.append(item.entity.objToLocalId)
        itemTv.text = sb.toString()
        holder.findViewById<ImageView>(R.id.delete).setOnClickListener {
            deleteRel?.ondelete(item.entity)
        }
        return holder
    }

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

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

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

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