import { SecurityGroup } from '../Models';
import axios, { AxiosPromise,AxiosRequestConfig } from 'axios';
import { CachingService } from ".";

export class SecurityGroupService {
    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;
    }

    getSecurityGroup(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/SecurityGroup/GetSecurityGroup/' + id,this._axiosRequestConfig)
    }
    deleteSecurityGroup(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/SecurityGroup/DeleteSecurityGroup/' + id,this._axiosRequestConfig)
    }
    getUserSecurityGroups(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/SecurityGroup/GetUserSecurityGroups/' + id,this._axiosRequestConfig)
    }
    listSecurityGroups(): Promise<Position[]> {

        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/SecurityGroup/ListSecurityGroups', this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
        });
    }
    saveSecurityGroup(securityGroup: SecurityGroup): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/SecurityGroup/SaveSecurityGroup', securityGroup,this._axiosRequestConfig)
    }
}