package com.sunny.app.soft.timberkeyboardnew.ui.activity

import android.app.AlertDialog
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.provider.Settings
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.sunny.app.soft.timberkeyboardnew.R
import com.sunny.app.soft.timberkeyboardnew.databinding.ActivityEnableBinding
import com.sunny.app.soft.timberkeyboardnew.tools.KeyboardManager

class SetKeyboardActivity :
    BaseActivity(),
    View.OnClickListener {

    private lateinit var binding: ActivityEnableBinding
    private lateinit var inputManager: InputMethodManager
    private lateinit var broadcastReceiver: BroadcastReceiver

    override fun setRootView(): View {
        binding = ActivityEnableBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        super.initView()
        inputManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
        broadcastReceiver = StepperReceiver()
        updateUi()
        register()
        initButton()
    }
    override fun loadAd(): Boolean = true
    private fun initButton() {
        binding.setKeyboardBack.setOnClickListener(this)
        binding.setKeyboardSteps1.setOnClickListener(this)
        binding.setKeyboardSteps2.setOnClickListener(this)
    }

    private fun register() {
        registerReceiver(broadcastReceiver, IntentFilter(Intent.ACTION_INPUT_METHOD_CHANGED))
    }

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

    }

    private fun updateUi() {
//        if (KeyboardManager.isEnable()) {
//            binding.setKeyboardSteps1.setBackgroundResource(R.drawable.shape_r24_grey)
//        } else {
//            binding.setKeyboardSteps1.setBackgroundResource(R.drawable.shape_r24_orange)
//        }
//        if (KeyboardManager.isChoose()) {
//            binding.setKeyboardSteps2.setBackgroundResource(R.drawable.shape_r24_grey)
//        } else {
//            binding.setKeyboardSteps2.setBackgroundResource(R.drawable.shape_r24_orange)
//        }
//        if (KeyboardManager.isChoose() && KeyboardManager.isEnable()) {
//            showInfoDialog()
//        }

    }

    private fun showInfoDialog() {
        AlertDialog.Builder(this).apply {
            title = "Settings"
            setMessage("You have successfully activated the soft keyboard!")
            setPositiveButton("OK") { _, _ -> finish() }
        }.create().show()
    }

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

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(broadcastReceiver)
    }

    override fun onClick(v: View?) {
        when (v) {
            binding.setKeyboardBack -> finish()
            binding.setKeyboardSteps1 -> chooseKeyboard()
            binding.setKeyboardSteps2 -> settingKeyboard()
        }
    }

    private fun settingKeyboard() {
        inputManager.showInputMethodPicker()
    }

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

}