package com.birdsounds.identify

import android.content.pm.PackageManager
import android.os.Bundle
import android.Manifest
import android.annotation.SuppressLint
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.gms.wearable.ChannelClient
import com.google.android.gms.wearable.Wearable

class MainActivity : AppCompatActivity() {
//    private lateinit var soundClassifier: SoundClassifier
    val REQUEST_PERMISSIONS = 1337


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        Wearable.getChannelClient(this.applicationContext)
            .registerChannelCallback(object : ChannelClient.ChannelCallback() {
                override fun onChannelOpened(channel: ChannelClient.Channel) {
                    super.onChannelOpened(channel)
                    Log.d("HEY", "onChannelOpened")
                }
            }
            )
        Downloader.downloadModels(this)

        requestPermissions()
        soundClassifier = SoundClassifier(this, SoundClassifier.Options())
        Location.requestLocation(this, soundClassifier)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets

        }
    }
    companion object {
        var soundClassifier: SoundClassifier? = null
//        fun getSoundClassifier(): SoundClassifier? {
//            return soundClassifier
//        }
    }

    private fun requestPermissions() {
        val perms = mutableListOf<String>()

        if (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            perms.add(Manifest.permission.ACCESS_COARSE_LOCATION)
            perms.add(Manifest.permission.ACCESS_FINE_LOCATION)
        }
        if (!perms.isEmpty()) requestPermissions(perms.toTypedArray(), REQUEST_PERMISSIONS)
    }
}