import {
    ChangeDetectionStrategy,
    Component,
    Inject,
    Input,
    ViewChild,
} from '@angular/core';
import { ViewType } from '@app/shared/enumerators/flight-request-type.enum';
import { FlightModel } from '@app/shared/models-legacy';
import { WINDOW } from '@app/shared/services';
import { RouterHandlerService } from '@app/shared/services/router-handler.service';
import { SharePanelComponent } from '@components';

@Component({
    selector: 'share-button',
    templateUrl: './share-button.component.html',
    styleUrls: ['./share-button.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ShareButtonComponent {
    @ViewChild('panel', { read: SharePanelComponent, static: true })
    panel: SharePanelComponent;
    @Input() viewType: ViewType = ViewType.Schedule;
    @Input() flight?: FlightModel;
    constructor(
        private navigate: RouterHandlerService,
        @Inject(WINDOW) private window$: Window,
    ) {}

    share($event: MouseEvent) {
        this.panel.togglePanel($event, this.getFlightLink(this.flight));
    }

    private getFlightLink(flight?: FlightModel) {
        if (!flight) return this.window$.location.href;

        return this.navigate.getDetailsAbsoluteUrl(flight, this.viewType);
    }
}
