package app.furmeet.events.system.helper

import android.app.Activity
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import app.furmeet.events.bff.R
import app.furmeet.events.system.schedule.abstractDataTypes.dataholder.PeekableEvent
import app.furmeet.events.bff.databinding.PeekEventItemBinding
import com.tooltip.Tooltip
import java.util.*

fun bindPeekAndPop(
        activity: Activity,
        viewGroup: ViewGroup,
        view: View,
        starStatus: Boolean?,
        starAction: () -> Unit,
        fullscreenAction: () -> Unit,
        peekableEvent: PeekableEvent
) {
    resourcesAliased = activity.resources
    var tooltip: Tooltip? = null
    PeekAndPop.Builder(activity)
            .peekLayout(R.layout.peek_event_item)
            .longClickViews(view)
            .blurBackground(false)
            .parentViewGroupToDisallowTouchEvents(viewGroup)
            .onGeneralActionListener(object : PeekAndPop.OnGeneralActionListener {
                override fun onPeek(longClickView: View, position: Int) {
//                    vibrate(longClickView.context, 10)
                    activity.hideKeyboard()
                }

                override fun onPop(longClickView: View, position: Int) {
                    tooltip?.dismiss()
                    tooltip = null
                }
            })
            .onHoldAndReleaseListener(object : PeekAndPop.OnHoldAndReleaseListener {
                override fun onHold(view: View, position: Int) {
                    vibrate(view.context, 10)
                    tooltip?.dismiss()
                    tooltip = when (view.id) {
                        R.id.starIt -> starStatus?.let {
                            Tooltip.Builder(view)
                                    .setText(view.context.getString(
                                            if (it)
                                                R.string.tooltip_remove_favorite
                                            else
                                                R.string.tooltip_add_favorite
                                    ))
                        }
                        R.id.goFullscreen ->
                            Tooltip.Builder(view)
                                    .setText(view.context.getString(R.string.tooltip_show_details))
                        else -> null
                    }
                            ?.setBackgroundColor(view.context.getColorCompat(R.color.colorBase03))
                            ?.setTextColor(view.context.getColorCompat(R.color.colorBase18))
                            ?.setGravity(Gravity.TOP)
                            ?.show()
                }

                override fun onLeave(view: View, position: Int) {
                    tooltip?.dismiss()
                    tooltip = null
                }

                override fun onRelease(view: View, position: Int) {
                    tooltip?.dismiss()
                    tooltip = null
                    when (view.id) {
                        R.id.starIt -> starAction()
                        R.id.goFullscreen -> fullscreenAction()
                    }
                }
            })
            .build()
//            .apply {
//                val scheduleManager = ScheduleManagerGetter[activity]
//                var obs = Observer { _: Observable, _: Any? -> }
//                obs = Observer { observable: Observable, _: Any? ->
//                    observable.deleteObserver(obs)
//                    destroy()
//                    val lht = PeekAndPop::class.java.getDeclaredField("longHoldTimer").let{
//                        it.isAccessible = true
//                        it.get(this) as Timer
//                    }
//                    lht.purge()
//                    lht.cancel()
//                }
//                scheduleManager.addObserver(obs)
//            }
            .apply {
                val binding = PeekEventItemBinding.bind(this.peekView)
                if (starStatus == null)
                    binding.starIt.visibility = View.GONE
                else
                    binding.starIt.setImageResource(
                            if (!starStatus)
                                R.drawable.ic_star_black_24dp
                            else
                                R.drawable.ic_star_border_black_24dp
                    )
                addHoldAndReleaseView(R.id.starIt)
                addHoldAndReleaseView(R.id.goFullscreen)
                binding.timeRange.text = "" +
                        peekableEvent.timeRange.start.timeInMillis.formatAsDayOfWeek()
                            .replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() } +
                        ", " +
                        peekableEvent.timeRange.start.timeInMillis.formatAsHoursAndMinutes() +
                        " - " +
                        peekableEvent.timeRange.finish.timeInMillis.formatAsHoursAndMinutes()
                binding.title.text = peekableEvent.title.solved
                if (peekableEvent.language == null) {
                    binding.language.visibility = View.GONE
                } else {
                    ("" + view.context.getString(R.string.preposition_in) + " " + view.context.getString(peekableEvent.language.language).lowercase())
                            .let {
                                binding.language.text = it
                            }
                }
                if (peekableEvent.subtitles == null || peekableEvent.subtitles.solved.isNullOrBlank()) {
                    binding.speaker.visibility = View.GONE
                } else {
                    binding.speaker.text = peekableEvent.subtitles.solved
                }
                if (peekableEvent.places == null || peekableEvent.places.isEmpty()) {
                    binding.place.visibility = View.GONE
                } else {
                    peekableEvent.places.map {
                        it.names.solved!!
                    }.let {
                        binding.place.text = it.textuallyJoined
                    }
                }
                if (peekableEvent.descriptions == null || peekableEvent.descriptions.solved.isNullOrBlank()) {
                    binding.description.visibility = View.GONE
                } else {
                    peekableEvent.descriptions.solved.let {
                        binding.description.text = it
                    }
                }
            }
    // */
}