/* -------------------------------------------- */  
import { SoSUtility } from "./sos-utility.js";
import { SoSCardDeck } from "./sos-card-deck.js";

/* -------------------------------------------- */  
export class SoSGMDeck extends Dialog {

  /* -------------------------------------------- */
  constructor(html) {
    let conf = {
      title: 'GM Deck Dialog',
      content: html,
      buttons: {
        'flip-close': { label: 'Cancel and Close', callback: html => this.onFlipClose() }
      },
      default: 'flip'
    };
    super(conf, { classes: ["sosdialog"], width: 640  });

    let deckData = game.settings.get("foundryvtt-shadows-over-sol", "gmDeck" );    
    if ( deckData == undefined ||  deckData.deck == undefined) {
      deckData = {deck:[], discard: [], cardEdge:[] };
    }

    this.name = "GM Deck";
    this.GMdeck   = new SoSCardDeck();
    this.GMdeck.initCardDeck( this, deckData );
  }

  /* -------------------------------------------- */
  onFlipClose() {
    this.close();
  }

  /* -------------------------------------------- */
  saveDeck( ) {
    let deck = { 
      deck: duplicate(this.GMdeck.data.deck), 
      discard: duplicate(this.GMdeck.data.discard),
      cardEdge: duplicate(this.GMdeck.data.cardEdge) 
    }
    game.settings.set("foundryvtt-shadows-over-sol", "gmDeck", deck );    
  }

  /* -------------------------------------------- */
  async updateFlip( deckData) {
    $('.view-deck').remove();
    $("#view-gm-deck").append(await this.GMdeck.getDeckHTML());

    $('.view-edge').remove();
    $("#view-gm-edge").append(await this.GMdeck.getEdgeHTMLForFlip());

    //this.flipData.GMdeck.drawFromDeck();

  }

  /* -------------------------------------------- */
  async drawCard() {
    let card = this.GMdeck.drawFromDeck();
    let cardPath =  `systems/foundryvtt-shadows-over-sol/img/cards/${card.cardName}.webp`;
    let cardData = { card: card, cardPath: cardPath };
    let html = await renderTemplate('systems/foundryvtt-shadows-over-sol/templates/chat-card.html', cardData );
    ChatMessage.create( { content: html, whisper: [ ChatMessage.getWhisperRecipients("GM") ] });  
    //dialog.onFlipClose();
  }

  /* -------------------------------------------- */
  resetDeckFull( ) {
    this.GMdeck.shuffleDeck();
    this.GMdeck.drawEdge( 3 );
    this.saveDeck();
  }
  /* -------------------------------------------- */
  drawNewEdge( ) {  
    this.GMdeck.drawEdge( 1 );
    this.saveDeck();
  }

  /* -------------------------------------------- */
  discardEdge( cardName ) {  
    this.GMdeck.discardEdge( cardName  );
    this.saveDeck();
  }
  
  /* -------------------------------------------- */
  resetDeck( ) {
    this.GMdeck.resetDeck();
    this.saveDeck();
  }

  /* -------------------------------------------- */
  activateListeners(html) {
    super.activateListeners(html);

    this.bringToTop();

    var dialog = this;
      
    function onLoad() {
      dialog.updateFlip();
    }
  
    // Setup everything onload
    $(function () { onLoad(); });
    
    html.find('.class-view-deck').click((event) => {
      this.drawCard();
    });
    html.find('.reset-deck-full').click((event) => {
      this.resetDeckFull();
      this.render(true);
    });
    html.find('.draw-new-edge').click((event) => {
      this.drawNewEdge();
      this.render(true);
    });
    html.find('.reset-deck').click((event) => {
      this.resetDeck();
      this.render(true);
    });

  }  

}