package com.design.zenspace.preview

import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.Window
import com.design.zenspace.R
import com.design.zenspace.databinding.FragmentShowWayBinding
import com.design.zenspace.tools.IntentListener
import androidx.core.graphics.drawable.toDrawable


class ShowWayDialog : androidx.fragment.app.DialogFragment() {
    private lateinit var vb: FragmentShowWayBinding
    private var listener: IntentListener? = null
    companion object {
        const val TYPE_HOME = 0
        const val TYPE_LOCK = 1
        const val TYPE_BOTH = 2
    }
    fun setListener(listener: IntentListener?) {
        this.listener = listener
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: android.os.Bundle?
    ): android.view.View {
        vb = FragmentShowWayBinding.inflate(layoutInflater)
        init()
        return vb.root
    }

    override fun onCreateDialog(savedInstanceState: android.os.Bundle?): android.app.Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)

        dialog.window?.apply {
            requestFeature(Window.FEATURE_NO_TITLE)
            setWindowAnimations(R.style.CenterShowDialogStyle)
            setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            setGravity(Gravity.CENTER)
        }

        return dialog
    }

    override fun onStart() {
        super.onStart()
        dialog?.apply {
            setCanceledOnTouchOutside(true)
            window?.apply {
                setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
                setLayout(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                )
            }
        }
    }


    private fun init() {
        vb.layoutHome.setOnClickListener {
            listener?.onClick(TYPE_HOME)
            dismiss()
        }

        vb.layoutLock.setOnClickListener {
            listener?.onClick(TYPE_LOCK)
            dismiss()
        }

        vb.layoutBoth.setOnClickListener {
            listener?.onClick(TYPE_BOTH)
            dismiss()
        }
    }
}