import { DatePipe, DecimalPipe } from '@angular/common';
import { Title } from '@angular/platform-browser';
import { trigger, state, style, animate, transition } from '@angular/animations';
import { Component, DestroyRef, inject, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatCardModule } from '@angular/material/card';
import { MatButtonModule } from '@angular/material/button';
import { MatDividerModule } from '@angular/material/divider';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatTabsModule } from '@angular/material/tabs';

import { CardContainerComponent, ListErrorsComponent } from '@components/shared';
import { CardColors, Errors, HWActivityStat, HWGuildClan, HWMember } from '@models';
import { ClanViewModel, MembersViewModel } from '@viewmodels/herowars';
import { HWClanService, HWMemberService, UtilitiesService } from '@services';
import { MAX_CLAN_MEMBERS } from '@constants';

@Component({
    selector: 'app-herowars-guildwar',
    imports: [
        DatePipe,
        DecimalPipe,
        FormsModule,
        CardContainerComponent,
        ListErrorsComponent,
        MatButtonModule,
        MatCardModule,
        MatDividerModule,
        MatFormFieldModule,
        MatIconModule,
        MatInputModule,
        MatTabsModule,
    ],
    templateUrl: './herowars-guildwar.component.html',
    styleUrl: './herowars-guildwar.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 HerowarsGuildwarComponent implements OnInit {
    private _titleService = inject(Title);
    private _utilitiesService = inject(UtilitiesService);
    private _clanService = inject(HWClanService);
    private _memberService = inject(HWMemberService);

    public destroyRef = inject(DestroyRef);
    public title = 'Guild War';
    public subtitle = 'Log';
    public description = 'Tools in working progress.';
    public colors: CardColors = { background: 'bg-raspberry', text: 'text-bg-primary' };
    public errors: Errors = { errors: {} };
    public now = new Date();
    public daysToWarn = 1;
    public maxClanMembers = MAX_CLAN_MEMBERS;
    public clanLoaded = false;
    public membersLoaded = false;
    public clanViewModel: ClanViewModel = {} as ClanViewModel;
    public membersViewModel: MembersViewModel = {} as MembersViewModel;
    private _guildClan: HWGuildClan = {} as HWGuildClan;
    private _guildMembers: HWMember[] = [];
    private _guildInfo: HWActivityStat = {} as HWActivityStat;

    ngOnInit() {
        this._titleService.setTitle(this.title);
    }

    getClanViewModel(): ClanViewModel {
        return this.clanViewModel;
    }

    getMembersViewModel(): MembersViewModel {
        return this.membersViewModel;
    }

    getColors(color = 'megna'): CardColors {
        if (color === 'accent') {
            this.colors = { background: 'bg-accent', text: 'text-navy' };
        } else if (color === 'raspberry') {
            this.colors = { background: 'bg-raspberry', text: 'text-bg-primary' };
        } else {
            this.colors = { background: 'bg-megna', text: 'text-bg-primary' };
        }
        return this.colors;
    }

    loadClan(): void {
        this._clanService.loadClan().subscribe((clan) => {
            this._guildClan = clan;
            this.clanViewModel = new ClanViewModel(this._guildClan);
            this.clanLoaded = true;
        });
    }

    loadData(): void {
        this._clanService.loadClan().subscribe((clan) => {
            this._guildClan = clan;
            this._memberService.loadMembers(this._guildClan).subscribe((members) => {
                this._guildMembers = members;
                this._guildClan = this._clanService.loadClanStats(this._guildClan, this._guildMembers);
                this.daysToWarn = parseInt(this._guildClan.daysToKick) / 2;
                this.clanViewModel = new ClanViewModel(this._guildClan);
                this.clanLoaded = true;
                this.membersViewModel = new MembersViewModel(this._guildMembers);
                this.membersLoaded = true;
            });
        });
    }

    loadMembers(): void {
        this._memberService.loadMembers(this._guildClan).subscribe((members) => {
            this._guildMembers = members;
            this.membersViewModel = new MembersViewModel(this._guildMembers);
            this._guildClan = this._clanService.loadClanStats(this._guildClan, this._guildMembers);
            this.membersLoaded = true;
        });
    }

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

    syncClan(): void {
        console.log(this._guildClan);
    }

    syncMembers(): void {
        console.log(this._guildMembers);
    }
}
