import { Component, Input, OnInit } 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 { FortificationCardContentComponent } from '@components/shared';
//import { HWMember } from '@models';
import { HWGuildWarFortification, HWGuildWarHeroTeam, HWGuildWarSlots, HWGuildWarTitanTeam, HWMember } from '@models';

/* JSON data */
import guildWarData from 'src/files-data/hw-guild-war.json';        // page Overview -> Statistics | page Guild War -> Guild War
import guildWarSlots from 'src/files-data/hw-guild-war-slots.json'; // no pages

@Component({
    selector: 'app-guildwar-defence',
    standalone: true,
    imports: [
        MatCardModule, MatDividerModule, MatIconModule,
        FortificationCardContentComponent
    ],
    templateUrl: './guildwar-defence.component.html',
    styleUrl: './guildwar-defence.component.scss'
})
export class GuildwarDefenceComponent implements OnInit {
    private _championsByPower: HWMember[] = [];
    private _championSlots: number[] = [];
    public guildMembers: HWMember[] = [];
    public teamType: string = '';
    public title = 'Fortifications';
    public subtitle = 'Guild War Defence';

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

    @Input() set type(value: string) {
        this.teamType = value;
    }

    ngOnInit() {
        for (const [teamId, teamValues] of Object.entries(guildWarData.teams)) {
            const index = this.guildMembers.findIndex(member => member.id === teamId);
            let totalHeroPower = 0;
            let totalTitanPower = 0;
            const heroes: HWGuildWarHeroTeam[] = [];
            const titans: HWGuildWarTitanTeam[] = [];
            this.guildMembers[index].heroes = { power: totalHeroPower, teams: heroes };
            this.guildMembers[index].titans = { power: totalTitanPower, teams: titans };

            for (const unit of Object.entries(teamValues.clanDefence_heroes.units)) {
                heroes.push(unit[1]);
                totalHeroPower += unit[1].power;
            }
            for (const unit of Object.entries(teamValues.clanDefence_titans.units)) {
                titans.push(unit[1]);
                totalTitanPower += unit[1].power;
            }
            this.guildMembers[index].heroes = { power: totalHeroPower, teams: heroes };
            this.guildMembers[index].titans = { power: totalTitanPower, teams: titans };
        }
        for (const data of Object.entries(guildWarData.slots)) {
            this._championSlots[parseInt(data[0])] = data[1];
        }
        switch (this.teamType) {
            case 'heroes':
                this.title = 'Heroes Fortifications';
                this.guildMembers.sort((a, b) => (a.heroes.power > b.heroes.power ? -1 : 1)); // Descending
                break;
            case 'titans':
                this.title = 'Titans Fortifications';
                this.guildMembers.sort((a, b) => (a.titans.power > b.titans.power ? -1 : 1)); // Descending
                break;
            default:
                break;
        }
        this._championsByPower = this.guildMembers.filter(member => member.champion === true);
    }

    private _getFortification(indexes: number[], type: string, name: string): HWGuildWarFortification {
        const teams: HWMember[] = [];
        const slots: HWGuildWarSlots = guildWarSlots.slots;
        let count = 0;
        indexes.forEach((data) => {
            switch (type) {
                case 'heroes':
                    //console.log(name, data, this._championsByPower[data]);
                    teams.push(this._championsByPower[data]);
                    count += this._championsByPower[data].heroes.power;
                    break;
                case 'titans':
                    teams.push(this._championsByPower[data]);
                    count += this._championsByPower[data].titans.power;
                    break;
                default:
                    break;
            }
        });
        return {
            name: name,
            type: type,
            count: count,
            teams: teams,
            positions: this._championSlots,
            slots: slots[name]
        };
    }

    /*
    --------------------------------
    0 - 3 - 6
    1 - 4 - 7
    2 - 5 - 8
    --------------------------------
    9 - 10 - 11 - 12
    --------------------------------
    13 - 14 - 15 - 16 - 17 - 18 - 19
    --------------------------------
    Mage Academy    1-2-31
    Lighthouse      3-4-32
    Barracks        5-6-33
    Foundry         13-14-15-36
    Citadel         25-26-28-28-29-30-40
    */

    getLighthouse(): HWGuildWarFortification {
        const indexes: number[] = [0, 4, 8];
        return this._getFortification(indexes, 'heroes', 'Lighthouse');
    }

    getBarracks(): HWGuildWarFortification {
        const indexes: number[] = [1, 5, 6];
        return this._getFortification(indexes, 'heroes', 'Barracks');
    }

    getMageAcademy(): HWGuildWarFortification {
        const indexes: number[] = [2, 3, 7];
        return this._getFortification(indexes, 'heroes', 'Mage Academy');
    }

    getFoundry(): HWGuildWarFortification {
        const indexes: number[] = [9, 11, 12, 13];
        return this._getFortification(indexes, 'heroes', 'Foundry');
    }

    getCitadel(): HWGuildWarFortification {
        const indexes: number[] = [10, 14, 15, 16, 17, 18, 19];
        return this._getFortification(indexes, 'heroes', 'Citadel');
    }

    /*
    --------------------------------
    0 - 1 - 2 - 3
    --------------------------------
    4 - 8 - 12 - 16
    5 - 9 - 13 - 17
    6 - 10 - 14 - 18
    7 - 11 - 15 - 19
    --------------------------------
    Bridge              7-8-9-34
    Spring of Elements  10-11-12-35
    Gates of Nature     16-17-18-37
    Bastion of Fire     19-20-21-38
    Bastion of Ice      22-23-24-39
    */

    getBridge(): HWGuildWarFortification {
        const indexes: number[] = [0, 1, 2, 3];
        return this._getFortification(indexes, 'titans', 'Bridge');
    }

    getGatesOfNature(): HWGuildWarFortification {
        const indexes: number[] = [5, 10, 15, 16];
        return this._getFortification(indexes, 'titans', 'Gates Of Nature');
    }

    getBastionOfFire(): HWGuildWarFortification {
        const indexes: number[] = [6, 11, 12, 17];
        return this._getFortification(indexes, 'titans', 'Bastion Of Fire');
    }

    getBastionOfIce(): HWGuildWarFortification {
        const indexes: number[] = [4, 9, 14, 19];
        return this._getFortification(indexes, 'titans', 'Bastion Of Ice');
    }

    getSpringOfElements(): HWGuildWarFortification {
        const indexes: number[] = [7, 8, 13, 18];
        return this._getFortification(indexes, 'titans', 'Spring Of Elements');
    }
}
