import { ReflectiveInjector } from "@angular/core";
import { getMetadataStorage } from "../odoo/utils/metadata-storage";

export class BaseView<T> {

    type: T = {} as T;
    model: any = {};
    injector: any = {};
    title: string;

    constructor(c: { new (): T; }, ...injectables: any[]) {
        this.type = new c();
        this.model = getMetadataStorage().models.filterByTarget(this.type.constructor).items.shift();
        this.injector = ReflectiveInjector.resolveAndCreate(injectables);
        this.title = "Sin título";
    }

    /**
     * 
     * @param injectable 
     */
    getInjectable(injectable: any): any {
        try {
            return this.injector.get(injectable);
        } catch (error) {
            return undefined;
        }
    }

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

    /**
     * 
     * @param title 
     */
    setTitle(title: string): void {
        this.title = title;
    }

    /**
     * 
     */
    getType(): T {
        return this.type;
    }

    /**
     * 
     */
    getModel(): any {
        return this.model;
    }

    /**
     * 
     */
    getModelName(): string {
        return this.getModel().name;
    }

    /**
     * 
     */
    getInjector(): any {
        return this.injector;
    }
}