package ru.annin.phitoled.presentation.ui.adapter

import android.bluetooth.BluetoothDevice
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.ViewGroup
import ru.annin.phitoled.R
import ru.annin.phitoled.presentation.ui.decoration.AdapterSection
import ru.annin.phitoled.presentation.ui.decoration.SectionedAdapter
import ru.annin.phitoled.presentation.ui.viewholder.ItemDeviceViewHolder
import ru.annin.phitoled.presentation.ui.viewholder.ItemSectionDeviceViewHolder

/**
 * Adapter списка устройств.
 *
 * @author Pavel Annin.
 */
class DeviceAdapter(var items: MutableList<BluetoothDevice> = mutableListOf<BluetoothDevice>(),
                    var onItemClick: ((BluetoothDevice) -> Unit)? = null) : RecyclerView.Adapter<RecyclerView.ViewHolder>(), SectionedAdapter {

    companion object {
        private const val TYPE_SECTION = 0
        private const val TYPE_ITEM = 1
    }

    override var sections: SparseArray<AdapterSection> = SparseArray()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = when (viewType) {
        TYPE_SECTION -> {
            val vRoot = LayoutInflater.from(parent.context).inflate(R.layout.item_section_device, parent, false)
            ItemSectionDeviceViewHolder(vRoot)
        }
        TYPE_ITEM -> {
            val vRoot = LayoutInflater.from(parent.context).inflate(R.layout.item_device, parent, false)
            ItemDeviceViewHolder(vRoot)
        }
        else -> null
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) = when (holder) {
        is ItemSectionDeviceViewHolder -> {
            holder.bindToData(sections[position])
        }
        is ItemDeviceViewHolder -> {
            val item = items[sectionedPositionToPosition(position)]
            holder.run {
                onClick = { onItemClick?.invoke(item) }
                bindToData(item)
            }
        }
        else -> TODO()
    }

    override fun getItemCount() = items.size + sections.size()

    override fun getItemViewType(position: Int)
            = if (isSectionHeaderPosition(position)) TYPE_SECTION else TYPE_ITEM

    fun add(item: BluetoothDevice, position: Int = RecyclerView.NO_POSITION) {
        val index = if (position == RecyclerView.NO_POSITION) items.size else position
        items.add(index, item)
        notifyItemInserted(positionToSectionedPosition(index))
    }

    fun addAll(items: List<BluetoothDevice>) {
        val size = itemCount
        this.items.addAll(items)
        notifyItemRangeInserted(positionToSectionedPosition(size), items.size)
    }

    fun remove(position: Int) {
        if (position < itemCount) {
            items.removeAt(position)
            notifyItemRemoved(positionToSectionedPosition(position))
        }
    }

    fun clear() {
        val size = items.size
        sections.clear()
        items.clear()
        notifyItemRangeRemoved(0, size)
    }

    fun swap(items: List<BluetoothDevice>) {
        this.items.clear()
        this.items.addAll(items)
        notifyDataSetChanged()
    }
}