﻿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("Loads the value from the storage.")]
    public class SaveSystemLoadAction : SaveSystemSettingsAction
    {

        [Tooltip("The unique identifier for this value.")]
        public FsmString identifier;
        [Tooltip("The value to save.")]
        [UIHint(UIHint.Variable)]
        public FsmVar value;
        [Tooltip("Optional: A value to return if the value does not exist.")]
        [UIHint(UIHint.Variable)]
        public FsmVar defaultValue;

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

        public override async void Enter()
        {
            this.defaultValue.UpdateValue();
            object loadedValue = null;
            if (this.settingsPreset)
            {
                if (await SaveSystemAPI.ExistsAsync(this.identifier.Value, this.settingsPreset.CustomSettings))
                {
                    loadedValue = await SaveSystemAPI.LoadAsync<object>(this.identifier.Value, this.settingsPreset.CustomSettings);
                }
            }
            else
            {
                if (await SaveSystemAPI.ExistsAsync(this.identifier.Value))
                {
                    loadedValue = await SaveSystemAPI.LoadAsync<object>(this.identifier.Value);
                }
            }
            if (this.defaultValue.GetValue() != null && !this.defaultValue.IsNone && loadedValue == null)
            {
                loadedValue = this.defaultValue.GetValue();
            }
            this.value.SetValue(loadedValue);
            Finish();
        }

    }

    [ActionCategory(SaveSystemAction.ActionCategoryName)]
    [Tooltip("Loads the value into an existing object from the storage.")]
    public class SaveSystemLoadIntoAction : SaveSystemSettingsAction
    {

        [Tooltip("The unique identifier for this value.")]
        public FsmString identifier;
        [Tooltip("The value to save.")]
        [UIHint(UIHint.Variable)]
        public FsmVar value;

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

        public override void Enter()
        {
            this.value.UpdateValue();
            if (this.value.IsNone || this.value.GetValue() == null)
            {
                HandleError("The 'Load Into' action requires an object to load the data into, but none was specified in the 'Value' field.");
            }
            else
            {
                if (this.settingsPreset)
                {
                    SaveSystemAPI.LoadIntoAsync(this.identifier.Value, this.value.GetValue(), this.settingsPreset.CustomSettings).ContinueWith(HandleTask);
                }
                else
                {
                    SaveSystemAPI.LoadIntoAsync(this.identifier.Value, this.value.GetValue()).ContinueWith(HandleTask);
                }
            }
            Finish();
        }

    }

}