import {Component, HostBinding, Injector, OnInit, Type, ViewChild} from '@angular/core';

import {SettingsService} from './core/settings/settings.service';
import {ToastContainerDirective, ToastrService} from "ngx-toastr";
import {ngxLoadingAnimationTypes} from "ngx-loading";
import {VarService} from "./core/service/var.service";
import {DomSanitizer} from "@angular/platform-browser";
import {HttpClient} from "@angular/common/http";
import {CookieService} from "ngx-cookie-service";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {

  @HostBinding('class.layout-fixed') get isFixed() {
    return this.settings.getLayoutSetting('isFixed');
  };

  @HostBinding('class.aside-collapsed') get isCollapsed() {
    return this.settings.getLayoutSetting('isCollapsed');
  };

  @HostBinding('class.layout-boxed') get isBoxed() {
    return this.settings.getLayoutSetting('isBoxed');
  };

  @HostBinding('class.layout-fs') get useFullLayout() {
    return this.settings.getLayoutSetting('useFullLayout');
  };

  @HostBinding('class.hidden-footer') get hiddenFooter() {
    return this.settings.getLayoutSetting('hiddenFooter');
  };

  @HostBinding('class.layout-h') get horizontal() {
    return this.settings.getLayoutSetting('horizontal');
  };

  @HostBinding('class.aside-float') get isFloat() {
    return this.settings.getLayoutSetting('isFloat');
  };

  @HostBinding('class.offsidebar-open') get offsidebarOpen() {
    return this.settings.getLayoutSetting('offsidebarOpen');
  };

  @HostBinding('class.aside-toggled') get asideToggled() {
    return this.settings.getLayoutSetting('asideToggled');
  };

  @HostBinding('class.aside-collapsed-text') get isCollapsedText() {
    return this.settings.getLayoutSetting('isCollapsedText');
  };

  varService;
  loadConfig = {
    animationType: ngxLoadingAnimationTypes.circle,
    primaryColour: '#dd0031',
    secondaryColour: '#ffffff',
    backdropBorderRadius: '8px'
  }
  @ViewChild(ToastContainerDirective, {static: true}) toastContainer: ToastContainerDirective;

  constructor(public settings: SettingsService, private toastrService: ToastrService, private  injector: Injector,
              private sanitizer: DomSanitizer,private http: HttpClient,private cookie: CookieService) {
    // 设置全局变量
    this.varService = VarService;
    VarService.INJECTOR = injector;
    VarService.DOM_SANITIZER = sanitizer;
    VarService.HTTP_CLIENT = http;
    VarService.COOKIE_SERVICE = cookie;
  }

  ngOnInit() {
    this.toastrService.overlayContainer = this.toastContainer;
    VarService.TOASTR = this.toastrService;
    // prevent empty links to reload the page
    document.addEventListener('click', e => {
      const target = e.target as HTMLElement;
      if (target.tagName === 'A' && ['', '#'].indexOf(target.getAttribute('href')) > -1)
        e.preventDefault();
    });
  }
}
