package com.base.appzxhy.ui.views

import android.app.AlertDialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import androidx.core.content.ContextCompat
import com.base.appzxhy.R
import com.base.appzxhy.databinding.DialogPlaySpeedBinding

object ShortPlaySpeedDialog {

    fun Context.showShortPlaySpeedDialog(speed: Float, selectSpeedAction: (speed: Float) -> Unit) {
        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogPlaySpeedBinding.inflate(LayoutInflater.from(this))
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

        val params = dialog.window?.attributes
//        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_320)
//        params?.height = resources.getDimensionPixelOffset(R.dimen.dp_400)
        params?.gravity = Gravity.BOTTOM
        params?.y = 50
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        when (speed) {
            0.5f -> {
                binding.tv05.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            0.75f -> {
                binding.tv075.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            1.0f -> {
                binding.tv1.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            1.25f -> {
                binding.tv125.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            1.5f -> {
                binding.tv15.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            2.0f -> {
                binding.tv2.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }
        }
        binding.tv05.setOnClickListener {
            dialog.dismiss()
            selectSpeedAction.invoke(0.5f)
        }
        binding.tv075.setOnClickListener {
            dialog.dismiss()
            selectSpeedAction.invoke(0.75f)
        }
        binding.tv1.setOnClickListener {
            dialog.dismiss()
            selectSpeedAction.invoke(1.0f)
        }
        binding.tv125.setOnClickListener {
            dialog.dismiss()
            selectSpeedAction.invoke(1.25f)
        }
        binding.tv15.setOnClickListener {
            dialog.dismiss()
            selectSpeedAction.invoke(1.5f)
        }
        binding.tv2.setOnClickListener {
            dialog.dismiss()
            selectSpeedAction.invoke(2.0f)
        }
        binding.flClose.setOnClickListener { dialog.dismiss() }


    }
}