package com.sunny.app.soft.timberkeyboardnew.tools

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import com.sunny.app.soft.timberkeyboardnew.App
import com.sunny.app.soft.timberkeyboardnew.R

import com.sunny.app.soft.timberkeyboardnew.databinding.DialogStepBinding


class DialogStep(var activity: Activity,var onClose:()->Unit) : DialogFragment() {



    private lateinit var dialogVb: DialogStepBinding



    private var inputManager: InputMethodManager? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        dialogVb = DialogStepBinding.inflate(inflater, container, false)
        init()
        return dialogVb.root
    }


    private fun choseKeyboard() {
        if (inputManager == null)
            inputManager = activity.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager?.showInputMethodPicker()
    }

    private fun EnableKeyboard() {
        val intent = Intent(Settings.ACTION_INPUT_METHOD_SETTINGS)
        startActivity(intent)
    }

    override fun onResume() {
        super.onResume()
        updateUi()
    }
    private fun init() {
        isCancelable = false
        val window = dialog!!.window
        window!!.setBackgroundDrawableResource(R.color.transparent)
        window.decorView.setPadding(0, 0, 0, 0)

        initBar(window)

        val wlp = window.attributes
        wlp.gravity = Gravity.BOTTOM
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.attributes = wlp

        dialogVb.close.setOnClickListener {
            dismiss()

        }
        dialogVb.setKeyboardSteps1.setOnClickListener {
            EnableKeyboard()
        }
        dialogVb.setKeyboardSteps2.setOnClickListener {

            choseKeyboard()
        }
        activity.registerReceiver(
            StepperReceiver(),
            IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED)
        )

    }

    inner class StepperReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            updateUi()
        }

    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        onClose.invoke()
    }

    private fun updateUi() {
        KeyboardManager.isEnable().let {
            dialogVb.setKeyboardSteps1.isSelected = it
            dialogVb.tvSteps1.isSelected = it
            dialogVb.imOkSteps1.isVisible = it
        }

        KeyboardManager.isChoose().let {
            dialogVb.setKeyboardSteps2.isSelected = it
            dialogVb.tvSteps2.isSelected = it
            dialogVb.imOkSteps2.isVisible = it
        }
        if(KeyboardManager.isChoose()&&KeyboardManager.isEnable()){
            dismiss()
        }


    }

    private fun initBar(window: Window) {
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
    }

    companion object {
        @JvmStatic
        fun newInstance(activity: Activity,onClose:()->Unit) = DialogStep(activity,onClose)
    }
}