﻿using System;
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("Writes all the text directly to the storage.")]
    public class SaveSystemWriteAllTextAction : SaveSystemSettingsAction
    {

        [Tooltip("The unique identifier for this value.")]
        public FsmString identifier;
        [Tooltip("The text to write.")]
        public FsmString text;

        public override void OnReset()
        {
            this.identifier = "hello-world";
            this.text = "Hello " + SaveSystemAction.ActionCategoryName + "!";
        }

        public override void Enter()
        {
            if (this.settingsPreset)
            {
                SaveSystemAPI.WriteAllTextAsync(this.identifier.Value, this.text.Value, this.settingsPreset.CustomSettings).ContinueWith(HandleTask);
            }
            else
            {

                SaveSystemAPI.WriteAllTextAsync(this.identifier.Value, this.text.Value).ContinueWith(HandleTask);
            }
        }

    }

    [ActionCategory(SaveSystemAction.ActionCategoryName)]
    [Tooltip("Reads all the text directly from the storage.")]
    public class SaveSystemReadAllTextAction : SaveSystemSettingsAction
    {

        [Tooltip("The unique identifier for this value.")]
        public FsmString identifier;
        [Tooltip("The text readed from storage.")]
        public FsmString text;

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

        public override async void Enter()
        {
            if (this.settingsPreset)
            {
                this.text = await SaveSystemAPI.ReadAllTextAsync(this.identifier.Value, this.settingsPreset.CustomSettings);
            }
            else
            {
                this.text = await SaveSystemAPI.ReadAllTextAsync(this.identifier.Value);
            }
            Finish();
        }

    }

    [ActionCategory(SaveSystemAction.ActionCategoryName)]
    [Tooltip("Writes all the bytes directly to the storage.")]
    public class SaveSystemWriteAllBytesAction : SaveSystemSettingsAction
    {

        [Tooltip("The unique identifier for this value.")]
        public FsmString identifier;
        [Tooltip("The base64 text to write as bytes.")]
        public FsmString text;

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

        public override void Enter()
        {
            if (this.settingsPreset)
            {
                SaveSystemAPI.WriteAllBytesAsync(this.identifier.Value, Convert.FromBase64String(this.text.Value), this.settingsPreset.CustomSettings).ContinueWith(HandleTask);
            }
            else
            {

                SaveSystemAPI.WriteAllBytesAsync(this.identifier.Value, Convert.FromBase64String(this.text.Value)).ContinueWith(HandleTask);
            }
        }

    }

    [ActionCategory(SaveSystemAction.ActionCategoryName)]
    [Tooltip("Reads all the bytes directly from the storage.")]
    public class SaveSystemReadAllBytesAction : SaveSystemSettingsAction
    {

        [Tooltip("The unique identifier for this value.")]
        public FsmString identifier;
        [Tooltip("The base64 encoded text readed from storage.")]
        public FsmString base64Text;

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

        public override async void Enter()
        {
            if (this.settingsPreset)
            {
                this.base64Text = Convert.ToBase64String(await SaveSystemAPI.ReadAllBytesAsync(this.identifier.Value, this.settingsPreset.CustomSettings));
            }
            else
            {
                this.base64Text = Convert.ToBase64String(await SaveSystemAPI.ReadAllBytesAsync(this.identifier.Value));
            }
            Finish();
        }

    }

}