const { ccclass, property } = cc._decorator;

@ccclass
export default class LocalStorage extends cc.Component {
  public static setGameStarted(value: boolean) {
    cc.sys.localStorage.setItem("SaveTheDoge - GameStarted", value);
  }

  public static getGameStarted(): boolean {
    if (cc.sys.localStorage.getItem("SaveTheDoge - GameStarted") == null) {
      cc.sys.localStorage.setItem("SaveTheDoge - GameStarted", false);
      return false;
    } else
      return JSON.parse(
        cc.sys.localStorage.getItem("SaveTheDoge - GameStarted")
      );
  }

  public static setMusic(value: boolean) {
    cc.sys.localStorage.setItem("SaveTheDoge - Music", value);
  }

  public static getMusic(): boolean {
    if (cc.sys.localStorage.getItem("SaveTheDoge - Music") == null) {
      cc.sys.localStorage.setItem("SaveTheDoge - Music", true);
      return true;
    } else
      return JSON.parse(cc.sys.localStorage.getItem("SaveTheDoge - Music"));
  }

  public static setCurrentLevel(value: number) {
    cc.sys.localStorage.setItem("SaveTheDoge - CurrentLevel", value);
  }

  public static getCurrentLevel(): number {
    if (cc.sys.localStorage.getItem("SaveTheDoge - CurrentLevel") == null) {
      cc.sys.localStorage.setItem("SaveTheDoge - CurrentLevel", 0);
      return 0;
    } else
      return JSON.parse(
        cc.sys.localStorage.getItem("SaveTheDoge - CurrentLevel")
      );
  }

  public static setHighestLevel(value: number) {
    cc.sys.localStorage.setItem("SaveTheDoge - HighestLevel", value);
  }

  public static getHighestLevel(): number {
    if (cc.sys.localStorage.getItem("SaveTheDoge - HighestLevel") == null) {
      cc.sys.localStorage.setItem("SaveTheDoge - HighestLevel", 1);
      return 1;
    } else
      return JSON.parse(
        cc.sys.localStorage.getItem("SaveTheDoge - HighestLevel")
      );
  }

  public static getAllStars(): Array<{ level: number; star: number }> {
    const stars = cc.sys.localStorage.getItem("SaveTheDoge - Stars");
    if (stars === null) {
      return [];
    } else {
      return JSON.parse(stars);
    }
  }

  public static getStarAtLevel(level: number): number {
    const allStars = this.getAllStars();

    const levelEntry = allStars.find((entry) => entry.level === level);

    if (levelEntry) {
      return levelEntry.star;
    } else {
      return 0;
    }
  }

  public static setStarAtLevel(level: number, starValue: number) {
    const allStars = this.getAllStars();
    const levelEntry = allStars.find((entry) => entry.level === level);
    if (levelEntry) {
      levelEntry.star = starValue;
    } else {
      allStars.push({ level, star: starValue });
    }
    cc.sys.localStorage.setItem(
      "SaveTheDoge - Stars",
      JSON.stringify(allStars)
    );
  }

  public static setAllCoin(value: number) {
    cc.sys.localStorage.setItem("SaveTheDoge - AllCoin", value);
  }

  public static getAllCoin(): number {
    if (cc.sys.localStorage.getItem("SaveTheDoge - AllCoin") == null) {
      cc.sys.localStorage.setItem("SaveTheDoge - AllCoin", 0);
      return 0;
    } else
      return JSON.parse(cc.sys.localStorage.getItem("SaveTheDoge - AllCoin"));
  }

  public static getAllSkins(): Array<{ skin: string; available: boolean }> {
    let skins = cc.sys.localStorage.getItem("SaveTheDoge - Skins");
    if (skins === null) {
      const defaultSkins = [
        { skin: "Dog", available: true },
        { skin: "Bee", available: true },
        { skin: "Cat", available: false },
        { skin: "Wolf", available: false },
        { skin: "Pig", available: false },
        { skin: "Cow", available: false },
        { skin: "Monkey", available: false },
        { skin: "Panda", available: false },
        { skin: "SkinnyBee", available: false },
        { skin: "Mosquito", available: false },
        { skin: "Alien", available: false },
      ];

      cc.sys.localStorage.setItem(
        "SaveTheDoge - Skins",
        JSON.stringify(defaultSkins)
      );

      skins = JSON.stringify(defaultSkins);
      return JSON.parse(skins);
    } else {
      return JSON.parse(skins);
    }
  }

  public static setSkinAvailability(skinName: string, available: boolean) {
    const allSkins = this.getAllSkins();
    const skinEntry = allSkins.find((entry) => entry.skin === skinName);
    if (skinEntry) {
      skinEntry.available = available;
    }
    cc.sys.localStorage.setItem(
      "SaveTheDoge - Skins",
      JSON.stringify(allSkins)
    );
  }

  public static setCurrentSkinPlayer(name: string) {
    cc.sys.localStorage.setItem("SaveTheDoge - CurrentSkinPlayer", name);
  }

  public static getCurrentSkinPlayer(): string {
    if (
      cc.sys.localStorage.getItem("SaveTheDoge - CurrentSkinPlayer") == null
    ) {
      cc.sys.localStorage.setItem("SaveTheDoge - CurrentSkinPlayer", "Dog");
      return "Dog";
    } else
      return cc.sys.localStorage.getItem("SaveTheDoge - CurrentSkinPlayer");
  }

  public static setCurrentSkinEnemy(name: string) {
    cc.sys.localStorage.setItem("SaveTheDoge - CurrentSkinEnemy", name);
  }

  public static getCurrentSkinEnemy(): string {
    if (cc.sys.localStorage.getItem("SaveTheDoge - CurrentSkinEnemy") == null) {
      cc.sys.localStorage.setItem("SaveTheDoge - CurrentSkinEnemy", "Bee");
      return "Bee";
    } else return cc.sys.localStorage.getItem("SaveTheDoge - CurrentSkinEnemy");
  }
}
