package com.cheng.blzb.ui.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import com.cheng.blzb.R
import com.cheng.blzb.common.Constants
import com.cheng.blzb.databinding.DialogGuideSaleBinding
import com.cheng.blzb.manager.UserConfigManager
import com.example.base.extensions.onClick
import com.example.base.utils.ScreenUtils

class GuideSaleDialog : DialogFragment() {
    private var totalSeconds = 0L

    private var mOnBackListener: ((Long) -> Unit)? = null     //回调事件

    lateinit var binding: DialogGuideSaleBinding

    override fun onStart() {
        super.onStart()
        val window = dialog?.window
        val windowParams = window?.attributes
        windowParams?.dimAmount = 0.7f
        windowParams?.width = ScreenUtils.getWindowSize().x
        windowParams?.height = ScreenUtils.getWindowSize().y
        dialog?.window?.attributes = windowParams
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        setCancelable(false)
        return super.onCreateView(inflater, container, savedInstanceState)
    }

    @SuppressLint("SetTextI18n")
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = layoutInflater.inflate(R.layout.dialog_guide_sale, null)

        binding = DialogGuideSaleBinding.bind(view)

        binding.tvEndTip2.typeface = Constants.almmsht
        binding.tvOriginPrice.typeface = Constants.dDIN_PRO_M
        binding.tvPrice.typeface = Constants.dDIN_PRO_M
        binding.tvNext.typeface = Constants.almmsht

        val countDownTime = UserConfigManager.getGuideCountdownTime()
        if (!TextUtils.isEmpty(countDownTime)) {
            totalSeconds = countDownTime!!.split(",")[1].toLong()
            binding.tvNext.text = "考虑${totalSeconds}秒"
        }

        binding.tvNext.onClick {
            mOnBackListener?.invoke(totalSeconds)
            dismiss()
        }

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

    fun setOnSelectListener(listener: ((Long) -> Unit)) {
        mOnBackListener = listener
    }

    companion object {
        fun newInstance(): GuideSaleDialog {
            val arg = Bundle()
            val fragment = GuideSaleDialog()
            fragment.arguments = arg
            return fragment
        }
    }
}