package app.furmeet.events.brasilfurfest.schedule.adapters

import android.content.Context
import android.view.ViewGroup
import com.adlerosn.brasilfurfest.R
import app.furmeet.events.brasilfurfest.helper.inflate
import app.furmeet.events.brasilfurfest.helper.layoutInflater
import app.furmeet.events.brasilfurfest.helper.solved
import app.furmeet.events.brasilfurfest.helper.toHtml
import app.furmeet.events.brasilfurfest.schedule.abstractDataTypes.convention.Convention
import app.furmeet.events.brasilfurfest.schedule.viewHolders.RecyclerViewHolder
import kotlinx.android.synthetic.main.fragment_rules_item.view.*
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

class ConventionRulesAdapter(convention: Convention) : RecyclerViewAdapter() {
    val data = listOf(
            fun(context: Context) = Pair(context.getString(R.string.convention_rules), convention.rules.solved!!.toHtml())
    ) + convention.additionalRules.map {
        fun(_: Context) = Pair(it.names.solved!!, it.rules.solved!!.toHtml())
    }

    override fun getItemCount(): Int = data.size

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder =
            RecyclerViewHolder(parent.context.layoutInflater.inflate(R.layout.fragment_rules_item).apply {
                val postAnimation = Runnable {
                    expandable.postOnAnimation {
                        doAsync {
                            Thread.sleep(expandable.duration.toLong() + 100)
                            uiThread {
                                expandIcon.setImageResource(
                                        if (expandable.isCollapsed) {
                                            R.drawable.ic_expand_more_black_24dp
                                        } else {
                                            R.drawable.ic_expand_less_black_24dp
                                        }
                                )
                                fulltext.setTextIsSelectable(!expandable.isCollapsed)
                            }
                        }
                    }
                }
                val toggleRunner = Runnable {
                    expandIcon.setImageResource(R.drawable.ic_more_horiz_black_24dp)
                    expandable.toggle(true)
                    postAnimation.run()
                }
                expandBar.setOnClickListener { toggleRunner.run() }
                expandable.setOnClickListener {
                    if (expandable.isCollapsed) {
                        toggleRunner.run()
                    }
                }
                collapser.setOnClickListener {
                    if (!expandable.isCollapsed) {
                        toggleRunner.run()
                    }
                }
            })

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        holder.itemView.apply {
            val item = data[position](context)
            ruleTitle.text = item.first
            fulltext.text = item.second
        }
    }
}