import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { annotationDefaultColorConfig, DefaultBasedColorType, File } from '@red/domain';
import { UserService } from '@users/user.service';
import { DossiersService } from '@services/dossiers/dossiers.service';
import { DefaultColorsService } from '@services/entity-services/default-colors.service';
import { Observable } from 'rxjs';
import { DictionariesMapService } from '@services/entity-services/dictionaries-map.service';
import { map } from 'rxjs/operators';

@Component({
    selector: 'redaction-file-workload',
    templateUrl: './file-workload.component.html',
    styleUrls: ['./file-workload.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class FileWorkloadComponent implements OnInit {
    @Input() file: File;

    suggestionColor$: Observable<string>;
    imageColor$: Observable<string>;
    updatedColor$: Observable<string>;
    analysisColor$: Observable<string>;
    hintColor$: Observable<string>;
    redactionColor$: Observable<string>;

    #dossierTemplateId: string;

    constructor(
        private readonly _userService: UserService,
        private readonly _defaultColorsService: DefaultColorsService,
        private readonly _dossiersService: DossiersService,
        private readonly _dictionariesMapService: DictionariesMapService,
    ) {}

    ngOnInit() {
        this.#dossierTemplateId = this._dossiersService.find(this.file.dossierId).dossierTemplateId;
        this.suggestionColor$ = this.#getDefaultColor$('suggestion');
        this.updatedColor$ = this.#getDefaultColor$('updated');
        this.analysisColor$ = this.#getDefaultColor$('analysis');
        this.hintColor$ = this.#getDefaultColor$('hint');
        this.redactionColor$ = this.#getDefaultColor$('redaction');
        this.imageColor$ = this._dictionariesMapService.watch$(this.#dossierTemplateId, 'image').pipe(map(e => e.hexColor));
    }

    #getDefaultColor$(type: DefaultBasedColorType): Observable<string> {
        return this._defaultColorsService.getColor$(this.#dossierTemplateId, annotationDefaultColorConfig[type]);
    }

    get updated(): boolean {
        return this.file.hasUpdates && this.file.assignee === this._userService.currentUser.id && !this.file.isApproved;
    }

    get noWorkloadItems(): boolean {
        return (
            !this.updated &&
            !this.file.analysisRequired &&
            !this.file.hasRedactions &&
            !this.file.hasImages &&
            !this.file.hintsOnly &&
            !this.file.hasSuggestions &&
            !this.file.hasAnnotationComments
        );
    }
}
