package com.ray650128.floatingwindow.ui

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import com.ray650128.floatingwindow.BuildConfig
import com.ray650128.floatingwindow.utils.FloatingWindowHelperUtils
import com.ray650128.floatingwindow.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val mContext: Context by lazy { this }

    private val floatingWindowPermission = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == Activity.RESULT_CANCELED) {
            if (!Settings.canDrawOverlays(mContext)) {
                binding.btnStep1.isEnabled = !Settings.canDrawOverlays(mContext)
                binding.btnStep2.isEnabled = Settings.canDrawOverlays(mContext)
                binding.btnStep3.isEnabled = FloatingWindowHelperUtils.isShowing
                binding.btnSetting.isEnabled = FloatingWindowHelperUtils.isShowing
                return@registerForActivityResult
            }
            showOverlayWindow()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.apply {
            btnStep1.setOnClickListener {
                val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                intent.data = Uri.parse("package:$packageName")
                floatingWindowPermission.launch(intent)
            }

            btnStep2.setOnClickListener {
                showOverlayWindow()
            }

            btnStep3.setOnClickListener {
                hideOverlayWindow()
            }

            btnSetting.setOnClickListener {
                val intent = Intent(mContext, SettingActivity::class.java)
                startActivity(intent)
            }

            textVersion.text = BuildConfig.VERSION_NAME
        }
    }

    override fun onResume() {
        super.onResume()

        binding.apply {
            btnStep1.isEnabled = !Settings.canDrawOverlays(mContext)
            btnStep2.isEnabled = Settings.canDrawOverlays(mContext) && !FloatingWindowHelperUtils.isShowing
            btnStep3.isEnabled = FloatingWindowHelperUtils.isShowing
            btnSetting.isEnabled = FloatingWindowHelperUtils.isShowing
        }
    }

    private fun showOverlayWindow() {
        FloatingWindowHelperUtils.show()
        binding.btnStep2.isEnabled = !FloatingWindowHelperUtils.isShowing
        binding.btnStep3.isEnabled = FloatingWindowHelperUtils.isShowing
        binding.btnSetting.isEnabled = FloatingWindowHelperUtils.isShowing
    }

    private fun hideOverlayWindow() {
        FloatingWindowHelperUtils.hide()
        binding.btnStep2.isEnabled = !FloatingWindowHelperUtils.isShowing
        binding.btnStep3.isEnabled = FloatingWindowHelperUtils.isShowing
        binding.btnSetting.isEnabled = FloatingWindowHelperUtils.isShowing
    }
}