package com.zxhy.solarmasterace.popupwindow

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.PopupWindow
import androidx.core.content.ContextCompat
import com.zxhy.solarmasterace.R
import com.zxhy.solarmasterace.databinding.PopupwindowMoreOperationBinding

@SuppressLint("ClickableViewAccessibility")
fun showPopupMoreOperation(
    activity: Activity,
    arcView: View,
    size: Int = 0,
    renameAction: (() -> Unit)? = null,
    detailAction: (() -> Unit)? = null,
    shareAction: (() -> Unit)? = null
) {
    var yOff = 0
    var height = 0

    val popupWindowView = activity.layoutInflater.inflate(R.layout.popupwindow_more_operation, null)
    val popupBinding = PopupwindowMoreOperationBinding.bind(popupWindowView)

    if (size == 1) {
        yOff = activity.resources.getDimensionPixelSize(R.dimen.dp_175)
        height = activity.resources.getDimensionPixelSize(R.dimen.dp_112)
    } else {
        popupBinding.llRename.visibility = View.GONE
        popupBinding.llDetails.visibility = View.GONE
        yOff = activity.resources.getDimensionPixelSize(R.dimen.dp_100)
        height = activity.resources.getDimensionPixelSize(R.dimen.dp_46)
    }

    val width =
        activity.resources.getDimensionPixelSize(R.dimen.dp_108)
    val popupWindow = PopupWindow(popupWindowView, width, height, true)
    popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    popupWindow.isOutsideTouchable = true
    popupWindow.isTouchable = true

    popupBinding.apply {

        llRename.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    tvRename.setTextColor(ContextCompat.getColor(activity, R.color.color_FF333333))
                    ivRename.setImageResource(R.mipmap.dsd_7886)
                }

                MotionEvent.ACTION_UP -> {
                    tvRename.setTextColor(ContextCompat.getColor(activity, R.color.color_FF999999))
                    ivRename.setImageDrawable(null)
                }

                else -> {}
            }
            false
        }
        llRename.setOnClickListener {
            tvRename.setTextColor(ContextCompat.getColor(activity, R.color.color_FF333333))
            ivRename.setImageResource(R.mipmap.dsd_7886)
            popupWindow.dismiss()
            renameAction?.invoke()
        }

        llDetails.setOnTouchListener { _, event ->
            when (event.action) {

                MotionEvent.ACTION_DOWN -> {
                    tvDetails.setTextColor(ContextCompat.getColor(activity, R.color.color_FF333333))
                    ivDetails.setImageResource(R.mipmap.dsd_7886)
                }

                MotionEvent.ACTION_UP -> {
                    tvDetails.setTextColor(ContextCompat.getColor(activity, R.color.color_FF999999))
                    ivDetails.setImageDrawable(null)
                }

                else -> {}
            }
            false
        }
        llDetails.setOnClickListener {
            tvDetails.setTextColor(ContextCompat.getColor(activity, R.color.color_FF333333))
            ivDetails.setImageResource(R.mipmap.dsd_7886)
            popupWindow.dismiss()
            detailAction?.invoke()
        }

        llShare.setOnTouchListener { _, event ->
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    tvShare.setTextColor(ContextCompat.getColor(activity, R.color.color_FF333333))
                    ivShare.setImageResource(R.mipmap.dsd_7886)
                }

                MotionEvent.ACTION_UP -> {
                    tvShare.setTextColor(ContextCompat.getColor(activity, R.color.color_FF999999))
                    ivShare.setImageDrawable(null)
                }

                else -> {}
            }
            false
        }
        llShare.setOnClickListener {
            tvShare.setTextColor(ContextCompat.getColor(activity, R.color.color_FF333333))
            ivShare.setImageResource(R.mipmap.dsd_7886)
            popupWindow.dismiss()
            shareAction?.invoke()
        }

        popupWindow.showAsDropDown(arcView, (arcView.width * 0.75).toInt(), -yOff)

    }
}