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 androidx.fragment.app.DialogFragment
import com.example.base.extensions.onClick
import com.example.base.extensions.toast
import com.example.base.utils.ClipboardUtils
import com.example.base.utils.ScreenUtils
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.umeng.socialize.bean.SHARE_MEDIA
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.common.EventConstants
import com.cheng.blzb.databinding.DialogShareBinding
import com.cheng.blzb.manager.EventReportManager
import com.cheng.blzb.manager.ShareManager
import com.cheng.blzb.manager.UserConfigManager

class ShareDialog : DialogFragment() {
    private lateinit var api: IWXAPI

    lateinit var binding: DialogShareBinding

    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 {
        api = WXAPIFactory.createWXAPI(requireContext(), Constants.WechatAppId)

        val view = layoutInflater.inflate(R.layout.dialog_share, null)
        binding = DialogShareBinding.bind(view)

        binding.ivWxShare.onClick {
            if (!api.isWXAppInstalled) {
                toast("您没有安装微信客户端,请先下载安装")
                return@onClick
            }
            ShareManager.shareUrl(requireActivity(), SHARE_MEDIA.WEIXIN, UserConfigManager.getShareEntity()!!) {}
            EventReportManager.eventReport(EventConstants.SHARE_APP, "", "weixin")
            dismiss()
        }

        binding.ivWxCircleShare.onClick {
            if (!api.isWXAppInstalled) {
                toast("您没有安装微信客户端,请先下载安装")
                return@onClick
            }
            ShareManager.shareUrl(requireActivity(), SHARE_MEDIA.WEIXIN_CIRCLE, UserConfigManager.getShareEntity()!!) {}
            EventReportManager.eventReport(EventConstants.SHARE_APP, "", "weixin_friend")
            dismiss()
        }

        binding.ivLinkShare.onClick {
            ClipboardUtils.copyText(UserConfigManager.getShareEntity()!!.link)
            EventReportManager.eventReport(EventConstants.SHARE_APP, "", "copy_url")
            toast("复制成功")
            dismiss()
        }

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