package com.paperapp.lenscape.preview

import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.Window
import com.paperapp.lenscape.R
import com.paperapp.lenscape.databinding.FragmentDialogGuideBinding
import com.paperapp.lenscape.tools.OnclickListener
import androidx.core.graphics.drawable.toDrawable


class GuideDialog : androidx.fragment.app.DialogFragment() {
    private lateinit var vb: FragmentDialogGuideBinding
    private var listener: OnclickListener? = null
    private var currentType = -1
    companion object {
        const val TYPE_HOME = 0
        const val TYPE_LOCK = 1
        const val TYPE_BOTH = 2

    }
    fun setListener(listener: OnclickListener?) {
        this.listener = listener
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: android.os.Bundle?
    ): android.view.View {
        vb = FragmentDialogGuideBinding.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 {
            currentType=TYPE_HOME
            setUIChange()
        }

        vb.layoutLock.setOnClickListener {
            currentType=TYPE_LOCK
            setUIChange()
        }

        vb.layoutBoth.setOnClickListener {
            currentType=TYPE_BOTH
            setUIChange()
        }
        vb.cancel.setOnClickListener { dismiss() }
        vb.confirm.setOnClickListener {
            listener?.onClick(currentType)
            dismiss()}
    }
    private fun setUIChange(){
        if(currentType==TYPE_HOME){
            vb.layoutHome.isSelected=true
            vb.layoutLock.isSelected=false
            vb.layoutBoth.isSelected=false
        }else if(currentType==TYPE_LOCK){
            vb.layoutHome.isSelected=false
            vb.layoutLock.isSelected=true
            vb.layoutBoth.isSelected=false
        }else{
            vb.layoutHome.isSelected=false
            vb.layoutLock.isSelected=false
            vb.layoutBoth.isSelected=true
        }
    }
}