import { Injectable } from '@angular/core';
import { map } from 'rxjs/operators';
import { StorageService } from './storage.service';
import { AlertService } from './alert.service';

interface ValuesCard {
  title: string;
}

@Injectable({
  providedIn: 'root'
})
export class GameService {
  
  cardset: Array<ValuesCard> = [];

  constructor(private storage: StorageService, private alert: AlertService) {}

  // main functions for all the games
  public async checkGameStorage(game: string){

    if(await this.storage.get(`${game.toUpperCase()}GAME_TOKEN`)){
      // continue or restart
      return await this.alert.presentAlertConfirm("Game found", "Do u want to continue or restart?", "restart", "continue");
    }
    // standard create new 
    return "create";
  }

  public async removeGameStorage(game: string){
    await this.storage.remove(`${game.toUpperCase()}GAME_TOKEN`);
  }

  public async saveGameStorage(game: string, status: string, sort: string, value: any){
    // TODO LOOK AT THIS
    await this.storage.set(`${game.toUpperCase()}GAME_TOKEN`, { status: status, cards: value});
  }

  // finish when backend is completed 
  public async createGameStorage(game: string){
    // return await this.http.post(`${config.endpoint}/...`, data).pipe(
        
    //   map((data) => {

    //     if (data["Status"] == "Success") {
    //       console.log(data["Data"]);
          await this.storage.set(`${game.toUpperCase()}GAME_TOKEN`, { status: "Sorting", cards:[{"title":"Temp_card_1"}, {"title":"Temp_card_2"}, {"title":"Temp_card_3"}, {"title":"Temp_card_4"}, {"title":"Temp_card_5"}, {"title":"Temp_card_6"}, {"title":"Temp_card_7"}, {"title":"Temp_card_8"}]});
    //     }
    //     return data;
    //   }),
      
    // ).toPromise();
  }

  public async initValuesGame(){
    console.log("[GameService] Initializing Value game...");
    const token = await this.storage.get("VALUESGAME_TOKEN");
    // console.log(this.cardset["cards"]);
    this.cardset = token["cards"];
    return token["status"];
  }
}