import { Component, Inject, OnInit, ViewEncapsulation } from '@angular/core';

import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { fuseAnimations } from '@fuse/animations';
import { GameService } from 'app/modules/polyglot/api/services/game.service';
import { Game } from 'app/modules/proto/models/api/game_pb';
import { Vendor } from 'app/modules/proto/models/api/vendor_pb';

export interface SlotGameComposeData {
  vendor: Vendor;
  gameList: Game[];
}
export interface SlotGameComposeResult {
  choice: boolean;
}

@Component({
  selector: 'slot-game-compose',
  templateUrl: './slot-game-compose.component.html',
  encapsulation: ViewEncapsulation.None,
  animations: fuseAnimations,
})
export class SlotGameComposeComponent implements OnInit {
  /**
   * Constructor
   */
  constructor(
    public matDialogRef: MatDialogRef<SlotGameComposeComponent>,
    @Inject(MAT_DIALOG_DATA) public data: SlotGameComposeData,
    private __gameService: GameService,
    private _sanitizer: DomSanitizer
  ) {}

  // -----------------------------------------------------------------------------------------------------
  // @ Lifecycle hooks
  // -----------------------------------------------------------------------------------------------------

  /**
   * On init
   */
  ngOnInit(): void {
    // Create the form
  }

  // -----------------------------------------------------------------------------------------------------
  // @ Public methods
  // -----------------------------------------------------------------------------------------------------

  /**
   * Save and close
   */
  saveAndClose(): void {
    // Save the message as a draft
    this.saveAsDraft();

    // Close the dialog
    this.matDialogRef.close();
  }

  /**
   * Discard the message
   */
  discard(): void {}

  /**
   * Save the message as a draft
   */
  saveAsDraft(): void {}

  private openSlotGameWindow(url: string): void {
    window.open(
      url,
      'gamepopup',
      'width=1400, height=900, toolbar=no, menubar=no, scrollbars=no, resizable=yes'
    );
  }

  onClickSlotGame(selectSlot: Game): void {
    console.log(selectSlot);
    let r = this.__gameService
      .getGameUrl(this.data.vendor.getKey(), selectSlot.getKey())
      .then((r) => {
        console.log('success', r.getUrl());

        this.openSlotGameWindow(r.getUrl());
      })
      .catch((e) => {
        console.log('fail', e);
      });
  }

  getSlotGameName(slot: Game): string {
    const namesJson = JSON.parse(slot.getNames());

    return namesJson.ko;
  }

  getSlotGameImage(slot: Game): SafeResourceUrl {
    const imagePathUrl = this._sanitizer.bypassSecurityTrustResourceUrl(
      slot.getImage()
    );
    return !!imagePathUrl ? imagePathUrl : 'assets/images/beteran/allbet.jpg';
  }
}
