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

import { HWGuildWarHeroTeam, HWGuildWarTitanTeam, HWMember } from '@models';
import { HWDataService } from '@services';

@Component({
    selector: 'app-guildwar-teams',
    imports: [DecimalPipe, MatCardModule, MatDividerModule, MatIconModule],
    templateUrl: './guildwar-teams.component.html',
    styleUrl: './guildwar-teams.component.scss',
})
export class GuildwarTeamsComponent implements OnInit {
    private _dataService = inject(HWDataService);
    private _championsByPower: HWMember[] = [];
    public guildMembers: HWMember[] = [];
    public title = 'Members';
    public subtitle = 'guild war teams power';
    public guildAverageHeroesPower: number = 0;
    public guildAverageTitansPower: number = 0;
    public guildTotalHeroesPower: number = 0;
    public guildTotalTitansPower: number = 0;

    @Input() set members(value: HWMember[]) {
        this.guildMembers = value;
    }

    ngOnInit() {
        this.guildMembers
            .sort((a, b) => ((a.heroes.power + a.titans.power) / 2 > (b.heroes.power + b.titans.power) / 2 ? -1 : 1))
            .map((data) => {
                this._championsByPower.push(data);
                return data;
            });

        this._dataService.guildWarData$.subscribe({
            next: (guildWarData) => {
                // eslint-disable-next-line @typescript-eslint/no-explicit-any
                for (const [teamId, teamValues] of Object.entries(guildWarData.teams as Record<string, any>)) {
                    const index = this._championsByPower.findIndex((member) => member.id === teamId);
                    if (index === -1) continue;
                    let totalHeroPower = 0;
                    let totalTitanPower = 0;
                    const heroes: HWGuildWarHeroTeam[] = [];
                    const titans: HWGuildWarTitanTeam[] = [];
                    for (const unit of Object.entries(
                        teamValues.clanDefence_heroes.units as Record<string, HWGuildWarHeroTeam>,
                    )) {
                        heroes.push(unit[1]);
                        totalHeroPower += unit[1].power;
                    }
                    for (const unit of Object.entries(
                        teamValues.clanDefence_titans.units as Record<string, HWGuildWarTitanTeam>,
                    )) {
                        titans.push(unit[1]);
                        totalTitanPower += unit[1].power;
                    }
                    this._championsByPower[index].heroes = { power: totalHeroPower, teams: heroes };
                    this._championsByPower[index].titans = { power: totalTitanPower, teams: titans };
                    this.guildTotalHeroesPower += totalHeroPower;
                    this.guildTotalTitansPower += totalTitanPower;
                    this._championsByPower[index].rewards = Math.floor(
                        this._championsByPower[index].scoreGifts + this._championsByPower[index].warGifts,
                    );
                }
                this.guildAverageHeroesPower = Math.floor(this.guildTotalHeroesPower / this._championsByPower.length);
                this.guildAverageTitansPower = Math.floor(this.guildTotalTitansPower / this._championsByPower.length);
            },
            error: (err) => console.error(err),
        });
    }

    getChampionsByPower(): HWMember[] {
        return this._championsByPower;
    }
}
