#if OLD_STORAGE
using System;
using System.Runtime.InteropServices;
using UnityEngine;

/// 古いのでMyStorageManagerを使用して下さい
public static class DataManager {

    private static readonly string defaultFilename = "SaveData";

    public static void Save(string tag, object value){
        Save(defaultFilename, tag, value);
    }
    public static void Save(string filename, string tag, object value){
        string key = string.Format("{0}?tag={1}", filename, tag);
        ES2.Save(value, key);
        PlayerPrefs.Save();
    }

    public static T LoadOrDefault<T>(string tag, T defaultValue){
        if(Exists(tag)){
            return Load<T>(tag);
        }else{
            return defaultValue;
        }
    }
    public static T Load<T>(string tag){
        return Load<T>(defaultFilename, tag);
    }
    public static T Load<T>(string filename, string tag){
        string key = string.Format("{0}?tag={1}", filename, tag);
        return ES2.Load<T>(key);
    }

    public static bool Exists(string tag){
        return Exists(defaultFilename, tag);
    }
    public static bool Exists(string filename, string tag){
        string key = string.Format("{0}?tag={1}", filename, tag);
        return ES2.Exists(key);
    }
    
    public static void Remove(string tag){
        Remove(defaultFilename, tag);
    }
    public static void Remove(string filename, string tag){
        if(!Exists(filename, tag)) return ;
        string key = string.Format("{0}?tag={1}", filename, tag);
        ES2.Delete(key);
        PlayerPrefs.Save();
    }


    public static void SaveAndBackup(string tag, string value){
        SaveAndBackup(defaultFilename, tag, value);
    }
    public static void SaveAndBackup(string filename, string tag, string value){
        string key = string.Format("{0}?tag={1}", filename, tag);
        ES2.Save(value, key);
        PlayerPrefs.SetString(key, value);
        PlayerPrefs.Save();
    }

    public static bool ExistsBackup(string tag){
        return ExistsBackup(defaultFilename, tag);
    }
    public static bool ExistsBackup(string filename, string tag){
        string key = string.Format("{0}?tag={1}", filename, tag);
        return PlayerPrefs.HasKey(key);
    }

    public static string LoadBackupOrDefault(string tag, string defaultValue){
        if(ExistsBackup(tag)){
            return LoadBackup(tag);
        }else{
            return defaultValue;
        }
    }
    public static string LoadBackup(string tag){
        return LoadBackup(defaultFilename, tag);
    }
    public static string LoadBackup(string filename, string tag){
        string key = string.Format("{0}?tag={1}", filename, tag);
        return PlayerPrefs.GetString(key);
    }

    public static void IsDiskFull(Exception ex, Func<string> getDiskFullMessage){
        const int ERROR_HANDLE_DISK_FULL = 0x27;
        const int ERROR_DISK_FULL = 0x70;

        int errorCode = Marshal.GetHRForException(ex) & 0xFFFF;
        if(errorCode == ERROR_HANDLE_DISK_FULL || errorCode == ERROR_DISK_FULL){
            NativeUtils.ShowAlert(ex.Message, getDiskFullMessage());
        }else{
            throw ex;
        }
    }
}
#endif
