﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HutongGames.PlayMaker.Actions;
using HutongGames.PlayMaker;
using Tooltip = HutongGames.PlayMaker.TooltipAttribute;

using Bayat.SaveSystem.Storage;

namespace Bayat.SaveSystem.PlayMaker
{

    [ActionCategory(SaveSystemAction.ActionCategoryName)]
    [Tooltip("Lists storage item at the location.")]
    public class SaveSystemListAction : SaveSystemSettingsAction
    {

        [Tooltip("The storage identifier to list items")]
        public FsmString identifier;
        [Tooltip("The items found in the storage.")]
        [VariableType(VariableType.String)]
        public FsmArray items;
        [Tooltip("The max amount of results to retrieve, 0 = disabled")]
        public FsmInt maxResults;
        [Tooltip("Whether list items recursively or not.")]
        public FsmBool recurse;

        public override void OnReset()
        {
            this.identifier = "hello-world";
            this.items = null;
            this.maxResults = 0;
            this.recurse = false;
        }

        public override async void Enter()
        {
            if (this.settingsPreset)
            {
                this.items.Values = await SaveSystemAPI.ListAsync(this.identifier.Value, new StorageListOptions()
                {
                    Recurse = this.recurse.Value,
                    MaxResults = this.maxResults.Value
                }, this.settingsPreset.CustomSettings);
            }
            else
            {
                this.items.Values = await SaveSystemAPI.ListAsync(this.identifier.Value, new StorageListOptions()
                {
                    Recurse = this.recurse.Value,
                    MaxResults = this.maxResults.Value
                });
            }
            Finish();
        }

    }

    [ActionCategory(SaveSystemAction.ActionCategoryName)]
    [Tooltip("Lists all storage item at the location.")]
    public class SaveSystemListAllAction : SaveSystemSettingsAction
    {

        [Tooltip("The storage identifier to list items")]
        public FsmString identifier;
        [Tooltip("The items found in the storage.")]
        [VariableType(VariableType.String)]
        public FsmArray items;

        public override void OnReset()
        {
            this.identifier = "hello-world";
            this.items = null;
        }

        public override async void Enter()
        {
            if (this.settingsPreset)
            {
                this.items.Values = await SaveSystemAPI.ListAllAsync(this.settingsPreset.CustomSettings);
            }
            else
            {
                this.items.Values = await SaveSystemAPI.ListAllAsync();
            }
            Finish();
        }

    }

}