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

namespace Bayat.SaveSystem.PlayMaker
{

    [ActionCategory(SaveSystemAction.ActionCategoryName)]
    [Tooltip("Checks whether the storage item exists or not.")]
    public class SaveSystemExistsAction : SaveSystemSettingsAction
    {

        [Tooltip("The storage identifier to check for")]
        public FsmString identifier;
        [Tooltip("Whether the file exists. This is set after the action runs.")]
        public FsmBool exists;

        [Tooltip("This event is triggered if the storage itme exists.")]
        public FsmEvent existsEvent;
        [Tooltip("This event is triggered if the storage item does not exist.")]
        public FsmEvent doesNotExistEvent;

        public override void OnReset()
        {
            this.identifier = "hello-world";
            this.exists = false;
            this.existsEvent = null;
            this.doesNotExistEvent = null;
        }

        public override async void Enter()
        {
            this.exists.Value = false;
            if (this.settingsPreset)
            {
                this.exists.Value = await SaveSystemAPI.ExistsAsync(this.identifier.Value, this.settingsPreset.CustomSettings);
            }
            else
            {
                this.exists.Value = await SaveSystemAPI.ExistsAsync(this.identifier.Value);
            }
            if (this.exists.Value && this.existsEvent != null)
            {
                Fsm.Event(this.existsEvent);
            }
            else if (this.doesNotExistEvent != null)
            {
                Fsm.Event(this.doesNotExistEvent);
            }
            Finish();
        }

    }

}