package com.xyzshell.myphoneinfo.dialog

import android.app.Dialog
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.DialogFragment
import com.xyzshell.myphoneinfo.R
import com.xyzshell.myphoneinfo.databinding.FragmentBottomDialogBinding


class BottomDialogFragment(val type:String, sel:Int? = null,val invoke:(Int)->Unit) : DialogFragment() {
    private lateinit var vb: FragmentBottomDialogBinding
    private var select=sel?:0
    private var appType=type
    companion object {
        const val FROMAPP = "app"
        const val USER = 0
        const val SYSTEM = 1
        const val ALL = 2
    }

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

    private fun showUI(select: Int) {
        when (select) {
            USER->{
                vb.user.nameText.setTextColor(Color.parseColor("#3B948A"))
                vb.user.check.visibility=View.VISIBLE
                vb.system.nameText.setTextColor(Color.parseColor("#C1C5C2"))
                vb.system.check.visibility=View.GONE
                vb.all.nameText.setTextColor(Color.parseColor("#C1C5C2"))
                vb.all.check.visibility=View.GONE
            }
            SYSTEM->{
                vb.user.nameText.setTextColor(Color.parseColor("#C1C5C2"))
                vb.user.check.visibility=View.GONE
                vb.system.nameText.setTextColor(Color.parseColor("#3B948A"))
                vb.system.check.visibility=View.VISIBLE
                vb.all.nameText.setTextColor(Color.parseColor("#C1C5C2"))
                vb.all.check.visibility=View.GONE
            }
            ALL->{
                vb.user.nameText.setTextColor(Color.parseColor("#C1C5C2"))
                vb.user.check.visibility=View.GONE
                vb.system.nameText.setTextColor(Color.parseColor("#C1C5C2"))
                vb.system.check.visibility=View.GONE
                vb.all.nameText.setTextColor(Color.parseColor("#3B948A"))
                vb.all.check.visibility=View.VISIBLE
            }

        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): 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.user.nameText.text=getString(R.string.user)
        vb.system.nameText.text=getString(R.string.system)
        vb.all.nameText.text=getString(R.string.all)
        showUI(select)
        if(appType == FROMAPP){
            showUI(select)
        }
        vb.user.root.setOnClickListener {
            showUI(USER)
            invoke(USER)
            dismiss()
        }

        vb.system.root.setOnClickListener {
            showUI(SYSTEM)
            invoke(SYSTEM)
            dismiss()
        }
        vb.all.root.setOnClickListener {
            showUI(ALL)
            invoke(ALL)
            dismiss()
        }
    }

}