import { ConnectionRepository } from '../../infrastructure/repositories/connection.repository'
import { ConnectionSummary } from '../../domain/models/connections/ConnectionSummary.interface';
import { ConnectionElement } from '../../domain/models/connections/ConnectionElement.interface';
import { ConnectionResponse } from '../../domain/models/connections/ConnectionResponse.interface';
import { ConnectionDataBaseModel } from '../../domain/models/connections/ConnectionDataBaseModel.interface';
import { PreviewDatafileResponse } from '../models/previewDatafile/PreviewDatafileResponse.interface';
import { DatabaseSchemaListResponse} from '../models/connections/DatabaseSchemaListResponse.interface';
import { DatabaseTableListResponse } from '../models/connections/DatabaseTableListResponse.interface';
import { DatabaseViewListResponse } from '../models/connections/DatabaseViewListResponse.interface';
import { ColumnListResponse } from '../models/connections/ColumnListResponse.interface';
import { ConfigurationResponse } from '../models/configurations/ConfigurationResponse.interface';
import { ColumnNormalizedListResponse } from '../models/connections/ColumnNormalizedListResponse.interface'

export class ConnectionService {
    private connectionRepository: ConnectionRepository;

    constructor() {
        this.connectionRepository = new ConnectionRepository();
    }

    async getConnectionSummary(): Promise<ConnectionSummary> {
        return await this.connectionRepository.getConnectionSummary();
    }

    async deleteConnection(idConnectionSelected : String){
        return await this.connectionRepository.deleteConnection(idConnectionSelected);
    }

    getConnectionTypeFromQueryString() : String {
        let query_string_params = this.connectionRepository.getQueryStringParameters();
        let cnx_type : String;
        cnx_type = (query_string_params.type == 'directory_file' ) ? "delimited-file" : query_string_params.type;
        return cnx_type;
    }
    getConnectionType(type : String, validation : Boolean) : String{
        let cnx_type : String;
        cnx_type = "";
        switch(type){
            case "Directory File":
                cnx_type = (validation) ? "delimited-file" : "directory_file";
                break;
            case "Database":
                cnx_type = "database";
                break;
            case "Object Store":
                cnx_type = "object_store";
                break;
        }
        return cnx_type
    }

    async validateConnection(connectionSelected : ConnectionElement) : Promise<ConnectionResponse>{
        let cnx_type : String;
        cnx_type = this.getConnectionType(connectionSelected.type, true);
        return await this.connectionRepository.validateConnection(connectionSelected.id, cnx_type);
    }

    async validateNewConnection(type: String, data : any) : Promise<ConnectionResponse>{
        let cnx_type : String;
        cnx_type = this.getConnectionType(type, true);
        return await this.connectionRepository.validateNewConnection(cnx_type, data);
    }

    showEditConnection(connectionSelected : ConnectionElement){
        let cnx_type : String;
        cnx_type = this.getConnectionType(connectionSelected.type, false);
        return '/connection/' + cnx_type + '/' + connectionSelected.id;
    }

    getQueryStringParameters() : any {
        return this.connectionRepository.getQueryStringParameters();
    }

    async editConnection(idConnection : String, cnx_type : String): Promise<ConnectionDataBaseModel>{
        return await this.connectionRepository.getConnection(idConnection, cnx_type);
    }

    async getConnectionList(): Promise<ConnectionDataBaseModel>{
        return await this.connectionRepository.getConnectionList();
    }

    async getConnectionDatabaseList(): Promise<ConnectionDataBaseModel>{
        return await this.connectionRepository.getConnectionDatabaseList();
    }

    async getConnectionDatafileList(): Promise<ConnectionDataBaseModel>{
        return await this.connectionRepository.getConnectionDatafileList();
    }

    async getDatabaseDriversList() {
        return await this.connectionRepository.getDatabaseDriversList();
    }

    async saveEditDatafileConnection(data : any, id : string) : Promise<any>{
        return await this.connectionRepository.saveEditDatafileConnection(data, id);
    }

    async saveEditDatabaseConnection (data : any, id : string) : Promise<any>{
        return await this.connectionRepository.saveEditDatabaseConnection(data, id);
    }

    async saveNewDatafileConnection(data : any) : Promise<any>{
        return await this.connectionRepository.saveNewDatafileConnection(data);
    }

    async saveNewDatabaseConnection (data : any) : Promise<any>{
        return await this.connectionRepository.saveNewDatabaseConnection(data);
    }

    async getPreviewDatafile(data : any) : Promise<PreviewDatafileResponse>{
        return await this.connectionRepository.getPreviewDatafile(data);
    }

    async getHeaderDataFile(data : any) : Promise<any>{
        return await this.connectionRepository.getHeaderDataFile(data);
    }

    async getDatabaseSchemaList(idDatabase : String) : Promise<DatabaseSchemaListResponse>{
        return await this.connectionRepository.get<DatabaseSchemaListResponse>(process.env.VUE_APP_ROOT_API + '/connections/database/' + idDatabase + '/metadata/schemas');
    }

    async getDatabaseTableList(idDatabase : String, schema : String) : Promise<DatabaseTableListResponse>{
        return await this.connectionRepository.get<DatabaseTableListResponse>(process.env.VUE_APP_ROOT_API + '/connections/database/' + idDatabase + '/metadata/schemas/' + schema + '/tables');
    }

    async getDatabaseViewList(idDatabase : String, schema : String) : Promise<DatabaseViewListResponse>{
        return await this.connectionRepository.get<DatabaseViewListResponse>(process.env.VUE_APP_ROOT_API + '/connections/database/' + idDatabase + '/metadata/schemas/' + schema + '/views');
    }

    async getColumnList(idDatabase : String, typeConnection : string, schema : String, object : String, name : String) : Promise<ColumnListResponse>{
        return await this.connectionRepository.get<ColumnListResponse>(process.env.VUE_APP_ROOT_API + '/connections/'+ typeConnection +'/' + idDatabase + '/metadata/schemas/' + schema + '/' + object + '/' + name +'/columns');
    }

    async getColumnNormalizedList(idDatabase : String, typeConnection : string, schema : String, object : String, name : String) : Promise<ColumnNormalizedListResponse>{
        let path ='';
        if(typeConnection == 'database'){
            path =  process.env.VUE_APP_ROOT_API + '/connections/'+ typeConnection +'/' + idDatabase + '/metadata/schemas/' + schema + '/' + object + '/' + name +'/columns/ndt';
        }
        else{
            path =  process.env.VUE_APP_ROOT_API + '/connections/'+ typeConnection +'/' + idDatabase + '/metadata/columns/ndt';
        }
        return await this.connectionRepository.get<ColumnNormalizedListResponse>(path);
    }

    async getMetricList(cnx_type : String) : Promise<ConfigurationResponse>{
        return await this.connectionRepository.get<ConfigurationResponse>(process.env.VUE_APP_ROOT_API + '/configurations/profile/metrics/platform/' + cnx_type);
    }

    async getConnectionById(idConnection : String): Promise<ConnectionDataBaseModel>{
        return await this.connectionRepository.get<ConnectionDataBaseModel>(process.env.VUE_APP_ROOT_API + '/connections/' + idConnection);
    }

}