package com.ray650128.floatingwindow

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import androidx.core.widget.addTextChangedListener
import com.ray650128.floatingwindow.databinding.ActivitySettingBinding

class SettingActivity : AppCompatActivity() {

    private lateinit var binding: ActivitySettingBinding

    private var xOffset = 0
    private var yOffset = 0

    private var windowGravity = FloatingWindowHelper.GravityType.TOP_LEFT

    private var windowIcon = FloatingWindowHelper.IconType.CHICKEN

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivitySettingBinding.inflate(layoutInflater)
        setContentView(binding.root)

        xOffset = PreferenceUtil.xOffset
        yOffset = PreferenceUtil.yOffset
        windowGravity = FloatingWindowHelper.GravityType.fromInt(PreferenceUtil.gravity)
        windowIcon = FloatingWindowHelper.IconType.fromInt(PreferenceUtil.icon)

        binding.apply {
            spGravity.apply {
                setSelection(PreferenceUtil.gravity)
                onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                        windowGravity = FloatingWindowHelper.GravityType.fromInt(position)
                        FloatingWindowHelper.setGravity(windowGravity)
                    }

                    override fun onNothingSelected(parent: AdapterView<*>?) {}
                }
            }

            spIcon.apply {
                setSelection(FloatingWindowHelper.IconType.getIndex(PreferenceUtil.icon) )
                onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
                    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                        windowIcon = FloatingWindowHelper.IconType.fromIndex(position)
                        FloatingWindowHelper.setIcon(windowIcon)
                    }

                    override fun onNothingSelected(parent: AdapterView<*>?) {}
                }
            }

            etXOffset.apply {
                setText("$xOffset")
                addTextChangedListener {
                    if (it.isNullOrEmpty()) return@addTextChangedListener
                    xOffset = it.toString().toIntOrNull() ?: return@addTextChangedListener
                    FloatingWindowHelper.setXOffset(xOffset)
                }
            }

            etYOffset.apply {
                setText("$yOffset")
                addTextChangedListener {
                    if (it.isNullOrEmpty()) return@addTextChangedListener
                    yOffset = it.toString().toIntOrNull() ?: return@addTextChangedListener
                    FloatingWindowHelper.setYOffset(yOffset)
                }
            }

            btnOk.setOnClickListener {
                PreferenceUtil.xOffset = xOffset
                PreferenceUtil.yOffset = yOffset
                PreferenceUtil.gravity = windowGravity.type
                PreferenceUtil.icon = windowIcon.value
                finish()
            }

            btnCancel.setOnClickListener {
                FloatingWindowHelper.setXOffset(PreferenceUtil.xOffset)
                FloatingWindowHelper.setYOffset(PreferenceUtil.yOffset)
                FloatingWindowHelper.setGravity(FloatingWindowHelper.GravityType.fromInt(PreferenceUtil.gravity))
                FloatingWindowHelper.setIcon(FloatingWindowHelper.IconType.fromInt(PreferenceUtil.icon))
                finish()
            }
        }
    }
}