import { Entity, Workspace, Lookup, CustomEntityFilter } from '../Models';
import axios, { AxiosPromise, AxiosRequestConfig } from 'axios';
import { CachingService } from ".";


export class EntityService {
    private baseUrl: string = '';
    private _axiosRequestConfig: AxiosRequestConfig;
    private config: any = require('../config.json');

    constructor() {
        let cachingService: CachingService = new CachingService();

        this._axiosRequestConfig = {
            headers: {
                'Content-Type': 'application/json',
                'TenantId': cachingService.read('TenantId'),
                'Language': cachingService.readPersistant('Language'),
                'Bearer': cachingService.read('Token')
            }
        };

        this.baseUrl = this.config.baseUrl;
    }


    listEntities(workspaceId: number): Promise<Entity[]> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/CustomizedEntity/ListEntities', workspaceId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Entity[]);
            }, (err) => {
                reject(err);
            });
        });
    }
    getLookup(fieldId: number): Promise<Lookup[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/CustomizedEntity/GetLookup?fieldId=' + fieldId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Lookup[]);
            }, (err) => {
                reject(err);
            });
        });
    }

    getReasonLookups(transferType: string): Promise<Lookup[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/AAAID/GetReasonLookups?transferType=' + transferType, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Lookup[]);
            }, (err) => {
                reject(err);
            });
        });
    }

    deleteEntity(id: number): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/CustomizedEntity/deleteEntity', id, this._axiosRequestConfig)
    }

    getEntity(id: number): Promise<Entity> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/CustomizedEntity/GetEntity', id, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Entity);
            }, (err) => {
                reject(err);
            });
        });
    }

    saveEntity(entity: Entity): Promise<Entity> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/CustomizedEntity/SaveEntity', entity, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Entity);
            }, (err) => {
                reject(err);
            });
        });
    }
    listRelatedEntities(id: number): Promise<Entity[]> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/CustomizedEntity/ListRelatedEntities', id, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Entity[]);
            }, (err) => {
                reject(err);
            });
        });
    }


    listWorkspaces(): Promise<Workspace[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/CustomizedEntity/ListWorkspaces/', this._axiosRequestConfig).then((response) => {
                resolve(response.data as Workspace[]);
            }, (err) => {
                reject(err);
            });
        });
    }

    deleteWorkspace(id: number): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/CustomizedEntity/DeleteWorkspace', id, this._axiosRequestConfig)
    }

    getWorkspace(id: number): Promise<Workspace> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/CustomizedEntity/GetWorkspace', id, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Workspace);
            }, (err) => {
                reject(err);
            });
        });
    }

    saveWorkspace(workspace: Workspace): Promise<Workspace> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/CustomizedEntity/SaveWorkspace', workspace, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Workspace);
            }, (err) => {
                reject(err);
            });
        });
    }

    listFilters(entityId: number): Promise<CustomEntityFilter[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/CustomizedEntity/GetFilters?entityId=' + entityId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as CustomEntityFilter[]);
            }, (err) => {
                reject(err);
            });
        });
    }
}