import { sys } from "cc";
import { constants } from "../data/constants";


export const localStorageManagerKey = {
    musicVolume: "musicVolume",
    gameData: "gameData",
};

export class localStorageManager {

    private static _instance: localStorageManager;
    private constructor() { };
    public static get instance(): localStorageManager {
        if (!this._instance) {
            this._instance = new localStorageManager();
        }
        return this._instance;
    };

    getKey(key: string): string {
        return constants.gameName + "_" + key;
    }

    getMusicVolume() {
        let key = this.getKey(localStorageManagerKey.musicVolume);
        let tmp = sys.localStorage.getItem(key);
        let val = 1;
        if (tmp != undefined) {
            val = parseFloat(tmp);
        }

        return val;
    }
    setMusicVolume(val: number) {
        let key = this.getKey(localStorageManagerKey.musicVolume);
        sys.localStorage.setItem(key, val.toFixed(2));
    }

    getGameData() {
        let key = this.getKey(localStorageManagerKey.gameData);
        let data = sys.localStorage.getItem(key);
        if (data) {
            data = JSON.parse(data);
            return data;
        }

        return {};
    }
    setGameData(data) {
        let key = this.getKey(localStorageManagerKey.gameData);
        sys.localStorage.setItem(key, JSON.stringify(data));
    }

}

