import {Component, OnInit} from "@angular/core";
import {HelpContentService} from "../../services/help-content.service";
import {CommunityStatistics} from "../../domain/statistics-classes";
import {ActivatedRoute} from "@angular/router";
import {EnvProperties} from "../../openaireLibrary/utils/properties/env-properties";
import { Community } from '../../domain/community';

@Component({
    selector: 'stats',
    templateUrl: 'stats.component.html'
})

export class StatsComponent implements OnInit {
    errorMessage: string;
    loadingMessage: string;

    communities: Community[] = [];
    selectedCommunityPid: string;

    stats: CommunityStatistics = null;
    tableNames: string[] = [];

    public properties: EnvProperties = null;

    constructor(private contentService: HelpContentService,
                private route: ActivatedRoute) {}

    ngOnInit() {
        this.route.data
            .subscribe((data: { envSpecific: EnvProperties }) => {
                this.properties = data.envSpecific;
                this.getCommunities();
                },
                error => console.log(`E R R O R!!`)
            );
    }

    getCommunities() {
        this.loadingMessage = 'Retrieving communities';
        this.contentService.getCommunities(this.properties.adminToolsAPIURL).subscribe(
            /*        this.contentService.getCommunities('http://duffy.di.uoa.gr:8080/uoa-admin-tools/').subscribe(*/
            comms => {
                this.communities = comms;
            },
            error => {
                this.loadingMessage = '';
                this.errorMessage = 'Failed to retrieve information on your communities!';
                console.log(error);
            },
            () => {
                console.log(`I have communities`);
                this.loadingMessage = '';
                this.selectedCommunityPid = this.communities[0].pid;
                this.getStatistics();
            }
        );
    }

    getStatistics() {
        this.loadingMessage = 'Retrieving statistics tables';
        this.errorMessage = '';
        this.stats = null;
        this.tableNames = [];
        this.contentService.getStatistics(this.selectedCommunityPid,this.properties.adminToolsAPIURL).subscribe(
            stats => this.stats = stats,
            error => {
                this.loadingMessage = '';
                this.errorMessage = 'Failed to retrieve statistics tables for the chosen community!';
                console.log(error);
            },
            () => {
                console.log(`I have statistics!`);
                this.loadingMessage = '';
                for (let key in this.stats.statistics){
                    this.tableNames.push(key);
                }
            }
        );
    }

    getStatsOfCommunity(pid: string) {
        this.selectedCommunityPid = pid;
        this.getStatistics();
    }

    toggleShow(name: string) {
        this.stats.statistics[name].show = !this.stats.statistics[name].show;
    }

    toggleShowInDashboard(name: string) {
        this.stats.statistics[name].showInDashboard = !this.stats.statistics[name].showInDashboard;
    }
}
