import {ChangeDetectorRef, Component, OnInit} from '@angular/core';
import * as electron from 'electron';

@Component({
  selector: 'app-title-bar',
  templateUrl: './title-bar.component.html',
  styleUrls: ['./title-bar.component.less']
})
export class TitleBarComponent implements OnInit {

  public isMaximized: boolean = false;
  public isFocused: boolean = true;

  constructor(
    private _changeDetectorRef: ChangeDetectorRef
  ) { }

  ngOnInit() {
    this._addEvents();
  }

  public minimize() {
    electron.remote.getCurrentWindow().minimize();
  }

  public maximize() {
    electron.remote.getCurrentWindow().maximize();
  }

  public unmaximize() {
    electron.remote.getCurrentWindow().unmaximize();
  }

  public close() {
    electron.remote.getCurrentWindow().close();
  }

  private _addEvents() {
    electron.remote.app.on('browser-window-blur', () => this._onBrowserWindowBlur());
    electron.remote.app.on('browser-window-focus', () => this._onBrowserWindowFocus());
    electron.remote.getCurrentWindow().on('maximize', () => this._onMaximize());
    electron.remote.getCurrentWindow().on('unmaximize', () => this._onUnmaximize());
  }

  private _onBrowserWindowBlur() {
    this.isFocused = false;
    this._changeDetectorRef.detectChanges();
  }

  private _onBrowserWindowFocus() {
    this.isFocused = true;
    this._changeDetectorRef.detectChanges();
  }

  private _onMaximize() {
    this.isMaximized = true;
    this._changeDetectorRef.detectChanges();
  }

  private _onUnmaximize() {
    this.isMaximized = false;
    this._changeDetectorRef.detectChanges();
  }

}
