import { Component, Input, OnChanges, ViewEncapsulation } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { IScheduleRouteParams } from '@schedule/services/url/types';
import { ISearchHistoryItem } from '@shared/services/history/search-history.service';

@Component({
    selector: 'schedule-history-item',
    templateUrl: './schedule-history-item.component.html',
    encapsulation: ViewEncapsulation.None,
})
export class ScheduleHistoryItemComponent implements OnChanges {
    @Input() item: ISearchHistoryItem;

    title: string;

    constructor(private translate: TranslateService) {}

    ngOnChanges() {
        this.title = this.getTitle();
    }

    get params() {
        return this.item.params as IScheduleRouteParams;
    }

    get arrival() {
        return this.params.outbound.arrival;
    }

    get departure() {
        return this.params.outbound.departure;
    }

    getTitle(): string {
        const { outbound, inbound } = this.params;

        let result = this.translate.instant('SCHEDULE.TITLE');

        if (!inbound) {
            result += ', ' + this.translate.instant('SHARED.PART_ONE_WAY');
        } else {
            result += ', ' + this.translate.instant('SHARED.THERE_AND_BACK');
        }

        if (outbound.connections === 0) {
            result += ', ' + this.translate.instant('SHARED.ONLY_DIRECT');
        }

        return result;
    }
}
