import {Component, Input, OnInit} from '@angular/core';
import {Session, User} from '../../openaireLibrary/login/utils/helper.class';
import {EnvProperties} from '../../openaireLibrary/utils/properties/env-properties';
import {ActivatedRoute} from '@angular/router';
import {EnvironmentSpecificService} from '../../openaireLibrary/utils/properties/environment-specific.service';
import {CommunitiesService} from '../../openaireLibrary/connect/communities/communities.service';
import {CommunityInfo} from '../../openaireLibrary/connect/community/communityInfo';
import {HelperFunctions} from '../../openaireLibrary/utils/HelperFunctions.class';
import {UserManagementService} from '../../openaireLibrary/services/user-management.service';
import {Title} from '@angular/platform-browser';

@Component({
  selector: 'wellcome',
  template: `
    <div
      class="image-front-topbar wellcomePage uk-background-norepeat uk-background-cover uk-background-top-center uk-section uk-padding-remove-bottom"
      uk-scrollspy="{&quot;target&quot;:&quot;[uk-scrollspy-class]&quot;,&quot;cls&quot;:&quot;uk-animation-fade&quot;,&quot;delay&quot;:false}"
      tm-header-transparent="light" tm-header-transparent-placeholder=""
      uk-height-viewport="offset-top: true">
      <div class="uk-container uk-container-large uk-section">
        <div *ngIf="showLoading" class="uk-animation-fade uk-container uk-container-large uk-section uk-margin-top  uk-width-1-1" role="alert"><span
          class="loading-gif  uk-align-center"></span></div>
        <div *ngIf="communities.length == 0 && !showLoading" class="uk-container uk-container-large uk-section uk-margin-top">
          <div class="uk-width-1-2@m uk-width-1-1@s">
            <div class="uk-h1">Build an Open Research <b>Gateway</b> for your <b>Community</b></div>
            <h4 class="uk-margin-remove-top">Turn Open Science into practice</h4>
            <div class="uk-h5 uk-margin-top">Share and link your research results.<br>
              Across organizations, across borders.<br>Customized to your needs.
            </div>
          </div>
          <div class="uk-width-1-1 uk-inline uk-margin-large-top uk-margin-large-bottom">
            <a class="uk-button portal-button" href="https://beta.connect.openaire.eu/about"> LEARN
              HOW</a>
          </div>
        </div>
        <div *ngIf="communities.length > 0 && !showLoading" class="uk-container uk-container-large uk-section-small">
          <div class="uk-container">
            <div class="uk-grid-match uk-grid-small uk-child-width-1-4@m uk-child-width-1-2@s uk-child-width-1-1@xs
                                uk-text-center uk-margin-large-top" uk-grid uk-height-match="div > div > .uk-card-media-top">
              <div *ngFor="let community of communities;">
                <div class="uk-padding-small  uk-card uk-card-small uk-card-default communityCard">
                  <div class="uk-container uk-text-small" *ngIf="community">
                    <div class="uk-card-media-top uk-padding uk-padding-remove-vertical target uk-flex uk-flex-center uk-flex-middle">
                      <a [queryParams]="{communityId: community.communityId}" routerLinkActive="router-link-active"
                         routerLink="/dashboard">
                        <div>
                          <img *ngIf="community.logoUrl != null && community.logoUrl != '' " src="{{community.logoUrl}}"
                               alt="{{(community.title)?community.title:community.shortTitle}} logo">
                          <span *ngIf="community.logoUrl == null || community.logoUrl == '' " class="uk-icon">
                                    <svg viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg" ratio="2.5"> <circle fill="none"
                                                                                                                     stroke="#000"
                                                                                                                     stroke-width="1.1"
                                                                                                                     cx="7.7"
                                                                                                                     cy="8.6"
                                                                                                                     r="3.5"></circle> <path
                                      fill="none" stroke="#000" stroke-width="1.1"
                                      d="M1,18.1 C1.7,14.6 4.4,12.1 7.6,12.1 C10.9,12.1 13.7,14.8 14.3,18.3"></path> <path fill="none"
                                                                                                                           stroke="#000"
                                                                                                                           stroke-width="1.1"
                                                                                                                           d="M11.4,4 C12.8,2.4 15.4,2.8 16.3,4.7 C17.2,6.6 15.7,8.9 13.6,8.9 C16.5,8.9 18.8,11.3 19.2,14.1"></path></svg>
                        </span>
                        </div>
                      </a>
                    </div>
                    <div class="uk-card-body uk-padding-remove-horizontal uk-padding-remove-top uk-margin-small-top target">
                      <a [queryParams]="{communityId: community.communityId}" routerLinkActive="router-link-active"
                         routerLink="/dashboard">
                        <div class="uk-text-center uk-text-bold">
                            <span class="uk-text-small uk-margin-small" *ngIf="community.title">
                                {{community.title}}
                            </span>
                          <span class="uk-text-small uk-margin-small" *ngIf="!community.title && community.shortTitle">
                                {{community.shortTitle}}
                            </span>
                        </div>
                      </a>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  `
})

export class WellcomeComponent implements OnInit {
  properties: EnvProperties = null;
  public communities: CommunityInfo[] = [];
  showLoading: boolean;
  private user: User;

  constructor(private  route: ActivatedRoute, private propertiesService: EnvironmentSpecificService, private _communitiesService: CommunitiesService,
              private title: Title,
              private userManagementService: UserManagementService) {
  }

  ngOnInit() {
    this.showLoading = true;
    this.title.setTitle('Administration Dashboard | OpenAIRE-Connect');
    this.propertiesService.loadEnvironment().then(es => {
      this.propertiesService.setEnvProperties(es);
      this.properties = this.propertiesService.envSpecific;
      HelperFunctions.scroll();
      let count = 0;
      this.userManagementService.getUserInfo().subscribe(user => {
        this.user = user;
        this._communitiesService.getCommunitiesState().subscribe(
          communities => {
            this.communities = [];
            if (!communities || communities.length === 0) {
              return;
            }
            var countCommunities = 0;
            var index_managerOfCommunity = null;
            for (var i = 0; i < communities.length; i++) {
              var com = communities[i];
              if (Session.isPortalAdministrator(this.user) || Session.isCommunityCurator(this.user)) {
                this.communities.push(communities[i]);
              } else {
                for (var manager of com.managers) {
                  if (this.user && manager == this.user.email) {
                    countCommunities++;
                    index_managerOfCommunity = i;
                    this.communities.push(communities[i]);
                    break;
                  }
                }
              }
            }
            this.showLoading = false;
          });
      });
    });
  }
}
