package com.base.appzxhy.ui.views

import android.app.AlertDialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import com.base.appzxhy.R
import com.base.appzxhy.databinding.DialogPlayQualityBinding
import com.ss.ttvideoengine.Resolution

object ShortPlayQualityDialog {

    fun Context.showShortPlayQualityDialog(
        currentResolution: Resolution,
        resolutions: Array<Resolution>,
        resolutionAction: (resolution: Resolution, resolutionString: String) -> Unit
    ) {
        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogPlayQualityBinding.inflate(LayoutInflater.from(this))
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(false)

        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)
        resolutions[0]

        if (resolutions.contains(Resolution.High)) {
            binding.tvResolution1.text = "480P"
        } else {
            binding.tvResolution1.visibility = View.GONE
        }

        if (resolutions.contains(Resolution.SuperHigh)) {
            binding.tvResolution2.text = "720p"
        } else {
            binding.tvResolution2.visibility = View.GONE
        }

        if (resolutions.contains(Resolution.ExtremelyHigh)) {
            binding.tvResolution3.text = "1080P"
        } else {
            binding.tvResolution3.visibility = View.GONE
        }

        when (currentResolution) {
            Resolution.Auto -> {
                binding.tvAuto.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            Resolution.High -> {
                binding.tvResolution1.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            Resolution.SuperHigh -> {
                binding.tvResolution2.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            Resolution.ExtremelyHigh -> {
                binding.tvResolution3.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_33ffffff_4dff3d71_5)
            }

            else -> {}
        }
        binding.flClose.setOnClickListener { dialog.dismiss() }

        binding.tvAuto.setOnClickListener {
            dialog.dismiss()
            resolutionAction.invoke(Resolution.Auto, "Auto")
        }
        binding.tvResolution1.setOnClickListener {
            dialog.dismiss()
            resolutionAction.invoke(Resolution.High, "480P")
        }
        binding.tvResolution2.setOnClickListener {
            dialog.dismiss()
            resolutionAction.invoke(Resolution.SuperHigh, "720P")
        }
        binding.tvResolution3.setOnClickListener {
            dialog.dismiss()
            resolutionAction.invoke(Resolution.ExtremelyHigh, "1080P")
        }
    }
}