package com.remax.visualnovel.ui.chat.ui

import android.content.Context
import android.graphics.Point
import android.graphics.Rect
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.DialogHoldToTalkBinding
import com.remax.visualnovel.widget.dialoglib.LBindingDialog


class HoldToTalkDialog(context: Context) :
        LBindingDialog<DialogHoldToTalkBinding>(context, DialogHoldToTalkBinding::inflate) {

    var isTouchInCancelArea = false

    fun build(): HoldToTalkDialog {
        with()
        setBottom()
        setBgColorToken(R.string.color_transparent)
        setMaskValue(0f)
        show()
        return this
    }


    fun onTouchPointChanged(point: Point) {
        with (binding) {
            isTouchInCancelArea = !getTouchValidArea().contains(point.x, point.y)
            when (isTouchInCancelArea) {
                false -> {
                    viewRelease.setBackgroundResource(R.mipmap.bg_recording_normal)
                    cancelHintRoot.isVisible = true
                    tvRelease.text = context.resources.getString(R.string.release_to_send)
                }

                true -> {
                    viewRelease.setBackgroundResource(R.mipmap.bg_recording_cancel)
                    cancelHintRoot.isVisible = false
                    tvRelease.text = context.resources.getString(R.string.release_to_cancel)
                }
            }
        }
    }


    private fun getTouchValidArea(): Rect {
        val location = IntArray(2)
        binding.viewRelease.getLocationOnScreen(location)
        return Rect(
            location[0],
            location[1],
            location[0] + binding.viewRelease.width,
            location[1] + binding.viewRelease.height
        )
    }

}
