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

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

    }


    getPosition(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/Position/GetPosition/' + id,this._axiosRequestConfig)
    }
    deletePosition(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/Position/DeletePosition/' + id,this._axiosRequestConfig)
    }
    getUserPositions(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/Position/GetUserPositions/' + id,this._axiosRequestConfig)
    }
    listPositions(): Promise<Position[]> {

        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/Position/ListPositions', this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
        });
    }
    savePosition(position: Position): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/Position/SavePosition', position,this._axiosRequestConfig)
    }
}