package com.app.intelligent.board.uifragment

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.provider.Settings
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import com.app.intelligent.board.AppContextHelper
import com.app.intelligent.board.R
import com.app.intelligent.board.databinding.KeyboardActivationDialogBinding
import com.app.intelligent.board.utils.AppCommonUtils


class KeyboardActivationDialog : DialogFragment() {

    private lateinit var vb: KeyboardActivationDialogBinding

    private lateinit var layoutStepOne: LinearLayout
    private lateinit var layoutStepTwo: LinearLayout
    private lateinit var imgStepOkOne: ImageView
    private lateinit var imgStepOkTwo: ImageView
    private lateinit var intentFilter: IntentFilter
    private var myreceiver: BroadcastReceiver? = null

    private lateinit var stepOne: TextView
    private lateinit var stepTwo: TextView

    private lateinit var context: Context

    private var clickAction: (() -> Unit )? = null



    companion object {
        fun newInstance(): KeyboardActivationDialog {
            val fragment = KeyboardActivationDialog()
            return fragment
        }
    }


    fun setClickListener(action:() -> Unit){
        clickAction = action
    }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        vb = KeyboardActivationDialogBinding.inflate(layoutInflater)
        context = AppContextHelper.Companion.appInstance


        findViewId()
        onViewStep()
        getReceiver()
        return vb.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.run {
            setCanceledOnTouchOutside(true)
            window?.run {
                setGravity(Gravity.BOTTOM)
                setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))

                attributes = attributes.apply {
                    width = WindowManager.LayoutParams.MATCH_PARENT
                    height = WindowManager.LayoutParams.WRAP_CONTENT
                }
            }

        }
    }

    private fun findViewId() {

        layoutStepOne = vb.linearStepOne
        layoutStepTwo = vb.linearStepTwo
        imgStepOkOne = vb.okOne
        imgStepOkTwo = vb.okTwo
        stepOne = vb.textStepOne
        stepTwo = vb.textStepTwo
    }

    private fun onViewStep() {

        layoutStepOne.setOnClickListener {
            startActivity(Intent(Settings.ACTION_INPUT_METHOD_SETTINGS))
        }
        layoutStepTwo.setOnClickListener {
            val inputMethodManager =
                context.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.showInputMethodPicker()
        }
        vb.imClose.setOnClickListener {
            dismiss()
        }
    }

    override fun onResume() {
        super.onResume()
        updateUI()
    }

    private fun getReceiver() {
        myreceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context?, intent: Intent?) {
                updateUI()
            }
        }
        intentFilter = IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED)

        context.registerReceiver(myreceiver, intentFilter)
    }

    private fun updateUI() {

        val checkEnable = AppCommonUtils.checkEnable(AppContextHelper.Companion.appInstance)
        val checkSetDefault = AppCommonUtils.checkSetDefault(AppContextHelper.Companion.appInstance)
        if (checkEnable && checkSetDefault) {
            clickAction?.invoke()
            dismiss()
            return
        }
        if (checkEnable) {
            layoutStepOne.isEnabled = false
            layoutStepOne.isSelected = true
            imgStepOkOne.isVisible = true
            stepOne.setTextColor(context.getColor(R.color.step_true))
        } else {
            layoutStepOne.isEnabled = true
            layoutStepOne.isSelected = false
            imgStepOkOne.isVisible = false
            stepOne.setTextColor(context.getColor(R.color.white))
        }

        if (checkSetDefault) {
            layoutStepTwo.isEnabled = false
            layoutStepTwo.isSelected = true
            imgStepOkTwo.isVisible = true
            stepTwo.setTextColor(context.getColor(R.color.step_true))
        } else {
            layoutStepTwo.isEnabled = true
            layoutStepTwo.isSelected = false
            imgStepOkTwo.isVisible = false
            stepTwo.setTextColor(context.getColor(R.color.white))
        }


    }

    override fun onDestroy() {
        super.onDestroy()
        if (myreceiver != null) {
            context.unregisterReceiver(myreceiver)
        }
    }



}