package hu.app_dev.terepi_seged

import android.os.Build
import android.provider.Settings
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
    private val deviceInfoChannelName = "hu.app_dev.terepi_seged/deviceInfo"

    override fun configureFlutterEngine(flutterEngine: FlutterEngine){
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(
            flutterEngine.dartExecutor.binaryMessenger,
        deviceInfoChannelName
        ).setMethodCallHandler{call, result ->
        when(call.method){
            "getAndroidDeviceName"->{
                result.success(getAndroidDeviceName())
            }
            "getAndroidId"->{
                result.success(getAndroidId())
            }
            else -> result.notImplemented()
        }
        }
        
    }

    private fun getAndroidDeviceName(): String {
        val name = android.provider.Settings.Global.getString(
            contentResolver,
            android.provider.Settings.Global.DEVICE_NAME
        )

        if (!name.isNullOrBlank()) {
            return name
        }

        val manufacturer = android.os.Build.MANUFACTURER.orEmpty()
        val model = android.os.Build.MODEL.orEmpty()

        return "$manufacturer $model".trim()
    }

    private fun getAndroidId(): String? {
        return try {
            Settings.Secure.getString(
                contentResolver,
                Settings.Secure.ANDROID_ID
            )
        } catch (e: Exception){
            null
        }
    }

}
