
import * as React from 'react';
import { injectable,inject} from 'inversify';
import { IConnectorConfig, CatalogoServer, IStepConnectorForm } from '../common/catalogo-protocol';
import { CatalogoService } from './catalogo-service';
import { FrontendApplicationContribution,FrontendApplication, } from '@theia/core/lib/browser';
import { CommonRequestConfig } from '@quarks/gluon-form/lib/typings';

import Panel from './layout/panel/catalogo-panel';
import { User } from '@quarks/auth-extension/lib/common';
import { AuthService } from '@quarks/auth-extension/lib/browser/auth-service';

import { MessageService } from '@theia/core/lib/common/message-service';
import { Message, MessageType } from '@theia/core/lib/common/message-service-protocol';


@injectable()
export class CatalogoServiceImpl implements CatalogoService, FrontendApplicationContribution{

    protected _connectors: IConnectorConfig[];
    private user: User = null;
    
    constructor(
        @inject(CatalogoServer) protected readonly server: CatalogoServer,
        @inject(AuthService) protected readonly auth: AuthService,
        @inject(MessageService) protected readonly message: MessageService
    ){}

    getUser(): User {
        return this.user;
    }

    logMessage(message: Message) {
        switch(message.type) {
            case MessageType.Info:
                this.message.info(message.text);
                break;
            case MessageType.Error:
                this.message.error(message.text);
                break;
            case MessageType.Log:
                this.message.log(message.text);
                break;
            case MessageType.Warning:
                this.message.warn(message.text);
                break;
        }
    }

    getConnectors(): IConnectorConfig[] {
        return this._connectors;
    }

    getConnector(name: string): IConnectorConfig {
        return this._connectors.filter(conn => conn.title === name)[0];
    }

    async reloadConnectors(): Promise<IConnectorConfig[]> {
        this._connectors = await this.server.fetchConnectors();
        return this._connectors;
    }

    getConnectorPanelConfiguration(): any {
        const props = {
            name: 'Edit Node',
            tabs: ['Properties'],
            onCancel: () => console.log('Canceled'),
            onDelete: () => console.log('Deleted'),
            onDone: () => console.log('Done')
        }
        return <Panel content='Panel' props={props} />
    }

    getConnectorForm(type: string): IStepConnectorForm[] {
        const conn = this._connectors.filter(cnn => cnn.type === type)[0];

        try {
            if(conn.form !== '') {
                const steps = JSON.parse(conn.form);
                return steps;
            } else {
                return [];
            }
        } catch(e) {
            console.error(e);
            return null;
        }
    }

    async commonRequest(request: CommonRequestConfig): Promise<any> {
        return await this.server.commonRequest(request, this.user);
    }

    async onStart(app: FrontendApplication):Promise<void> {
        this.user = this.auth.getUserInfo();
        this.server.initVoyagerSDK(this.user.token);
        this._connectors = await this.server.fetchConnectors();
    }
}