import {ICellRendererAngularComp} from 'ag-grid-angular';
import {Component, HostListener} from '@angular/core';
import {ICellRendererParams} from 'ag-grid-community';
import {NavigationService} from '../../../../service/navigation.service';
import {NodeTypeIcons} from '../../../../structures/node-types';

@Component({
    selector: 'editor-page-link-renderer',
    template: `
    <div title="Control-click to open this page" (click)="onClick($event)" *ngIf="pageId">
        <i [ngClass]="typeIcons[pageType || 'page']" style="margin-right: 5px;"></i> {{ pageTitle }}
    </div>`,
})
export class PageLinkRendererComponent implements ICellRendererAngularComp {
    public params: ICellRendererParams;
    public pageId?: string;
    public pageTitle?: string;
    public pageType?: string;
    public typeIcons = NodeTypeIcons;

    constructor(
        protected readonly nav: NavigationService,
    ) { }

    agInit(params: ICellRendererParams): void {
        this.params = params;
        this.pageId = params.value;

        // @ts-ignore
        const page = params._pagesData.find(x => x.id === this.pageId);
        if ( page ) {
            this.pageTitle = page.name;
            this.pageType = page.type;
        }
    }

    onClick(event) {
        if ( event.ctrlKey ) {
            event.stopPropagation();
            event.preventDefault();
            this.nav.requestNavigation(this.pageId);
        }
    }

    refresh(params: any): boolean {
        return false;
    }
}
