package com.base.browserwhite.ui.views

import android.app.AlertDialog
import android.content.Context
import android.content.Context.WINDOW_SERVICE
import android.util.DisplayMetrics
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import com.base.browserwhite.R
import com.base.browserwhite.databinding.DialogBookmarkMoreBinding
import com.base.browserwhite.utils.LogEx


object BookmarkDialog {

    private val TAG = "BookmarkDialog"

    fun Context.showBookmarkMoreDialog(
        anchorView: View,
        uiType: Int = -1,//-1所有展示
        openNewTab: (() -> Unit)? = null,
        deleteAction: (() -> Unit)? = null,
        editAction: (() -> Unit)? = null,
        addNavAction: (() -> Unit)? = null,
        selectAction: (() -> Unit)? = null,
        addDeskAction: (() -> Unit)? = null,
        addBookmarkAction: (() -> Unit)? = null,
    ) {
        val binding = DialogBookmarkMoreBinding.inflate(LayoutInflater.from(this))
        val dialog = AlertDialog.Builder(this).create()
        dialog.setView(binding.root)

        dialog.show()

        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        var itemCount = 7
        when (uiType) {
            -1 -> {
                binding.llAddBookmark.visibility = View.GONE
                binding.vAddBookmark.visibility = View.GONE

                itemCount = 6
            }

            0 -> {
                binding.llOpenNewTab.visibility = View.GONE
                binding.vOpenNewTab.visibility = View.GONE

                binding.llAddBookmark.visibility = View.GONE
                binding.vAddBookmark.visibility = View.GONE

                binding.llAddNavigation.visibility = View.GONE
                binding.vAddNavigation.visibility = View.GONE

                binding.llAddDesktop.visibility = View.GONE
                binding.vAddDesktop.visibility = View.GONE

                itemCount = 3
            }

            1 -> {
                binding.llOpenNewTab.visibility = View.GONE
                binding.vOpenNewTab.visibility = View.GONE

                binding.llEdit.visibility = View.GONE
                binding.vEdit.visibility = View.GONE

                binding.llSelect.visibility = View.GONE
                binding.vAddDesktop.visibility = View.GONE
                itemCount = 4
            }

        }


        val params = dialog.window?.attributes
        params?.dimAmount = 0f
        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_280)
//        params?.width = FrameLayout.LayoutParams.WRAP_CONTENT
        params?.height = FrameLayout.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.TOP
        val location = IntArray(2)

        val isTopHalf = viewIsTopHalf(anchorView)
        LogEx.logDebug(TAG, "isTopHalf=$isTopHalf")

        anchorView.getLocationOnScreen(location)
        LogEx.logDebug(TAG, "anchorView x=${location[0]}")
        LogEx.logDebug(TAG, "anchorView y=${location[1]}")
//        LogEx.logDebug(TAG, "height=${binding.root.height}")
//        LogEx.logDebug(TAG, "width=${binding.root.width}")
        LogEx.logDebug(TAG, "itemCount=$itemCount")
        val x = location[0] - resources.getDimensionPixelOffset(R.dimen.dp_290)
        val y = if (isTopHalf) {
            location[1] + resources.getDimensionPixelOffset(R.dimen.dp_2)
        } else {
            var margin = resources.getDimensionPixelOffset(R.dimen.dp_35)
            if (itemCount == 3) {
                margin = resources.getDimensionPixelOffset(R.dimen.dp_50)
            }
            val offSetY =
                itemCount * resources.getDimensionPixelOffset(R.dimen.dp_65) + margin
            LogEx.logDebug(TAG, "offSetY=$offSetY")
            location[1] - offSetY
        }
        LogEx.logDebug(TAG, "x=$x")
        //598 576
        LogEx.logDebug(TAG, "y=$y")
        params?.x = x
        params?.y = y
        dialog.window?.attributes = params

        binding.llOpenNewTab.setOnClickListener {
            dialog.dismiss()
            openNewTab?.invoke()
        }
        binding.llDelete.setOnClickListener {
            dialog.dismiss()
            deleteAction?.invoke()
        }
        binding.llEdit.setOnClickListener {
            dialog.dismiss()
            editAction?.invoke()
        }
        binding.llAddBookmark.setOnClickListener {
            dialog.dismiss()
            addBookmarkAction?.invoke()
        }

        binding.llAddNavigation.setOnClickListener {
            dialog.dismiss()
            addNavAction?.invoke()
        }
        binding.llAddDesktop.setOnClickListener {
            dialog.dismiss()
            addDeskAction?.invoke()
        }
        binding.llSelect.setOnClickListener {
            dialog.dismiss()
            selectAction?.invoke()
        }
    }


    fun Context.viewIsTopHalf(view: View): Boolean {
        val location = IntArray(2)
        view.getLocationOnScreen(location)

        val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
        val dm = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(dm)
        val screenHeight = dm.heightPixels

        // 判断视图的顶部是否在屏幕的上半部分
        val isTopHalf = location[1] < screenHeight / 2

        // 判断视图的底部是否在屏幕的上半部分
        val isBottomHalf = location[1] + view.height < screenHeight / 2

        return if (isTopHalf) {
            // 视图的顶部在屏幕的上半部分
            true
        } else if (isBottomHalf) {
            // 视图的底部在屏幕的上半部分
            true
        } else {
            // 视图在屏幕的下半部分
            false
        }

    }
}