import { http } from '../http/http';
import { Repository } from '../../infrastructure/repositories/repository'
import { ConnectionSummary } from '../../domain/models/connections/ConnectionSummary.interface';
import { ConnectionResponse } from '../../domain/models/connections/ConnectionResponse.interface';
import { ConnectionDataBaseModel } from '../../domain/models/connections/ConnectionDataBaseModel.interface';
import { DriverList } from '../../domain/models/drivers/DriverList.interface';
import { PreviewDatafileResponse } from '../../domain/models/previewDatafile/PreviewDatafileResponse.interface';


export class ConnectionRepository extends Repository {
    constructor() {
        super();
    }

    async getConnectionSummary(): Promise<ConnectionSummary> {
        const connectionSummary = await http.get<ConnectionSummary>(process.env.VUE_APP_ROOT_API + '/connections/summary');
        return connectionSummary;
    }

    async deleteConnection(idConnectionSelected: String): Promise<ConnectionResponse> {
        const connectionResponse = await http.delete<ConnectionResponse>(process.env.VUE_APP_ROOT_API + '/connections/' + idConnectionSelected);
        return connectionResponse;
    }

    async validateConnection(idConnectionSelected: String, cnx_type: String): Promise<ConnectionResponse> {
        const connectionResponse = await http.get<ConnectionResponse>(process.env.VUE_APP_ROOT_API + '/connections/' + cnx_type + '/' + idConnectionSelected + '/test');
        return connectionResponse;
    }

    async validateNewConnection(cnx_type: String, data : any): Promise<ConnectionResponse> {
        const connectionResponse = await http.post<ConnectionResponse>(process.env.VUE_APP_ROOT_API + '/connections/' + cnx_type + '/test', data);
        return connectionResponse;
    }

    async getConnection(idConnectionSelected: String, cnx_type: String): Promise<ConnectionDataBaseModel> {
        const connectionResponse = await http.get<ConnectionDataBaseModel>(process.env.VUE_APP_ROOT_API + '/connections/' + cnx_type + '/' + idConnectionSelected);
        return connectionResponse;
    }

    async getConnectionList(): Promise<ConnectionDataBaseModel> {
        const connectionResponse = await http.get<ConnectionDataBaseModel>(process.env.VUE_APP_ROOT_API + '/connections');
        return connectionResponse;
    }

    async getConnectionDatabaseList(): Promise<ConnectionDataBaseModel> {
        const connectionResponse = await http.get<ConnectionDataBaseModel>(process.env.VUE_APP_ROOT_API + '/connections/database');
        return connectionResponse;
    }

    async getConnectionDatafileList(): Promise<ConnectionDataBaseModel> {
        const connectionResponse = await http.get<ConnectionDataBaseModel>(process.env.VUE_APP_ROOT_API + '/connections/delimited-file');
        return connectionResponse;
    }

    getQueryStringParameters(): any {
        return http.getQueryStringParameters();
    }

    async getDatabaseDriversList(): Promise<DriverList> {
        const driverList = await http.get<DriverList>(process.env.VUE_APP_ROOT_API + '/drivers');
        return driverList;
    }

    async saveEditDatafileConnection(data : any, id : string) : Promise<any>{
        let response : any = {};
        response = await http.put(process.env.VUE_APP_ROOT_API + '/connections/delimited-file/' + id, data);
        return response;
    }

    async saveEditDatabaseConnection(data : any, id : string) : Promise<any>{
        let response : any = {};
        response = await http.put(process.env.VUE_APP_ROOT_API + '/connections/database/' + id, data);
        return response;
    }

    async saveNewDatafileConnection(data : any) : Promise<any>{
        let response : any = {};
        response = await http.post(process.env.VUE_APP_ROOT_API + '/connections/delimited-file/', data);
        return response;
    }

    async saveNewDatabaseConnection(data : any) : Promise<any>{
        let response : any = {};
        response = await http.post(process.env.VUE_APP_ROOT_API + '/connections/database/', data);
        return response;
    }

    async getPreviewDatafile(data : any) : Promise<PreviewDatafileResponse>{
        let response : PreviewDatafileResponse;
        response = await http.post<PreviewDatafileResponse>(process.env.VUE_APP_ROOT_API + '/connections/delimited-file/preview', data);
        return response;
    }

    async getHeaderDataFile(data : any) : Promise<any>{
        let response : any = {};
        response = await http.post(process.env.VUE_APP_ROOT_API + '/connections/delimited-file/header', data);
        return response;
    }
}