package com.ray650128.floatingwindow.ui

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.utils.FloatingWindowHelperUtils
import com.ray650128.floatingwindow.utils.PreferenceUtil
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 = FloatingWindowHelperUtils.GravityType.TOP_LEFT

    private var windowIcon = FloatingWindowHelperUtils.IconType.CHICKEN

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

        xOffset = PreferenceUtil.xOffset
        yOffset = PreferenceUtil.yOffset
        windowGravity = FloatingWindowHelperUtils.GravityType.fromInt(PreferenceUtil.gravity)
        windowIcon = FloatingWindowHelperUtils.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 = FloatingWindowHelperUtils.GravityType.fromInt(position)
                        FloatingWindowHelperUtils.setGravity(windowGravity)
                    }

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

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

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

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

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

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

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