import { ChangeDetectionStrategy, Component, ElementRef, inject, OnDestroy, OnInit, signal } from '@angular/core';

import { Subscription } from 'rxjs';
import { first } from 'rxjs/operators';

import { CONFIG_TOKEN } from '@eui/core';
import { ElementLifeCycleService, ElementSetupService } from '@csdr/integration/element';

type StatusType = 'Loading' | 'Loaded' | 'Failed';

@Component({
    changeDetection: ChangeDetectionStrategy.OnPush,
    template: `
        @if (status() === 'Loaded') {
            <router-outlet></router-outlet>
        }
        @if (status() === 'Loading') {
            <eui-block-content [isBlocked]="true">
                <div style="height: 100%"></div>
            </eui-block-content>
        }
        @if (status() === 'Failed') {
            <div class="error-container">
                <div>
                    <h1>Translations or user data for this module failed to load.</h1>
                    <p>Do you want to try again?</p>
                    <button euiButton euiPrimary (click)="loadData()">
                        <span euiLabel>{{ 'global.RELOAD' | translate }}</span>
                    </button>
                </div>
            </div>
        }`,
    styles: [`
        .error-container {
            width: 50vw;
            height: 50vh;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }

        .error-container div {
            text-align: center
        }
    `],
})
export class ModuleComponent implements OnInit, OnDestroy {
    status = signal<StatusType | undefined>(undefined);

    private initSubscription: Subscription | undefined = undefined;

    private elRef = inject(ElementRef);
    private config = inject(CONFIG_TOKEN);
    private elementLifeCycleService = inject(ElementLifeCycleService);
    private elementSetupService = inject(ElementSetupService);

    ngOnInit() {
        const elementData = this.elementLifeCycleService.getElementData(this.elRef);
        this.elementLifeCycleService.setup(elementData);
        this.loadData();
    }

    ngOnDestroy(): void {
        this.initSubscription?.unsubscribe();
        this.elementLifeCycleService.cleanup();
    }

    loadData() {
        this.status.set('Loading');
        try {
            this.initSubscription?.unsubscribe();

            this.initSubscription = this.elementSetupService
                .init()
                .pipe(first())
                .subscribe((loadStatus: { success: boolean; error?: string }) => {
                    if (!loadStatus.success) {
                        this.status.set('Failed');
                        console.error(`[ELEMENT: ${this.config.global.elementName}] `, loadStatus.error);
                    } else {
                        this.status.set('Loaded');
                    }
                });
        } catch (e) {
            console.log(e);
            this.status.set('Failed');
        }
    }
}
