package com.baijiayun.live.ui.preview

import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.baijiayun.bjyutils.drawable.DrawableBuilder
import com.baijiayun.bjyutils.kt.dp
import com.baijiayun.live.ui.R
import com.baijiayun.live.ui.databinding.BjyFragmentSettingPreviewDialogBinding
import com.baijiayun.livebase.base.BaseDialogFragment
import com.baijiayun.livebase.utils.ThemeDataUtil
import com.baijiayun.liveuibase.widgets.setting.BaseSettingFragment

/**
 * @author        chenfu
 * @date          2023/4/4
 * description：
 */
abstract class PreviewSettingDialogFragment : BaseDialogFragment() {

    private var _binding: BjyFragmentSettingPreviewDialogBinding? = null
    private val binding get() = _binding!!

    abstract fun getSettingFragment(): BaseSettingFragment

    override fun getLayoutId() = R.layout.bjy_fragment_setting_preview_dialog

    override fun init(savedInstanceState: Bundle?, arguments: Bundle?) {
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        _binding = BjyFragmentSettingPreviewDialogBinding.bind(contentView)
        hideTitleBar()
        childFragmentManager
            .beginTransaction()
            .replace(binding.settingContainer.id, getSettingFragment())
            .commitAllowingStateLoss()
        binding.closeIv.setOnClickListener {
            dismissAllowingStateLoss()
        }
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.setBackgroundDrawable(
            DrawableBuilder()
                .rectangle()
                .solidColor(
                    ThemeDataUtil.getColorFromThemeConfigByAttrId(
                        context,
                        com.baijiayun.liveuibase.R.attr.base_theme_window_bg_color
                    )
                )
                .topLeftRadius(4.dp)
                .bottomLeftRadius(4.dp)
                .build()
        )
    }

    override fun setWindowParams(windowParams: WindowManager.LayoutParams) {
        super.setWindowParams(windowParams)
        windowParams.width = realWindowWidth().dp
        windowParams.height = WindowManager.LayoutParams.MATCH_PARENT
        windowParams.gravity = Gravity.END
    }

    open fun realWindowWidth() = 300

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}