import { Directive, Input, OnChanges } from '@angular/core';
import { Meta, Title } from '@angular/platform-browser';
import { CanonicalService } from '../services/canonical.service';

@Directive({
    selector: 'meta-tags',
})
export class MetaTagsDirective implements OnChanges {
    @Input() title: string;
    @Input() description: string;
    @Input() canonical: 'fullURL' | 'baseURL' = 'baseURL';
    @Input() noRobots = false;

    constructor(private titleService: Title, private metaService: Meta, private canonicalService: CanonicalService) {}

    ngOnChanges() {
        this.setTags();
        this.setOpenGraphTags();
    }

    private setTags() {
        this.titleService.setTitle(this.title);
        this.metaService.updateTag({ name: 'description', content: this.description });
        //ToDo: disabled WZDEV-488
        //this.canonicalService.updateURL(this.canonical);

        this.noRobots ? this.metaService.updateTag({ name: 'robots', content: 'noindex' }) : this.metaService.removeTag('name="robots"');
    }

    private setOpenGraphTags() {
        this.metaService.updateTag({ name: 'og:title', content: this.title });
        this.metaService.updateTag({ name: 'og:description', content: this.description });
        this.metaService.updateTag({ name: 'og:url', content: this.canonicalService.getUrl() });
    }
}
