import {Component, TemplateRef, ViewChild} from '@angular/core';
import {Observable} from "rxjs";
import {select, Store} from "@ngrx/store";
import {fromMatrixViewPage, selectFilteredFollowingsWithLists, selectLists} from "../../shared/state/store";
import {Account, List} from 'projects/mastodon-api/src/public-api';

@Component({
    selector: 'app-matrix',
    templateUrl: './matrix.component.html',
    styleUrls: ['./matrix.component.scss']
})
export class MatrixComponent {
    @ViewChild('dialog', {static: true}) dialog: TemplateRef<any> | undefined;
    followings$: Observable<ReadonlyArray<Account>>;
    lists$: Observable<ReadonlyArray<List>>;
    selectedAccount: Account | undefined;

    constructor(private store: Store) {
        this.followings$ = this.store.pipe(select(selectFilteredFollowingsWithLists));
        this.lists$ = this.store.pipe(select(selectLists));
    }

    openMoreInfo(account: Account) {
        this.selectedAccount = account;
        // TODO
        // this.dialogService.open(this.dialog!);
    }

    isChecked(lists: List[], id: string): boolean {
        return lists.some(list => list.id === id);
    }

    onCheckedChange($event: any, accountId: string, listId: string) {
        if ($event.currentTarget.checked) {
            this.store.dispatch(fromMatrixViewPage.addAccountToList({accountId, listId}));
        } else {
            this.store.dispatch(fromMatrixViewPage.removeAccountFromList({accountId, listId}));
        }
    }
}
