namespace MajacloudSdk.Editor
{
    /// <summary>The demo scenes, in the order they appear on the Home scene and in Build Settings.</summary>
    internal static class DemoScenes
    {
        public const string Home = "00 Home";
        public const string AllInOne = "01 All-in-One";
        public const string SignIn = "02 Sign-in";
        public const string Leaderboard = "03 Leaderboard";
        public const string CloudSave = "04 Cloud Save";
        public const string Rewards = "05 Rewards";
        public const string RemoteConfig = "06 Remote Config";
        public const string TestData = "07 Test Data";
        public const string Docs = "08 Docs";

        public sealed class Entry
        {
            public readonly string File;
            public readonly string Title;
            public readonly string Summary;
            public readonly string Icon;

            public Entry(string file, string title, string summary, string icon)
            {
                File = file;
                Title = title;
                Summary = summary;
                Icon = icon;
            }
        }

        public static readonly Entry[] Demos =
        {
            new Entry(AllInOne, "All-in-One", "Every service in one screen", "A"),
            new Entry(SignIn, "Sign-in", "Guest player, name and account linking", "S"),
            new Entry(Leaderboard, "Leaderboard", "Submit a score, see the top players", "L"),
            new Entry(CloudSave, "Cloud Save", "Save and load player progress", "C"),
            new Entry(Rewards, "Rewards", "Daily reward and coin wallet", "R"),
            new Entry(RemoteConfig, "Remote Config", "Live values from your dashboard", "V"),
            new Entry(TestData, "Test Data", "Add test data and run a quick check", "T"),
            new Entry(Docs, "Docs", "Short code examples to copy", "D"),
        };
    }

    /// <summary>Code shown in the demo scenes and the Docs scene. Keep lines under ~60 characters.</summary>
    internal static class DemoCode
    {
        public const string SignIn =
            "var player = await Majacloud.Auth.SignInAnonymouslyAsync();\n" +
            "await Majacloud.Auth.SetDisplayNameAsync(\"Maja\");\n" +
            "await Majacloud.Auth.LinkAccountAsync(); // opens browser";

        public const string Leaderboard =
            "await Majacloud.Leaderboards.SubmitScoreAsync(\"coins\", 120);\n" +
            "var top = await Majacloud.Leaderboards.GetTopAsync(\"coins\");\n" +
            "var me = await Majacloud.Leaderboards.GetMyRankAsync(\"coins\");";

        public const string CloudSave =
            "await Majacloud.CloudSave.SaveAsync(\"progress\", progress);\n" +
            "var save = await Majacloud.CloudSave\n" +
            "    .LoadAsync<PlayerProgress>(\"progress\");\n" +
            "if (save.IsNotFound) { /* first run */ }";

        public const string Rewards =
            "var claim = await Majacloud.Rewards.ClaimAsync(\"daily_coins\");\n" +
            "var wallet = await Majacloud.Rewards.GetWalletAsync();\n" +
            "Debug.Log(wallet.Value.Coins);";

        public const string RemoteConfig =
            "string hi = Majacloud.RemoteConfig.GetString(\"welcome_message\");\n" +
            "int damage = Majacloud.RemoteConfig.GetInt(\"enemy_damage\", 25);\n" +
            "bool down = Majacloud.RemoteConfig.GetBool(\"maintenance\");";

        public sealed class Section
        {
            public readonly string Title;
            public readonly string Text;
            public readonly string Code;

            public Section(string title, string text, string code)
            {
                Title = title;
                Text = text;
                Code = code;
            }
        }

        public static readonly Section[] Docs =
        {
            new Section("1. Set up",
                "In the editor open Tools › Majacloud › Setup, paste your Base URL, Project ID and Client Key, then press Test connection. The SDK starts by itself when the game runs.",
                "using MajacloudSdk;\n\n" +
                "// Settings live in Assets/Resources/MajacloudSettings.asset\n" +
                "Debug.Log(Majacloud.IsInitialized);"),
            new Section("2. Sign in",
                "Call this once when the game starts. It restores the saved player or creates a new guest.",
                "var signIn = await Majacloud.Auth.SignInAnonymouslyAsync();\n" +
                "if (signIn.Ok) Debug.Log(\"Hi \" + signIn.Value.DisplayName);"),
            new Section("3. Leaderboard",
                "Create a leaderboard in the dashboard, then use its slug.",
                Leaderboard),
            new Section("4. Cloud save",
                "Save any [Serializable] class per player. Load it on any device.",
                "[Serializable] public class Progress { public int level; }\n\n" +
                "await Majacloud.CloudSave.SaveAsync(\"progress\",\n" +
                "    new Progress { level = 3 });\n" +
                "var save = await Majacloud.CloudSave\n" +
                "    .LoadAsync<Progress>(\"progress\");"),
            new Section("5. Rewards",
                "The server decides if a reward can be claimed, so players cannot cheat the cooldown.",
                Rewards),
            new Section("6. Remote config",
                "Values are downloaded at start and cached offline. Always pass a fallback.",
                RemoteConfig),
            new Section("7. Errors",
                "Calls never throw. Check Ok, then read Value or Error.",
                "var result = await Majacloud.Rewards.ClaimAsync(\"daily_coins\");\n" +
                "if (!result.Ok) Debug.Log(result.Error.Message);"),
            new Section("8. Coroutines",
                "Not using async? Every call has a callback version.",
                "StartCoroutine(Majacloud.Leaderboards.GetTop(\"coins\", 10, r =>\n" +
                "{\n" +
                "    if (r.Ok) Debug.Log(r.Value.Entries.Count);\n" +
                "}));"),
        };
    }
}
