import { BG, GAME_NAME, GEM, HINT, IS_FREE_SPIN, LEVEL, MUSIC, UNDO, 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 setBackground(value: number) {
        cc.sys.localStorage.setItem(GAME_NAME + '-' + BG, value);
    }

    public static setGem(value: number) {
        cc.sys.localStorage.setItem(GAME_NAME + '-' + GEM, value);
    }
    public static setHint(value: number) {
        cc.sys.localStorage.setItem(GAME_NAME + '-' + HINT, value);
    }
    public static setUndo(value: number) {
        cc.sys.localStorage.setItem(GAME_NAME + '-' + UNDO, value);
    }

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

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

    public static set(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(): [number] {
        let arrayStr = cc.sys.localStorage.getItem(GAME_NAME + '-' + WORM_ACTIVE);
        if (arrayStr) {
            return JSON.parse(arrayStr)
        } else {
            cc.sys.localStorage.setItem(GAME_NAME + '-' + WORM_ACTIVE, JSON.stringify([0]))
            return [0];
        }
    }

    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 getGem(): number {
        if (cc.sys.localStorage.getItem(GAME_NAME + '-' + GEM) == null) {
            cc.sys.localStorage.setItem(GAME_NAME + '-' + GEM, 0);
            return 0;
        } else return JSON.parse(cc.sys.localStorage.getItem(GAME_NAME + '-' + GEM));
    }

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

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

    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));
    }

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


}

