import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { CommonService } from 'src/app/hmg-common/services/common/common.service';
import { Platform } from '@ionic/angular';
import { TranslatorService } from '../../services/translator/translator.service';
import { BackButtonEventDetail } from '@ionic/core';


@Component({
    selector: 'nav-buttons',
    templateUrl: './nav-buttons.component.html',
    styleUrls: ['./nav-buttons.component.scss']
})
export class NavButtonsComponent implements OnInit {

    @Input() enableBack = true;
    @Input() enableMenu = false;
    @Input() navigate = true;
    @Output() backClick = new EventEmitter();
    @Output() menuClick = new EventEmitter();
    @Input() forwardLink: string;
    @Input() backLink: string;

    public direction: string;
    public isIOS: boolean;
    constructor(
        public cs: CommonService,
        public platform: Platform,
    ) {
    }

    ngOnInit() {
        this.platform.ready().then(() => {
            this.isIOS = this.platform.is('ios');
            this.direction = TranslatorService.getCurrentDirection();

            this.platform.backButton.subscribeWithPriority(0 , () => {
                this.backButtonClicked({});
            });

        });
    }

    public backButtonClicked(event) {
        // tslint:disable-next-line:no-console
        if (this.forwardLink) {
            if (this.navigate) {
                this.cs.navigateForward(this.forwardLink);
            }
        } else if (this.backLink) {
            this.backClick.emit([event]);
            if (this.navigate) {
                this.cs.navigateBack(this.backLink);
            }
        } else {
            this.backClick.emit([event]);
            if (this.navigate) {
                this.cs.back();
            }
        }
    }


    public onMenuClicked() {
        this.menuClick.emit();
    }

}
