package com.input.board.appskey.keyboard.viewUi

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.view.isVisible
import com.input.board.appskey.keyboard.R
import com.input.board.appskey.keyboard.fileTools.InputMethodCom

class KeyBoardSettingActivity : AppCompatActivity() {

    private lateinit var layoutStepOne: LinearLayout
    private lateinit var imgBack: ImageView
    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
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_step)
        findViewId()
        onViewStep()
        getReceiver()
    }

    @SuppressLint("SuspiciousIndentation")
    private fun findViewId() {
        layoutStepOne = findViewById<LinearLayout>(R.id.linear_step_one)
        layoutStepTwo = findViewById<LinearLayout>(R.id.linear_step_two)
        imgBack = findViewById(R.id.back)
        imgStepOkOne = findViewById(R.id.ok_one)
        imgStepOkTwo = findViewById(R.id.ok_two)
        stepOne= findViewById(R.id.text_step_one)
        stepTwo= findViewById(R.id.text_step_two)
    }

    private fun onViewStep(){
        imgBack.setOnClickListener {
            finish()
        }
        layoutStepOne.setOnClickListener {
            startActivity(Intent(Settings.ACTION_INPUT_METHOD_SETTINGS))
        }
        layoutStepTwo.setOnClickListener {
            val inputMethodManager = this.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.showInputMethodPicker()
        }
    }

    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)

        registerReceiver(myreceiver,intentFilter)
    }

    private fun updateUI(){

        val checkEnable = InputMethodCom.checkEnable(this)
        val checkSetDefault = InputMethodCom.checkSetDefault(this)
        if(checkEnable){
            layoutStepOne.isEnabled =false
            layoutStepOne.isSelected = true
            imgStepOkOne.isVisible = true
            stepOne.setTextColor(getColor(R.color.step_true))
        }else{
            layoutStepOne.isEnabled =true
            layoutStepOne.isSelected = false
            imgStepOkOne.isVisible = false
            stepOne.setTextColor(getColor(R.color.white))
        }

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


    }

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