import { AfterContentInit , ChangeDetectionStrategy , ChangeDetectorRef , Component , ElementRef , Renderer2 , } from '@angular/core' ; import { SceneMode } from '@firestone-hs/reference-data' ; import { BehaviorSubject , combineLatest , Observable } from 'rxjs' ; import { distinctUntilChanged , takeUntil } from 'rxjs/operators' ; import { Preferences } from '../../models/preferences' ; import { OverwolfService } from '../../services/overwolf.service' ; import { PreferencesService } from '../../services/preferences.service' ; import { AppUiStoreFacadeService } from '../../services/ui-store/app-ui-store-facade.service' ; import { AbstractWidgetWrapperComponent } from './_widget-wrapper.component' ; @ Component ( { selector : 'decktracker-player-widget-wrapper' , styleUrls : [ '../../../css/component/overlays/foreground-widget.component.scss' , '../../../css/component/overlays/decktracker-player-widget-wrapper.component.scss' , ] , template : `
		<decktracker-overlay-player
			class="widget"
			*ngIf="showWidget$ | async"
			cdkDrag
			(cdkDragStarted)="startDragging()"
			(cdkDragReleased)="stopDragging()"
			(cdkDragEnded)="dragEnded($event)"
		></decktracker-overlay-player>
	` , changeDetection : ChangeDetectionStrategy . OnPush , } ) export class DecktrackerPlayerWidgetWrapperComponent extends AbstractWidgetWrapperComponent implements AfterContentInit { protected defaultPositionLeftProvider = ( gameWidth , gameHeight ) => gameWidth - 250 ; protected defaultPositionTopProvider = ( gameWidth , gameHeight ) => 10 ; protected positionUpdater = ( left , top ) => this . prefs . updateTrackerPosition ( left , top ) ; protected positionExtractor = async ( prefs ) => prefs . decktrackerPosition ; protected getRect = ( ) => this . el . nativeElement . querySelector ( '.widget' ) ?. getBoundingClientRect ( ) ; protected isWidgetVisible = ( ) => this . visible ; protected bounds = { left : - 100 , right : - 100 , top : - 50 , bottom : - 50 , } ; private visible : boolean ; showWidget$ : Observable < boolean > ; constructor ( protected readonly ow , protected readonly el , protected readonly prefs , protected readonly renderer , protected readonly store , protected readonly cdr ) { super ( ow , el , prefs , renderer , store , cdr ) ; } ngAfterContentInit ( ) { const displayFromGameModeSubject = this . ow . getMainWindow ( ) . decktrackerDisplayEventBus ; const displayFromGameMode$ = displayFromGameModeSubject . asObservable ( ) ; this . showWidget$ = combineLatest ( this . store . listen$ ( ( [ main , nav , pref ] ) => main . currentScene , ( [ main , nav , pref ] ) => true , ( [ main , nav , pref ] ) => pref . decktrackerCloseOnGameEnd ) , this . store . listenDeckState$ ( ( deckState ) => deckState ?. playerTrackerClosedByUser , ( deckState ) => deckState ?. gameStarted , ( deckState ) => deckState ?. gameEnded , ( deckState ) => deckState ?. isBattlegrounds ( ) , ( deckState ) => deckState ?. isMercenaries ( ) , ( deckState ) => deckState ?. playerDeck ?. totalCardsInZones ( ) ) , displayFromGameMode$ ) . pipe ( this . mapData ( ( [ [ currentScene , displayFromPrefs , decktrackerCloseOnGameEnd ] , [ closedByUser , gameStarted , gameEnded , isBgs , isMercs , totalCardsInZones ] , displayFromGameMode , ] ) => { if ( closedByUser || ! gameStarted || isBgs || isMercs || ! displayFromGameMode || ! displayFromPrefs ) { console . debug ( closedByUser , gameStarted , isBgs , isMercs , displayFromGameMode , displayFromPrefs ) ; return false ; } if ( ! decktrackerCloseOnGameEnd ) { console . debug ( decktrackerCloseOnGameEnd , displayFromGameMode ) ; return displayFromGameMode ; } if ( currentScene !== SceneMode . GAMEPLAY ) { console . debug ( currentScene ) ; return false ; } console . debug ( gameEnded , totalCardsInZones ) ; return ! gameEnded && totalCardsInZones > 0 ; } ) ) ; this . showWidget$ . pipe ( distinctUntilChanged ( ) , takeUntil ( this . destroyed$ ) ) . subscribe ( ( show ) => { this . visible = show ; this . reposition ( ) ; } ) ; } }