import { DatePipe } from '@angular/common';
import { Title } from '@angular/platform-browser';
import { trigger, state, style, animate, transition } from '@angular/animations';
//import { Component, DestroyRef, inject, OnInit, computed, signal, Signal } from '@angular/core';
import { Component, DestroyRef, inject, OnInit } from '@angular/core';
import { MatCardModule } from '@angular/material/card';
import { MatDividerModule } from '@angular/material/divider';
import { MatIconModule } from '@angular/material/icon';
import { MatTabsModule } from '@angular/material/tabs';

import {
    ListErrorsComponent,
    GuildCardComponent,
    GuildraidsLogComponent,
    GuildwarChampionsComponent,
    GuildwarDefenceComponent,
    GuildwarTeamsComponent,
    MembersStatisticsComponent,
} from '@components/shared';
import { Errors, HWMember } from '@models';
import { HWDataService } from '@services';

@Component({
    selector: 'app-herowars',
    imports: [
        DatePipe,
        MatCardModule,
        MatDividerModule,
        MatIconModule,
        MatTabsModule,
        ListErrorsComponent,
        GuildCardComponent,
        GuildraidsLogComponent,
        GuildwarChampionsComponent,
        GuildwarDefenceComponent,
        GuildwarTeamsComponent,
        MembersStatisticsComponent,
    ],
    templateUrl: './herowars.component.html',
    styleUrl: './herowars.component.scss',
    animations: [
        trigger('flyInOut', [
            state('in', style({ transform: 'translateY(0)' })),
            transition('void => *', [style({ transform: 'translateY(-100%)' }), animate(200)]),
            transition('* => void', [style({ transform: 'translateY(100%)' }), animate(200)]),
        ]),
    ],
})
export class HerowarsComponent implements OnInit {
    private _titleService = inject(Title);
    private _dataService = inject(HWDataService);

    public title = 'HeroWars GM Tools';
    public description = 'Tools in working progress.';
    public errors: Errors = { errors: {} };
    public destroyRef = inject(DestroyRef);
    public now = new Date();
    private _guildMembers: HWMember[] = [];

    ngOnInit() {
        this._titleService.setTitle(this.title);
        this._dataService.guildData$.subscribe({
            next: (guildData) => {
                for (const data of Object.entries(guildData.clan.members)) {
                    const member: HWMember = {} as HWMember;
                    Object.assign(member, data[1]);
                    member.champion = guildData.clan.warriors.indexOf(parseInt(member.id)) !== -1;
                    member.heroes = { power: 0, teams: [] };
                    member.titans = { power: 0, teams: [] };
                    this._guildMembers.push(member);
                }
            },
            error: (err) => console.error(err),
        });
    }

    getMembers(): HWMember[] {
        return this._guildMembers;
    }
}
