package com.xyzshell.myphoneinfo.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import com.xyzshell.myphoneinfo.R
import com.xyzshell.myphoneinfo.databinding.PopupLayoutBinding
import androidx.core.graphics.drawable.toDrawable

class CustomPopView(context: Context, type: Int, private val callback: (String) -> Unit) {
    private val context: Context = context
    private val type: Int = type
    private lateinit var popupWindow: PopupWindow
    private lateinit var binding: PopupLayoutBinding

    init {
        initPopupWindow()
    }

    @SuppressLint("ServiceCast")
    private fun initPopupWindow() {
        // 初始化布局
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        binding = PopupLayoutBinding.inflate(inflater)

        // 设置PopupWindow
        popupWindow = PopupWindow(
            binding.root,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            true
        )

        // 设置背景和动画
        popupWindow.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
        popupWindow.animationStyle = R.style.PopupAnimation

        setType()
        binding.llMore1.setOnClickListener {
            if (binding.textMore1.text.toString() == context.getString(R.string.metric_units)) {
                binding.textMore1.text = context.getString(R.string.imper_units)
            } else {
                binding.textMore1.text = context.getString(R.string.metric_units)
            }
            dismiss()
        }
        binding.popSleep.setOnClickListener {
            dismiss()
            callback.invoke(binding.textSleep.text.toString())
            if (binding.textSleep.text.toString() == context.getString(R.string.hide_deep_sleep)) {
                binding.textSleep.text = context.getString(R.string.show_deep_sleep)
            } else {
                binding.textSleep.text = context.getString(R.string.hide_deep_sleep)
            }
        }
        binding.textMore2.setOnClickListener {}
        binding.textMore3.setOnClickListener {}
        binding.textMore4.setOnClickListener {}
        binding.textMore5.setOnClickListener {}
        binding.textMore6.setOnClickListener {}
        binding.text1.setOnClickListener {
            dismiss()
            callback.invoke(binding.text1.text.toString())
        }
        binding.text2.setOnClickListener {
            dismiss()
            callback.invoke(binding.text2.text.toString())
        }
    }

    private fun setType() {//0：簇1或簇2    1：隐藏深度休眠  2：主页更多 3:公制单位和重置 4：公制单位
        when (type) {
            0 -> {
                binding.popCluster.visibility = View.VISIBLE
                binding.popSleep.visibility = View.GONE
                binding.popMore.visibility = View.GONE
            }

            1 -> {
                binding.popCluster.visibility = View.GONE
                binding.popSleep.visibility = View.VISIBLE
                binding.popMore.visibility = View.GONE

            }

            2 -> {
                binding.popCluster.visibility = View.GONE
                binding.popSleep.visibility = View.GONE
                binding.popMore.visibility = View.VISIBLE
            }

            3 -> {
                binding.popCluster.visibility = View.GONE
                binding.popSleep.visibility = View.GONE
                binding.popMore.visibility = View.VISIBLE
                binding.llHide.visibility = View.GONE
            }

            4 -> {
                binding.popCluster.visibility = View.GONE
                binding.popSleep.visibility = View.GONE
                binding.popMore.visibility = View.VISIBLE
                binding.llMore2.visibility = View.GONE
                binding.llHide.visibility = View.GONE
            }
        }
    }

    // 显示PopView
    fun show(anchorView: View) {
        if (::popupWindow.isInitialized && !popupWindow.isShowing) {
            popupWindow.showAsDropDown(anchorView)
        }
    }

    // 在指定位置显示
    fun showAtLocation(parent: View, gravity: Int, x: Int, y: Int) {
        if (::popupWindow.isInitialized && !popupWindow.isShowing) {
            popupWindow.showAtLocation(parent, gravity, x, y)
        }
    }

    // 隐藏PopView
    fun dismiss() {
        if (::popupWindow.isInitialized && popupWindow.isShowing) {
            popupWindow.dismiss()
        }
    }

    // 设置PopView内容
    fun setContent(text: String) {
    }

    // 判断是否正在显示
    fun isShowing(): Boolean {
        return ::popupWindow.isInitialized && popupWindow.isShowing
    }
}