import { HWGuildClan } from '@models';

export class ClanViewModel {
    clan: HWGuildClan;

    constructor(clan: HWGuildClan) {
        this.clan = clan;
    }

    getTitle(): string {
        return this.clan.title;
    }

    getServer(): string {
        return this.clan.serverId;
    }

    getMembersCount(): string {
        return this.clan.membersCount;
    }

    getCountry(): string {
        return this.clan.country;
    }

    getDescription(): string {
        return this.clan.description;
    }

    getLevel(): string {
        return this.clan.level;
    }

    getMinLevel(): string {
        return this.clan.minLevel;
    }

    getTopActivity(): number {
        return +this.clan.topActivity;
    }

    getTopDungeon(): number {
        return +this.clan.topDungeon;
    }

    getDisbanding(): boolean {
        return this.clan.disbanding;
    }

    getFrameId(): number {
        return this.clan.frameId;
    }

    getOwnerId(): string {
        return this.clan.ownerId;
    }
}

export class ClansViewModel {
    clans: ClanViewModel[];

    constructor(clans: HWGuildClan[]) {
        this.clans = clans.map(clan => new ClanViewModel(clan));
    }

    getClans(): ClanViewModel[] {
        return this.clans;
    }

    getClanCount(): number {
        return this.clans.length;
    }

    getClanByTitle(title: string): ClanViewModel | undefined {
        return this.clans.find(clan => clan.getTitle() === title);
    }

    getClansByCountry(country: string): ClanViewModel[] {
        return this.clans.filter(clan => clan.getCountry() === country);
    }

    getClansByServer(server: string): ClanViewModel[] {
        return this.clans.filter(clan => clan.getServer() === server);
    }

    getClansByLevel(level: string): ClanViewModel[] {
        return this.clans.filter(clan => clan.getLevel() === level);
    }

    getDisbandingClans(): ClanViewModel[] {
        return this.clans.filter(clan => clan.getDisbanding());
    }

    getActiveDisbands(): ClanViewModel[] {
        return this.clans.filter(clan => !clan.getDisbanding());
    }

    getClansByOwnerId(ownerId: string): ClanViewModel[] {
        return this.clans.filter(clan => clan.getOwnerId() === ownerId);
    }

    getClansByMinLevel(minLevel: string): ClanViewModel[] {
        return this.clans.filter(clan => clan.getMinLevel() === minLevel);
    }

    getAverageTopActivity(): number {
        if (this.clans.length === 0) {
            return 0;
        }
        const totalActivity = this.clans.reduce((sum, clan) => {
            const activity = clan.getTopActivity() || 0;
            return sum + activity;
        }, 0);
        return totalActivity / this.clans.length;
    }

    getAverageTopDungeon(): number {
        if (this.clans.length === 0) {
            return 0;
        }
        const totalDungeon = this.clans.reduce((sum, clan) => {
            const dungeon = clan.getTopDungeon() || 0;
            return sum + dungeon;
        }, 0);
        return totalDungeon / this.clans.length;
    }

    getClansByCountryAndServer(country: string, server: string): ClanViewModel[] {
        return this.clans.filter(clan => clan.getCountry() === country && clan.getServer() === server);
    }
}
