package com.base.browserwhite.ui.views

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
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.bean.ConstObject.BAIDU
import com.base.browserwhite.bean.ConstObject.BING
import com.base.browserwhite.bean.ConstObject.DUCKDUCKGO
import com.base.browserwhite.bean.ConstObject.GOOGLE
import com.base.browserwhite.bean.ConstObject.YAHOO
import com.base.browserwhite.bean.ConstObject.YANDEX
import com.base.browserwhite.databinding.DialogDeleteTipBinding
import com.base.browserwhite.databinding.DialogMediaMoreBinding
import com.base.browserwhite.databinding.DialogMyAccountBinding
import com.base.browserwhite.databinding.DialogPermissonOpenBinding
import com.base.browserwhite.databinding.DialogSearchEngineBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog

object DialogViews {

    @SuppressLint("SetTextI18n")
    fun Context.showGerPermission(
        tittle: String? = null,
        desc: String? = null,
        denyText: String? = null,
        allowText: String? = null,
        deny: ((view: Dialog) -> Unit)? = null,
        allow: ((view: Dialog) -> Unit)? = null
    ): Dialog {
        val dialog = Dialog(this)
        val binding = DialogPermissonOpenBinding.inflate(LayoutInflater.from(this))
        dialog.requestWindowFeature(1)
        dialog.window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        dialog.window!!.setLayout(-1, -1)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        dialog.setContentView(binding.root)
        val str = resources.getString(R.string.app_name)

        tittle?.let { binding.tvTitle.text = it }
        desc?.let { binding.tvDesc.text = it }
        denyText?.let { binding.idDeny.text = it }
        allowText?.let { binding.idTvAllow.text = it }

        binding.idFullLottie.imageAssetsFolder = "permission_finger/images/"
        binding.idFullLottie.setAnimation("permission_finger/data.json")
        binding.idFullLottie.playAnimation()
        binding.idDeny.setOnClickListener {
            dialog.dismiss()
            deny?.invoke(dialog)
        }
        binding.idTvAllow.setOnClickListener {
            dialog.dismiss()
            allow?.invoke(dialog)
        }
        dialog.show()
        return dialog
    }

    fun Context.showSearchEngineDialog(selectWebSite: String = GOOGLE, confirmAction: (webSite: String) -> Unit) {

        val binding = DialogSearchEngineBinding.inflate(LayoutInflater.from(this))
        val dialog = AlertDialog.Builder(this).create()
        dialog.setView(binding.root)
        dialog.show()

        val params = dialog.window?.attributes
        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_345)
//        params?.width = FrameLayout.LayoutParams.WRAP_CONTENT
        params?.height = FrameLayout.LayoutParams.WRAP_CONTENT
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }

        when (selectWebSite) {
            GOOGLE -> binding.llGoogle.isSelected = true
            BING -> binding.llBing.isSelected = true
            YAHOO -> binding.llYahoo.isSelected = true
            DUCKDUCKGO -> binding.llDuck.isSelected = true
            YANDEX -> binding.llYandex.isSelected = true
            BAIDU -> binding.llBaidu.isSelected = true
        }

        val dismissSelect = {
            binding.llGoogle.isSelected = false
            binding.llBing.isSelected = false
            binding.llYahoo.isSelected = false
            binding.llDuck.isSelected = false
            binding.llYandex.isSelected = false
            binding.llBaidu.isSelected = false
        }

        var tempSelectWebSite = GOOGLE

        binding.llGoogle.setOnClickListener {
            dismissSelect.invoke()
            binding.llGoogle.isSelected = true
            tempSelectWebSite = GOOGLE
        }
        binding.llBing.setOnClickListener {
            dismissSelect.invoke()
            binding.llBing.isSelected = true
            tempSelectWebSite = BING
        }
        binding.llYahoo.setOnClickListener {
            dismissSelect.invoke()
            binding.llYahoo.isSelected = true
            tempSelectWebSite = YAHOO
        }
        binding.llDuck.setOnClickListener {
            dismissSelect.invoke()
            binding.llDuck.isSelected = true
            tempSelectWebSite = DUCKDUCKGO
        }
        binding.llYandex.setOnClickListener {
            dismissSelect.invoke()
            binding.llYandex.isSelected = true
            tempSelectWebSite = YANDEX
        }
        binding.llBaidu.setOnClickListener {
            dismissSelect.invoke()
            binding.llBaidu.isSelected = true
            tempSelectWebSite = BAIDU
        }

        binding.tvConfirm.setOnClickListener {
            dialog.dismiss()
            confirmAction.invoke(tempSelectWebSite)
        }

    }

    fun Context.showMyAccountDialog() {
        val dialog = BottomSheetDialog(this)
        val binding = DialogMyAccountBinding.inflate(LayoutInflater.from(this))
        dialog.setContentView(binding.root)
        dialog.setCanceledOnTouchOutside(false)

        dialog.show()

        val parentView = binding.root.parent as View
        val behavior = BottomSheetBehavior.from(parentView)
        //展开
        behavior.state = BottomSheetBehavior.STATE_EXPANDED

        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
    }

    fun Context.showMediaMoreDialog(
        anchorView: View,
        deleteAction: () -> Unit,
        shareAction: () -> Unit,
    ) {
        val binding = DialogMediaMoreBinding.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_180)
//        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 = location[0]
        params?.y = location[1] - resources.getDimensionPixelOffset(R.dimen.dp_200)

        dialog.window?.attributes = params

        binding.llDelete.setOnClickListener {
            dialog.dismiss()
            deleteAction.invoke()
        }
        binding.llShare.setOnClickListener {
            dialog.dismiss()
            shareAction.invoke()
        }

    }

    fun Context.showDeleteTipDialog(action: () -> Unit) {
        val binding = DialogDeleteTipBinding.inflate(LayoutInflater.from(this))
        val dialog = AlertDialog.Builder(this).create()
        dialog.setView(binding.root)

        dialog.show()
        val params = dialog.window?.attributes
        params?.width=resources.getDimensionPixelOffset(R.dimen.dp_335)
        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)
        binding.tvCancel.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvDelete.setOnClickListener {
            dialog.dismiss()
            action.invoke()
        }
    }

}