import { Component, OnInit } from '@angular/core';
import { VersionRevisionService } from './version-revisions.service';
import { VersionRevision, Version } from '../core/entities';

@Component({
	selector: 'app-version-revisions',
	templateUrl: 'version-revisions.component.html',
	styles: [`
		#versionRevisionsArea {
			padding-top: 70px;
		}

		@media screen and (max-width: 767px) {
			#versionRevisionsArea {
				padding-top: 20px;
			}
		}
	`]
})
export class VersionRevisionComponent implements OnInit {
	versionsList: Array<Version>;
	versionRevisionsList: Array<VersionRevision>;
	versionRevisionsBugfixList: Array<VersionRevision>;

	constructor(
		private versionRevisionService: VersionRevisionService
	) {
		this.versionsList = [];
		this.versionRevisionsList = [];
		this.versionRevisionsBugfixList = [];
	}

	ngOnInit(): void {
		this.versionRevisionService.getVersions().subscribe(versionsList => {
			this.versionsList = versionsList;
			this.showVersionRevision(this.versionsList[0]);
		});
	}

	showVersionRevision(version: Version): void {
		this.versionsList.forEach(versionTmp => versionTmp.active = false);
		version.active = true;

		this.versionRevisionService.findByVersionNumber(version.number).subscribe(versionRevisionsList => {
			this.versionRevisionsList = versionRevisionsList.filter(vr => !vr.bugfix);
			this.versionRevisionsBugfixList = versionRevisionsList.filter(vr => vr.bugfix);
		});
	}
}
