import { DOCUMENT } from '@angular/common';
import { Injectable, Optional, RendererFactory2, ViewEncapsulation, Inject } from '@angular/core';

@Injectable({
    providedIn: 'root'
})
export class LinkService {

    // constructor(
    //     private rendererFactory: RendererFactory2,
    //     @Inject(DOCUMENT) private document
    // ) {
    // }

    // /**
    //  * Inject the State into the bottom of the <head>
    //  */
    // addTag(tag: LinkDefinition, forceCreation?: boolean) {

    //     try {
    //         const renderer = this.rendererFactory.createRenderer(this.document, {
    //             id: '-1',
    //             encapsulation: ViewEncapsulation.None,
    //             styles: [],
    //             data: {}
    //         });

    //         debugger;

    //         const link = renderer.createElement('link');
    //         const head = this.document.head;
    //         const selector = this._parseSelector(tag);

    //         if (head === null) {
    //             throw new Error('<head> not found within DOCUMENT.');
    //         }

    //         Object.keys(tag).forEach((prop: string) => {
    //             return renderer.setAttribute(link, prop, tag[prop]);
    //         });

    //         // [TODO]: get them to update the existing one (if it exists) ?
    //         renderer.appendChild(head, link);

    //     } catch (e: { target: { value: any; }; length: number; }) {
    //         console.error('Error within linkService : ', e);
    //     }
    // }

    // private _parseSelector(tag: LinkDefinition): string {
    //     // Possibly re-work this
    //     const attr: string = tag.rel ? 'rel' : 'hreflang';
    //     return `${attr}="${tag[attr]}"`;
    // }

    constructor(@Inject(DOCUMENT) private doc: any) { }

    createLinkForCanonicalURL(url?: string) {
        if (!url) {
            let links = this.doc.getElementsByTagName("link");
            links[links.length - 1].setAttribute('href', this.doc.URL);
        } else {
            let links = this.doc.getElementsByTagName("link");
            let linkurl = links[links.length - 1].getAttribute('href');
            if (linkurl.indexOf('localhost:4200') > -1 || linkurl.indexOf('https://credit.nsserver.in/') > -1) {
                let links = this.doc.getElementsByTagName("link");
                links[links.length - 1].setAttribute('href', this.doc.URL);
            } else {
                let link: HTMLLinkElement = this.doc.createElement('link');
                link.setAttribute('rel', 'canonical');
                this.doc.head.appendChild(link);
                link.setAttribute('href', this.doc.URL);
            }
        }
        // let links = this.doc.getElementsByTagName("link");
        // links[1].setAttribute('href', this.doc.URL);
        // let links = this.doc.getElementsByTagName("link");
        // let link: HTMLLinkElement = this.doc.createElement('link');
        // link.setAttribute('rel', 'canonical');
        // this.doc.head.appendChild(link);
        // link.setAttribute('href', this.doc.URL);
    }
}

export declare type LinkDefinition = {
    charset?: string;
    crossorigin?: string;
    href?: string;
    hreflang?: string;
    media?: string;
    rel?: string;
    rev?: string;
    sizes?: string;
    target?: string;
    type?: string;
} & {
    [prop: string]: string;
};