using System;
using System.Runtime.InteropServices;
using SGModule.Common.Helper;

public static class Keychain {
    public static bool Save(string key, string value) {
#if UNITY_IOS
        return SaveToKeychain(key, value);
#endif
        Log.Common.Warning("Keychain is only supported on iOS.");
        return false;
    }

    public static string Get(string key) {
#if UNITY_IOS
        var resultPtr = GetFromKeychain(key);
        if (resultPtr != IntPtr.Zero) {
            var result = Marshal.PtrToStringAuto(resultPtr);
            return result;
        }

        return null;
#endif
        Log.Common.Warning("Keychain is only supported on iOS.");
        return null;
    }

    public static bool Delete(string key) {
#if UNITY_IOS
        return DeleteFromKeychain(key);
#endif
        Log.Common.Warning("Keychain is only supported on iOS.");
        return false;
    }
#if UNITY_IOS

    [DllImport("__Internal")]
    private static extern bool SaveToKeychain(string key, string value);

    [DllImport("__Internal")]
    private static extern IntPtr GetFromKeychain(string key);

    [DllImport("__Internal")]
    private static extern bool DeleteFromKeychain(string key);
#endif
}
