import { Component, OnInit, inject } from '@angular/core';
import { DatePipe, DecimalPipe } from '@angular/common';
import { MatCardModule } from '@angular/material/card';
import { MatDividerModule } from '@angular/material/divider';
import { MatIconModule } from '@angular/material/icon';

import { HWGuildData, HWGuildClan, HWGuildStat } from '@models';
import { HWDataService } from '@services';

@Component({
    selector: 'app-guild-card',
    imports: [DatePipe, DecimalPipe, MatCardModule, MatDividerModule, MatIconModule],
    templateUrl: './guild-card.component.html',
    styleUrl: './guild-card.component.scss',
})
export class GuildCardComponent implements OnInit {
    private _dataService = inject(HWDataService);

    public guildData: HWGuildData = {
        clan: { warriors: [] as number[] } as unknown as HWGuildClan,
        stat: {} as HWGuildStat,
        slots: {},
        membersStat: [],
        serverResetTime: 0,
        clanWarEndSeasonTime: 0,
        freeClanChangeInterval: { start: 0, end: 0 },
        giftUids: [],
    };
    private _guildClan: HWGuildClan = {} as HWGuildClan;
    private _guildStat: HWGuildStat = {} as HWGuildStat;

    ngOnInit() {
        this._dataService.guildData$.subscribe({
            next: (data) => {
                Object.assign(this._guildClan, data.clan);
                this.guildData.clan = this._guildClan;
                Object.assign(this._guildStat, data.stat);
                this.guildData.stat = this._guildStat;
                this.guildData.serverResetTime = data.serverResetTime;
                this.guildData.clanWarEndSeasonTime = data.clanWarEndSeasonTime;
                this.guildData.freeClanChangeInterval = data.freeClanChangeInterval;
                this.guildData.giftUids = data.giftUids;
            },
            error: (err) => console.error(err),
        });
    }
}
