import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
import {ActivatedRoute, NavigationEnd, RouteConfigLoadEnd, Router} from '@angular/router';

import {EnvProperties} from './openaireLibrary/utils/properties/env-properties';
import {EnvironmentSpecificService} from './openaireLibrary/utils/properties/environment-specific.service';
import {User} from './openaireLibrary/login/utils/helper.class';
import {UserManagementService} from "./openaireLibrary/services/user-management.service";
import {StakeholderService} from "./services/stakeholder.service";
import {Subscriber} from "rxjs";
import {LayoutService} from "./library/sharedComponents/sidebar/layout.service";


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent implements OnInit, OnDestroy {
  properties: EnvProperties;
  user: User;
  loginCheck: boolean = false;
  hasSidebar: boolean = false;
  hasHeader: boolean = false;
  private subscriptions: any[] = [];

  constructor(private  route: ActivatedRoute,
              private propertiesService: EnvironmentSpecificService,
              private router: Router,
              private userManagementService: UserManagementService,
              private layoutService: LayoutService,
              private stakeholderService: StakeholderService,
              private cdr: ChangeDetectorRef) {
  }

  ngOnInit() {
    this.propertiesService.loadEnvironment()
      .then(properties => {
        this.properties = properties;
        this.router.events.forEach((event) => {
          if (event instanceof NavigationEnd) {
            let r = this.route;
            while (r.firstChild) {
              r = r.firstChild;
            }
            r.params.subscribe(params => {
              if (params['stakeholder']) {
                if (!this.stakeholderService.stakeholder ||
                  this.stakeholderService.stakeholder.alias !== params['stakeholder']) {
                  this.stakeholderService.getStakeholder(this.properties.monitorServiceAPIURL, params['stakeholder']).subscribe(stakeholder => {
                    this.stakeholderService.setStakeholder(stakeholder);
                    this.layoutService.setOpen(true);
                  });
                }
              } else {
                this.stakeholderService.setStakeholder(null);
              }
            });
          }
        });
        this.userManagementService.getUserInfo(this.properties.userInfoUrl).subscribe(user => {
          this.user = user;
          this.loginCheck = true;
        }, error => {
          console.log("App couldn't fetch properties");
          console.log(error);
        });
      });
    this.subscriptions.push(this.layoutService.hasSidebar.subscribe(hasSidebar => {
      this.hasSidebar = hasSidebar;
      this.cdr.detectChanges();
    }));
    this.subscriptions.push(this.layoutService.hasHeader.subscribe(hasHeader => {
      this.hasHeader = hasHeader;
      this.cdr.detectChanges();
    }));
  }

  public ngOnDestroy() {
    this.subscriptions.forEach(value => {
      if (value instanceof Subscriber) {
        value.unsubscribe();
      }
    });
  }

  public get open() {
    return this.layoutService.open;
  }

  public toggleOpen(event = null) {
    if (!event) {
      this.layoutService.setOpen(!this.open);
    } else if (event && event['value'] === true) {
      this.layoutService.setOpen(false);
    }
  }
}
