import { Component, Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { ModalController } from '@ionic/angular';
import { GameService } from '../services/game.service';

@Injectable({
  providedIn: 'root'
  })

@Component({
  selector: 'app-home',
  templateUrl: 'modalsGame.page.html',
  styleUrls: ['modalsGame.page.scss'],
})
export class ModalsGamePage {

  constructor(private router: Router, public modalController: ModalController, private game: GameService) {}

  async presentModal(game: string) {
    const modal = await this.modalController.create({
      component: ModalsGamePage,
      cssClass: 'my-custom-class',
      componentProps: {
        'gameTitle': game,
      }
    });
    return await modal.present();
  }

  dismiss() {
    // using the injected ModalController this page
    // can "dismiss" itself and optionally pass back data
    this.modalController.dismiss({
      'dismissed': true
    });
  }

  async play(game: string) {
    // check if game cookie exists
    // create cookie with data if non existing
    // continue or restart with cookie
    switch(await this.game.checkGameStorage(game)){
      case "continue":{
        // dont have to do anything cookie already exists
        break;
      }
      case "create":{
        //create game storage
        await this.game.createGameStorage(game);
        break;
      } 
      case "restart":{
        // remove and create a new cookie
        await this.game.removeGameStorage(game);
        await this.game.createGameStorage(game);
        break;
      }
      default:{
        // stay in modal do nothing
        return;
      }
    }

    // when all the steps are done and clicked on play, dismiss modal and open page
    this.dismiss();

    // initialize the corrosponding game
    switch (game) {
      case "Wellnesswheel":
        
        break;
      case "Lifeline":
      
        break;
      case "Beliefs":
      
        break;
      case "Values":
        const status = await this.game.initValuesGame();
        this.router.navigateByUrl(`/${game.toLowerCase()}${status}`);
        return;
    }

    // routing is in lowercase, set text to lower
    this.router.navigateByUrl(`/${game.toLowerCase()}`);
  }
}
