package com.gh.gamecenter.newsdetail

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import com.gh.gamecenter.R
import com.gh.gamecenter.common.base.fragment.BaseDraggableDialogFragment
import com.gh.gamecenter.common.entity.NormalShareEntity
import com.gh.gamecenter.common.eventbus.EBShare
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.databinding.DialogNewsDetailShareBinding
import org.greenrobot.eventbus.EventBus

class NewsShareDialog : BaseDraggableDialogFragment() {

    private lateinit var mBinding: DialogNewsDetailShareBinding
    private var mShareEntity: NormalShareEntity? = null
    private var mShareUtils: ShareUtils? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mShareEntity = requireArguments().getParcelable(KEY_SHARE)
        mShareUtils = getShareUtils()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return DialogNewsDetailShareBinding.inflate(inflater, container, false)
            .apply { mBinding = this }.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mBinding.run {
            tvTitle.text = mShareEntity?.shareTitle ?: ""

            vWechatBackground.setOnClickListener {
                logEvent("微信")
                mShareUtils?.wechatShare()
            }

            vWechatMomentsBackground.setOnClickListener {
                logEvent("朋友圈")
                mShareUtils?.wechatMomentsShare()
            }

            vQqBackground.setOnClickListener {
                logEvent("QQ好友")
                mShareUtils?.qqShare()
            }

            vQqZoneBackground.setOnClickListener {
                logEvent("QQ空间")
                mShareUtils?.qZoneShare()
            }

            vWeiboBackground.setOnClickListener {
                logEvent("新浪微博")
                mShareUtils?.sinaWeiboShare()
            }

            vSmsBackground.setOnClickListener {
                logEvent("短信")
                EventBus.getDefault().post(EBShare(ShareUtils.shareEntrance, "短信"))
                mShareUtils?.shortMessageShare()
            }

            vCopyLinkBackground.setOnClickListener {
                logEvent("复制链接")
                EventBus.getDefault().post(EBShare(ShareUtils.shareEntrance, "复制链接"))
                mShareUtils?.copyLink(mShareUtils?.shareUrl ?: "")
            }

            tvCancel.setOnClickListener {
                dismissAllowingStateLoss()
            }
        }
    }

    private fun getShareUtils(): ShareUtils {
        val shareUtils = ShareUtils.getInstance(requireContext())
        mShareEntity?.run {
            shareUtils.shareParamsDetail(
                requireActivity(),
                shareUrl,
                shareIcon,
                shareTitle,
                shareSummary,
                shareEntrance,
                id
            )
        }
        return shareUtils
    }

    private fun logEvent(shareType: String) {
        NewLogUtils.logViewOrClickGameCollectionDetail(
            "click_game_collect_detail_share",
            mShareEntity?.shareTitle ?: "",
            mShareEntity?.id ?: "",
            shareType
        )
        SensorsBridge.trackEvent(
            "GameCollectDetailShareClick",
            "game_collect_title", mShareEntity?.shareTitle ?: "",
            "game_collect_id", mShareEntity?.id ?: "",
            "share_type", shareType
        )
    }

    override fun getRootView(): View = mBinding.root
    override fun getDragCloseView(): View = mBinding.dragClose

    override fun onDarkModeChanged() {
        super.onDarkModeChanged()
        mBinding.run {
            container.background = R.drawable.game_detail_more_dialog_background.toDrawable(requireContext())
            tvCancel.background = com.gh.gamecenter.common.R.drawable.bg_common_button_light_fill_gray.toDrawable(requireContext())
            dividerLine1.setBackgroundColor(com.gh.gamecenter.common.R.color.ui_divider.toColor(requireContext()))
            tvCancel.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(requireContext()))
            tvTitle.setTextColor(com.gh.gamecenter.common.R.color.text_secondary.toColor(requireContext()))
            handleView.background = R.drawable.download_dialog_close_hint.toDrawable(requireContext())
            clShare.children.forEach {
                if (it is TextView) {
                    it.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(requireContext()))
                }
            }
            tvCopyLink.setTextColor(com.gh.gamecenter.common.R.color.text_tertiary.toColor(requireContext()))
        }
    }

    companion object {
        const val KEY_SHARE = "share"
        const val REQUEST_CODE = 1105

        @JvmStatic
        fun show(activity: AppCompatActivity, shortId: String?, id: String?, gameIcon: String?, title: String?) {
            val entity = NormalShareEntity(
                id ?: "",
                activity.getString(R.string.share_news_article_url, shortId),
                gameIcon ?: activity.getString(com.gh.gamecenter.common.R.string.gh_icon_url),
                title ?: "",
                activity.getString(R.string.news_share_description),
                ShareUtils.ShareEntrance.news,
                null
            )
            NewsShareDialog().apply {
                arguments = Bundle().apply {
                    putParcelable(KEY_SHARE, entity)
                }
            }.show(
                activity.supportFragmentManager,
                NewsShareDialog::class.java.name
            )
        }
    }
}