package com.base.browserwhite.ui.views

import android.app.AlertDialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import com.base.browserwhite.R
import com.base.browserwhite.databinding.DialogBookmarkMoreBinding
import com.base.browserwhite.ui.activity.splash.Splash2Activity
import com.base.browserwhite.ui.activity.webbrowser.WebBrowserActivity
import com.base.browserwhite.utils.ShortcutUtils

object 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,
    ) {
        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)

        val params = dialog.window?.attributes
        params?.dimAmount = 0f
        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_250)
//        params?.width = FrameLayout.LayoutParams.WRAP_CONTENT
        params?.height = FrameLayout.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.TOP
        val location = IntArray(2)
        anchorView.getLocationOnScreen(location)
        params?.x = resources.getDimensionPixelOffset(R.dimen.dp_100)
        params?.y = location[1] - resources.getDimensionPixelOffset(R.dimen.dp_150)
        dialog.window?.attributes = params

        when (uiType) {
            -1 -> {
                binding.llAddBookmark.visibility = View.GONE
            }

            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
            }

            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.llOpenNewTab.setOnClickListener {
            dialog.dismiss()
            openNewTab?.invoke()
        }
        binding.llDelete.setOnClickListener {
            dialog.dismiss()
            deleteAction?.invoke()
        }
        binding.llEdit.setOnClickListener {
            dialog.dismiss()
            editAction?.invoke()
        }
        binding.llAddNavigation.setOnClickListener {
            dialog.dismiss()
            addNavAction?.invoke()
        }
        binding.llAddDesktop.setOnClickListener {
            dialog.dismiss()
            addDeskAction?.invoke()
        }
        binding.llSelect.setOnClickListener {
            dialog.dismiss()
            selectAction?.invoke()
        }
    }
}