package com.gh.gamecenter.gamecollection.detail

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

class GameCollectionShareDialog : BaseDraggableDialogFragment() {

    private lateinit var mBinding: DialogGameCollectionShareBinding
    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 DialogGameCollectionShareBinding.inflate(inflater, container, false)
            .apply { mBinding = this }.root
    }

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

        mBinding.run {
            wechatItem.setOnClickListener {
                logEvent("微信")
                mShareUtils?.wechatShare()
            }

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

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

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

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

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

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

            cancelTv.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())
            cancelTv.background = R.drawable.button_round_gray_light.toDrawable(requireContext())
            dividerLine1.setBackgroundColor(R.color.divider.toColor(requireContext()))
            cancelTv.setTextColor(R.color.text_subtitle.toColor(requireContext()))
            titleTv.setTextColor(R.color.text_title.toColor(requireContext()))
            handleView.background = R.drawable.download_dialog_close_hint.toDrawable(requireContext())
            shareContainerOne.children.forEach {
                if (it is LinearLayout && it.getChildAt(1) is TextView) {
                    (it.getChildAt(1) as TextView).setTextColor(R.color.text_subtitle.toColor(requireContext()))
                }
            }
            shareContainerTwo.children.forEach {
                if (it is LinearLayout && it.getChildAt(1) is TextView) {
                    (it.getChildAt(1) as TextView).setTextColor(R.color.text_subtitle.toColor(requireContext()))
                }
            }
        }
    }

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

        @JvmStatic
        fun showMoreDialog(
            activity: AppCompatActivity,
            share: NormalShareEntity,
        ) {
            GameCollectionShareDialog().apply {
                arguments = Bundle().apply {
                    putParcelable(KEY_SHARE, share)
                }
            }.show(
                activity.supportFragmentManager,
                GameCollectionShareDialog::class.java.name
            )
        }
    }
}