import { Component, OnInit, OnDestroy, inject } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { RouterLink } from '@angular/router';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
import { MatDividerModule } from '@angular/material/divider';
import { MatIconModule } from '@angular/material/icon';

import { Subscription } from 'rxjs';

import { Errors, User } from '@models';
import { UserService } from '@services';
import { ListErrorsComponent } from '@components/shared';

@Component({
    imports: [RouterLink, MatButtonModule, MatCardModule, MatDividerModule, MatIconModule, ListErrorsComponent],
    selector: 'app-profile',
    templateUrl: './profile.component.html',
    styleUrl: './profile.component.scss',
})
export class ProfileComponent implements OnInit, OnDestroy {
    private titleService = inject(Title);
    private userService = inject(UserService);

    private _user: Subscription = new Subscription();
    title = 'Mon compte';
    bannerTitle = '';
    user: Partial<User> = {} as Partial<User>;
    errors: Errors = { errors: {} };
    btnSettingsTitle = 'Paramètres';

    ngOnInit() {
        this.titleService.setTitle(`Ad Astra - ${this.title}`);
        Object.assign(this.user, this.userService.getCurrentUser());
        if (this.user.username) {
            this.bannerTitle = `@${this.user.username}`;
        } else {
            this.bannerTitle = `${this.user.firstname} ${this.user.lastname}`;
        }
    }

    ngOnDestroy() {
        this._user.unsubscribe();
    }
}
