package com.adealink.weparty.location

import android.annotation.SuppressLint
import androidx.fragment.app.FragmentActivity
import com.adealink.frame.data.PermissionRlt
import com.adealink.frame.locale.location.config.ILocationConfig
import com.adealink.weparty.location.datasource.local.LocationLocalService
import com.adealink.weparty.permission.PermissionUtils

class LocationConfig : ILocationConfig {

    override fun getUserCountryCode(): String {
        return LocationLocalService.countryCode
    }

    override fun hasPermission(
        activity: FragmentActivity,
        permission: String
    ): Boolean {
        return PermissionUtils.hasPermissions(activity, permission)
    }

    @SuppressLint("CheckResult")
    override fun requestPermissions(
        activity: FragmentActivity,
        vararg permissions: String,
        callback: (PermissionRlt) -> Unit
    ) {
        PermissionUtils.getRxPermissions(activity)
            .request(*permissions).subscribe { granted ->
                if (granted) {
                    callback(PermissionRlt.Granted)
                } else {
                    callback(PermissionRlt.Denied)
                }
            }
    }

}