// angular
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ActivatedRoute } from '@angular/router';

// libs
import { Subject } from 'rxjs';

// app
import { AccountCategoryService } from '../../services/account.category.service';
import { ApiRoutes } from '../../api-routes';
import { AccountDetailedModel } from '../../model/account.detailed.model';
import { MotionModel } from '../../model/motion.model';

@Component({
	templateUrl: './account.list.component.html',
	styleUrls: ['./account.list.component.scss'],
})
export class AccountListComponent {
	public accounts?: AccountDetailedModel[];
	public category = '';
	public activeAccount = '';

	reloadSubject: Subject<string[]> = new Subject<string[]>();

	constructor(
		private httpClient: HttpClient,
		private activatedRoute: ActivatedRoute,
		private accountCategoryService: AccountCategoryService,
	) {
		this.activatedRoute.params.subscribe(x => this.activeAccount = x['accountId']);
	}

	ngOnInit(): void {
		this.activatedRoute.params.subscribe(params => {
			var categoryId = params['id'];

			this.httpClient
				.get<AccountDetailedModel[]>(ApiRoutes.Accounts + '?category=' + categoryId)
				.subscribe(data => {
					this.accounts = data;
				});
			this.accountCategoryService
				.getCategory(categoryId)
				.subscribe(data => {
					this.category = data.name!;
				});
		});
	}

	handlerOnUpdate(motions: MotionModel[]) {
		this.reloadSubject.next(motions.map(x => x.accountId!));
	}
}
