package com.design.wallpapergallery.view

import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.Window
import com.design.wallpapergallery.R
import com.design.wallpapergallery.databinding.FragmentDialogSelectBinding
import com.design.wallpapergallery.listener.OpenTypeListener
import androidx.core.graphics.drawable.toDrawable


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

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: android.os.Bundle?
    ): android.view.View {
        vb = FragmentDialogSelectBinding.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.BottomSheetDialogStyle)
            setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
            setGravity(Gravity.BOTTOM)
        }

        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()
        }
    }
}