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.PipeEntity
import com.sybotan.android.demo.R
import com.sybotan.android.demo.bean.PipeConfigEntityPackClass
import java.lang.StringBuilder


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

    override fun getChild(groupPosition: Int, childPosition: Int): PipeEntity {
        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: PipeConfigEntityPackClass = mGroups[groupPosition]
        val tvGroup = holder?.findViewById(R.id.title) as TextView
        //1:正向from->to;-1:反向to->from;0:无方向
//        var sb = StringBuilder()
//        sb.append(group.fromName)
//        if (group.entity.pipeDirection == 1) {
//            sb.append("  —>  ")
//        } else if (group.entity.pipeDirection == -1) {
//            sb.append("  <—  ")
//        } else if (group.entity.pipeDirection == 0) {
//            sb.append("  ——  ")
//        }
//        sb.append(group.toName)
        tvGroup.text = group.pipeTypeName
        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)
        //1:正向from->to;-1:反向to->from;0:无方向
        if (item.direction == 1) {
            sb.append("  —>  ")
        } else if (item.direction == -1) {
            sb.append("  <—  ")
        } else if (item.direction == 0) {
            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: PipeConfigEntityPackClass)
    }

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

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

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