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


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

    }


    getOrganization(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/Organization/GetOrganization/' + id, this._axiosRequestConfig)
    }
    deleteOrganization(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/Organization/DeleteOrganization/' + id, this._axiosRequestConfig)
    }
    getUserOrganizations(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/Organization/GetUserOrganizations/' + id, this._axiosRequestConfig)
    }
    listOrganizations(): Promise<OrganizationUnit[]> {

        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/Organization/ListOrganizations', this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
        });
    }
    listOrganizationsForTransfer(correspondenceDepartmentId: number): Promise<OrganizationUnit[]> {

        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/AAAID/ListOrganizationsForTransfer?correspondenceDepartmentId=' + correspondenceDepartmentId, this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
        });
    }
    saveOrganization(organizationUnit: OrganizationUnit): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/Organization/SaveOrganization', organizationUnit, this._axiosRequestConfig)
    }
    getOrganizationUnitTypes(): Promise<OrganizationUnitType[]> {

        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/Organization/GetOrganizationUnitTypes', this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
        });
    }
}