// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameData extends cc.Component {

    private static _instance: GameData = null;

    public selectedLevel: number = 1;
    public score: number = 0;

    public soundEnabled: boolean = true;
    public musicEnabled: boolean = true;

    public static get instance(): GameData {
        if (!this._instance) {
            this._instance = new GameData();
            this._instance.soundEnabled = cc.sys.localStorage.getItem('soundEnabled') !== 'false';
            this._instance.musicEnabled = cc.sys.localStorage.getItem('musicEnabled') !== 'false';
        }
        return this._instance;
    }

    public setSoundEnabled(enabled: boolean) {
        this.soundEnabled = enabled;
        cc.sys.localStorage.setItem('soundEnabled', enabled.toString());
    }

    public setMusicEnabled(enabled: boolean) {
        this.musicEnabled = enabled;
        cc.sys.localStorage.setItem('musicEnabled', enabled.toString());
    }

    public setScore(score: number) {
        this.score = score;
    }

    public getScore(): number {
        return this.score;
    }

    public get enemyHealth(): number {
        return this.selectedLevel;
    }

    private levelConfig = {
        1: { shootingSpeed: 6, spawnInterval: 6, playerSpeed: 2.5 },
        2: { shootingSpeed: 5, spawnInterval: 8, playerSpeed: 3 },
        3: { shootingSpeed: 4, spawnInterval: 10, playerSpeed: 3.5 },
        4: { shootingSpeed: 3, spawnInterval: 12, playerSpeed: 4 },
    };

    public get playerShootingInterval(): number {
        return this.levelConfig[this.selectedLevel].shootingSpeed;
    }

    public get enemySpawnInterval(): number {
        return this.levelConfig[this.selectedLevel].spawnInterval;
    }

    public get enemyMoveTime(): number {
        return this.levelConfig[this.selectedLevel].playerSpeed;
    }
}
