class AppMetaInitializer {
    private readonly settings: object;

    constructor(settings: object) {
        this.settings = settings;
    }

    public init() {
        console.log('Init app meta');
        document.title = this.settings.app_name;
        this.setMeta('application-name', this.settings.app_name);
        this.setMeta('apple-mobile-web-app-title', this.settings.app_name);
        this.setMeta('mobile-web-app-capable', 'yes');
        this.setMeta('apple-mobile-web-app-capable', 'yes');
        this.setMeta('apple-mobile-web-app-status-bar-style', 'default');
        this.setMeta('theme-color', '#000000');
        this.setMeta('msapplication-navbutton-color', '#000000');
        this.setMeta('apple-mobile-web-app-status-bar-style', 'black-translucent');
        this.addLink('manifest', this.settings.manifest_url);

        this.addLink('icon', this.settings.app_icon192, '192x192');
        this.addLink('apple-touch-icon', this.settings.app_icon192);
        this.addLink('apple-touch-icon', this.settings.app_icon180, '180x180');
        this.addLink('apple-touch-icon', this.settings.app_icon152, '152x152');
        this.addLink('apple-touch-icon', this.settings.app_icon120, '120x120');
    }

    private setMeta(name: string, content: string) {
        let meta = document.querySelector(`meta[name="${name}"]`);
        if (!meta) {
            meta = document.createElement('meta');
            meta.setAttribute('name', name);
            document.head.appendChild(meta);
        }
        meta.setAttribute('content', content);
    }

    private addLink(rel: string, href: string, sizes?: string) {
        const link = document.createElement('link');
        link.rel = rel;
        link.href = href;
        if (sizes) link.sizes = sizes;
        document.head.appendChild(link);
    }
}

export default AppMetaInitializer;