import { GAME_NAME, LEVEL, MUSIC, WORM_ACTIVE, WORM_SELECT } from "./Constans";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LocalStorage {

    public static setLevel(value: number) {
        cc.sys.localStorage.setItem(GAME_NAME + '-' + LEVEL, value);
    }

    public static setMusic(value: boolean) {
        cc.sys.localStorage.setItem(GAME_NAME + '-' + MUSIC, value);
    }

    public static setWormSelected(value: number) {
        cc.sys.localStorage.setItem(GAME_NAME + '-' + WORM_SELECT, value);
    }

    public static setWormActive(value: number) {
        let arrayStr = cc.sys.localStorage.getItem(GAME_NAME + '-' + WORM_ACTIVE);
        let array = arrayStr ? JSON.parse(arrayStr) : [0];

        if (array.indexOf(value) == -1) {
            array.push(value);
            cc.sys.localStorage.setItem(GAME_NAME + '-' + WORM_ACTIVE, JSON.stringify(array));
        }
    }

    public static getWormSelected() {
        if (cc.sys.localStorage.getItem(GAME_NAME + '-' + WORM_SELECT) == null) {
            cc.sys.localStorage.setItem(GAME_NAME + '-' + WORM_SELECT, 0);
            return 0;
        } else return JSON.parse(cc.sys.localStorage.getItem(GAME_NAME + '-' + LEVEL));
    }

    public static getWormActive() {
        let arrayStr = cc.sys.localStorage.getItem(GAME_NAME + '-' + WORM_ACTIVE);
        let array = arrayStr ? JSON.parse(arrayStr) : [0];
        return array;
    }

    public static getLevel(): number {
        if (cc.sys.localStorage.getItem(GAME_NAME + '-' + LEVEL) == null) {
            cc.sys.localStorage.setItem(GAME_NAME + '-' + LEVEL, 0);
            return 0;
        } else return JSON.parse(cc.sys.localStorage.getItem(GAME_NAME + '-' + LEVEL));
    }

    public static getMusic(): boolean {
        if (cc.sys.localStorage.getItem((GAME_NAME + '-' + MUSIC)) == null) {
            cc.sys.localStorage.setItem(GAME_NAME + '-' + MUSIC, true);
            return true;
        } else return JSON.parse(cc.sys.localStorage.getItem(GAME_NAME + '-' + MUSIC));
    }


}

