package com.cheng.blzb.ui.dialog

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.view.ViewTreeObserver
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.DialogFragment
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.common.EventConstants
import com.cheng.blzb.databinding.DialogPublishBidBinding
import com.cheng.blzb.manager.EventReportManager
import com.cheng.blzb.ui.activity.PublicActivity
import com.cheng.blzb.ui.fragment.bid.publish.bid.PublishBidMessageFragment
import com.cheng.blzb.ui.fragment.bid.publish.goods.PublishGoodsMessageFragment
import com.example.base.extensions.onClick
import com.example.base.utils.ScreenUtils

class PublishBidDialog : DialogFragment() {
    private lateinit var binding: DialogPublishBidBinding

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window?.attributes
        windowParams?.dimAmount = 0.7f
        windowParams?.width = ScreenUtils.getWindowSize().x
        windowParams?.gravity = Gravity.BOTTOM
        windowParams?.windowAnimations = R.style.dialog_bottom
        dialog?.window?.attributes = windowParams
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_publish_bid, null)

        binding = DialogPublishBidBinding.bind(view)

        binding.tvTitle.typeface = Constants.pmzdbt
        binding.tvBidTitle.typeface = Constants.almmsht
        binding.tvGoodsTitle.typeface = Constants.almmsht

        binding.layoutBid.setupWith(binding.root)
            .setFrameClearDrawable(binding.layoutBid.background)
            .setBlurRadius(5f)
        binding.layoutBid.clipToOutline = true
        binding.layoutBid.outlineProvider = ViewOutlineProvider.BACKGROUND

        binding.layoutGoods.setupWith(binding.root)
            .setFrameClearDrawable(binding.layoutBid.background)
            .setBlurRadius(5f)
        binding.layoutGoods.clipToOutline = true
        binding.layoutGoods.outlineProvider = ViewOutlineProvider.BACKGROUND

        binding.layoutBid.onClick {
            PublicActivity.start(requireActivity(), PublishBidMessageFragment::class.java)
            EventReportManager.eventReport(EventConstants.JUMP_TO_PUBLISH, "home", "标讯")
            dismiss()
        }

        binding.layoutGoods.onClick {
            PublicActivity.start(requireActivity(), PublishGoodsMessageFragment::class.java, Pair("from", 1))
            EventReportManager.eventReport(EventConstants.JUMP_TO_PUBLISH, "home", "商品")
            dismiss()
        }

        binding.ivClose.onClick { dismiss() }

        binding.ivTopBg.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                val lp = binding.tvTitle.layoutParams as ConstraintLayout.LayoutParams
                lp.topMargin = (binding.ivTopBg.height * 55f / 291f).toInt()
                binding.tvTitle.layoutParams = lp

                binding.ivTopBg.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })

        val dialog = Dialog(requireContext())
        dialog.setContentView(view)
        return dialog
    }

    companion object {
        fun newInstance(): PublishBidDialog {
            val arg = Bundle()
            val fragment = PublishBidDialog()
            fragment.arguments = arg
            return fragment
        }
    }
}