package com.gh.common.dialog

import android.app.Dialog
import android.graphics.Paint
import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.text.HtmlCompat
import com.gh.gamecenter.common.base.fragment.BaseDialogFragment
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.toColor
import com.gh.gamecenter.R
import com.gh.gamecenter.databinding.DialogGameOffServiceBinding
import com.gh.gamecenter.feature.entity.GameEntity

// 游戏关闭下载弹窗
class GameOffServiceDialogFragment : BaseDialogFragment() {

    private var mDialog: GameEntity.Dialog? = null
    private var mBinding: DialogGameOffServiceBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mDialog = requireArguments().getParcelable(KEY_DIALOG)
    }

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

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

        mBinding?.run {
            mDialog?.run {
                titleTv.text = title
                contentTv.text = HtmlCompat.fromHtml(content, HtmlCompat.FROM_HTML_MODE_LEGACY)
                okTv.setOnClickListener {
                    dismissAllowingStateLoss()
                }

                // 过滤内容为空的元素
                val notEmptySite = sites.filter { it.text.isNotBlank() }

                notEmptySite.forEachIndexed { index, site ->
                    val siteTv = TextView(context)
                    siteTv.layoutParams = LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                    ).apply {
                        topMargin = 24F.dip2px()
                        if (index == notEmptySite.size - 1) bottomMargin = 8F.dip2px()
                    }
                    siteTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14F)
                    siteTv.setTextColor(R.color.theme_font.toColor(requireContext()))
                    siteTv.text = site.text
                    siteTv.paintFlags = siteTv.paintFlags or Paint.UNDERLINE_TEXT_FLAG
                    siteTv.setOnClickListener {
                        //                    MtaHelper.onEvent("游戏下载状态按钮", getKey(), site.text)
                        DirectUtils.directToWebView(requireContext(), site.url, "(关闭下载弹窗)")
                        dismissAllowingStateLoss()
                    }

                    container.addView(siteTv)
                }
            }
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return super.onCreateDialog(savedInstanceState).apply { setCanceledOnTouchOutside(true) }
    }

    companion object {
        const val KEY_DIALOG = "dialog"

        @JvmStatic
        fun getInstance(dialog: GameEntity.Dialog) = GameOffServiceDialogFragment().apply {
            arguments = Bundle().apply {
                putParcelable(KEY_DIALOG, dialog)
            }
        }
    }

}