package com.base.locationsharewhite.ui.views

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.base.locationsharewhite.R
import com.base.locationsharewhite.ads.AdsMgr
import com.base.locationsharewhite.databinding.DialogAppExitBinding
import com.base.locationsharewhite.databinding.DialogMapTypeBinding
import com.base.locationsharewhite.databinding.DialogViewerMoreBinding
import com.base.locationsharewhite.utils.LogEx
import com.google.android.gms.maps.GoogleMap

object DialogView {

    fun Context.showMapTypeDialog(googleMap: GoogleMap) {
        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogMapTypeBinding.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_375)
        params?.height = LinearLayout.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.BOTTOM
        params?.y = 20
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        LogEx.logDebug("showMapTypeDialog", "mapType=${googleMap.mapType}")
        if (googleMap.mapType == GoogleMap.MAP_TYPE_SATELLITE) {
            binding.ivStatellite.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_03b988_15)
            binding.tvStatellite.setTextColor(Color.parseColor("#03B988"))
        } else {
            binding.ivStreet.background = ContextCompat.getDrawable(this, R.drawable.bg_stroke_03b988_15)
            binding.tvStreet.setTextColor(Color.parseColor("#03B988"))
        }

        binding.llStatellite.setOnClickListener {
            dialog.dismiss()
            googleMap.mapType = GoogleMap.MAP_TYPE_SATELLITE
        }

        binding.llStreet.setOnClickListener {
            dialog.dismiss()
            googleMap.mapType = GoogleMap.MAP_TYPE_NORMAL
        }
    }

    fun Activity.showAppExitDialog() {
        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogAppExitBinding.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_356)
        params?.height = LinearLayout.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.BOTTOM
        params?.y = 50
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvQuit.setOnClickListener {
            dialog.dismiss()
            finish()
        }
        binding.ivClose.setOnClickListener {
            dialog.dismiss()
        }
        AdsMgr.showNative(binding.flAd, R.layout.layout_admob_app_exit)
    }


    fun Context.showViewerMoreDialog(
        anchorView: View,
        renameAction: (() -> Unit)? = null,
        deleteAction: (() -> Unit)? = null,
    ) {
        val binding = DialogViewerMoreBinding.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_120)
        params?.height = FrameLayout.LayoutParams.WRAP_CONTENT
        params?.gravity = Gravity.TOP

        val height = resources.getDimensionPixelOffset(R.dimen.dp_90)
        val isOutScreen = viewIsOutScreen(anchorView, height)
        val location = IntArray(2)
        anchorView.getLocationOnScreen(location)

        val marginEnd = resources.getDimensionPixelOffset(R.dimen.dp_235)
        LogEx.logDebug("showViewerMoreDialog", "marginEnd=$marginEnd ${location[0]}")
        val x = location[0] - marginEnd

        val y = if (isOutScreen) {
            binding.root.background = ContextCompat.getDrawable(this, R.drawable.bg_b3000000_tlr10_bl10)
            location[1] - height - resources.getDimensionPixelOffset(R.dimen.dp_48)
        } else {
            binding.root.background = ContextCompat.getDrawable(this, R.drawable.bg_b3000000_tl10_blr10)
            location[1] + resources.getDimensionPixelOffset(R.dimen.dp_2)
        }
        params?.x = x
        params?.y = y
        dialog.window?.attributes = params

        binding.tvRename.setOnClickListener {
            dialog.dismiss()
            renameAction?.invoke()
        }
        binding.tvDelete.setOnClickListener {
            dialog.dismiss()
            deleteAction?.invoke()
        }
    }

    fun Context.viewIsOutScreen(view: View, height: Int): Boolean {
        val screenHeight = resources.displayMetrics.heightPixels
        val viewBottomPosition: Int = view.bottom

        if (viewBottomPosition + height > screenHeight) {
            // 超出了屏幕
            return true
        } else {
            // 没有超出屏幕
            return false
        }

    }
}