import { Injectable } from '@angular/core';
import { ThemeableBrowser, ThemeableBrowserOptions, ThemeableBrowserObject } from '@ionic-native/themeable-browser/ngx';
import { Platform } from '@ionic/angular';

@Injectable({
  providedIn: 'root'
})
export class HmgBrowserService {
  static options: ThemeableBrowserOptions = {
    statusbar: {
      color: '#ffffffff'
    },
    toolbar: {
      height: 44,
      color: '#f0f0f0ff'
    },
    title: {
      color: '#60686b',
      showPageTitle: true
    },
    backButton: {
      image: 'back',
      imagePressed: 'back_pressed',
      align: 'left',
      event: 'backPressed'
    },
    forwardButton: {
      image: 'forward',
      imagePressed: 'forward_pressed',
      align: 'left',
      event: 'forwardPressed'
    },
    closeButton: {
      image: 'close',
      imagePressed: 'close_pressed',
      align: 'left',
      event: 'closePressed'
    },
    /*
    customButtons: [
      {
        image: 'share',
        imagePressed: 'share_pressed',
        align: 'right',
        event: 'sharePressed'
      }
    ],
    menu: {
      image: 'menu',
      imagePressed: 'menu_pressed',
      title: 'Test',
      cancel: 'Cancel',
      align: 'right',
      items: [
        {
          event: 'helloPressed',
          label: 'Hello World!'
        },
        {
          event: 'testPressed',
          label: 'Test!'
        }
      ]
    },
    */
    backButtonCanClose: true
  };

  private browser: ThemeableBrowserObject;
  constructor(
    private themeableBrowser: ThemeableBrowser,
    private platform: Platform
  ) { }

  public registerBack(event: any) {
    this.registerEvent('backPressed', event);
  }

  public registerClose(event: any) {
    this.registerEvent('closePressed', event);
  }

  private registerEvent(eventName: string, event: any) {
    if (this.browser) {
      this.browser.on(eventName).subscribe((data: any) => {
        if (event) {
          event();
        }
      });
    }
  }

  public openURLThemable(url: string) {
    this.platform.ready().then(() => {
      this.browser = this.themeableBrowser.create(url, '_blank', HmgBrowserService.options);
    });
  }



}
