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

interface ValuesCard {
  id: number;
  title: string;
  value: number;
}

@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, value: any){
    // TODO LOOK AT THIS
    await this.storage.set(`${game.toUpperCase()}GAME_TOKEN`, { status: status, data: 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", data:[{"id": 1, "title":"Temp_card_1", "value": 0}, {"id": 2, "title":"Temp_card_2", "value": 0}, {"id": 3, "title":"Temp_card_3", "value": 0}, {"id": 4, "title":"Temp_card_4", "value": 0}, {"id": 5, "title":"Temp_card_5", "value": 0}, {"id": 6, "title":"Temp_card_6", "value": 0}, {"id": 7, "title":"Temp_card_7", "value": 0}, {"id": 8, "title":"Temp_card_8", "value": 0}]});
    //     }
    //     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["data"];
    return token["status"];
  }
}