package com.gh.gamecenter.gamedetail.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.Window
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.gh.common.util.NewLogUtils
import com.gh.gamecenter.common.base.TrackableDialog
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.common.view.VerticalItemDecoration
import com.gh.gamecenter.R
import com.gh.gamecenter.gamedetail.entity.BigEvent

class GameBigEventDialog(
    context: Context,
    val gameName: String,
    val gameId: String,
    val bigEvents: List<BigEvent>,
    val mEntrance: String,
    mEvent: String,
    mKey: String,
    mValue: String
) : TrackableDialog(context, R.style.GhAlertDialog, mEvent, mKey, mValue) {

    private val mDelayLogRunnable = Runnable {
        NewLogUtils.logGameDetailMajorEventView(gameName, gameId)
    }

    override fun onStart() {
        super.onStart()
        if (window != null) {
            window!!.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            val params = window!!.attributes
            params.width = context.resources.displayMetrics.widthPixels - DisplayUtils.dip2px(40f)
            window!!.attributes = params
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)

        val contentView = LayoutInflater.from(context).inflate(R.layout.dialog_game_big_event, null)
        setContentView(contentView)

        val bigEventRv = contentView.findViewById<RecyclerView>(R.id.bigEventRv)
        bigEventRv.adapter = GameBigEventAdapter(context, bigEvents, gameName, mEntrance)
        bigEventRv.layoutManager = LinearLayoutManager(context)
        bigEventRv.addItemDecoration(VerticalItemDecoration(context, 19F, false, R.color.background_white))

        val closeTv = contentView.findViewById<TextView>(R.id.dialog_close)
        closeTv.setOnClickListener {
            dismiss()
        }
        setOnCancelListener {
            mHandler.removeCallbacks(mDelayLogRunnable)
        }

        mHandler.postDelayed(mDelayLogRunnable, 3000)
    }

    companion object {
        private val mHandler = Handler(Looper.getMainLooper())

        fun showGameBigEventDialog(
            context: Context,
            gameName: String,
            gameId: String,
            bigEvents: List<BigEvent>,
            mEntrance: String
        ) {
            val dialog = GameBigEventDialog(context, gameName, gameId, bigEvents, mEntrance, "游戏大事件", "弹窗", gameName)
            dialog.show()
        }
    }

}