using Android.App;
using Android.Content;
using Android.Content.PM;

namespace salesbook.Maui
{
    [Activity(
        Theme = "@style/Maui.SplashTheme",
        MainLauncher = true,
        ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode |
                               ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]

    [IntentFilter(
        [
            Shiny.ShinyPushIntents.NotificationClickAction
        ],
        Categories =
        [
            "android.intent.category.DEFAULT"
        ]
    )]
    public class MainActivity : MauiAppCompatActivity
    {
        private readonly IDictionary<int, Action<Result, Intent>> _onActivityResultSubscriber =
            new Dictionary<int, Action<Result, Intent>>();

        public void StartActivityForResult(Intent intent, Action<Result, Intent> onResultAction)
        {
            var requestCode = new Random(DateTime.Now.Millisecond).Next();
            _onActivityResultSubscriber.Add(requestCode, onResultAction);
            StartActivityForResult(intent, requestCode);
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if (_onActivityResultSubscriber.TryGetValue(requestCode, out var value))
                value(resultCode, data);

            base.OnActivityResult(requestCode, resultCode, data);
        }
    }
}