
import { LoginRequest, AccessControlList, EntityRecordId, InheritPermissions } from '../Models';
import axios, { AxiosPromise,AxiosRequestConfig } from 'axios';
import { CachingService } from ".";


export class SecurityService {
    private baseUrl: string = '';
    
    private cachingService: CachingService = new CachingService();
    private _axiosRequestConfig : AxiosRequestConfig;
    private config: any = require('../config.json');
    constructor() {
        // const headers: Headers = new Headers();
        // headers.append('Content-Type', 'application/json');
        // headers.append('TenantId', cachingService.read('TenantId'));
        // headers.append('Language', cachingService.readPersistant('Language'));
        // headers.append('Token', cachingService.read('Token'));
        // this._axiosRequestConfig = {headers: headers};

        this._axiosRequestConfig = {
            headers: {
                'Content-Type': 'application/json',
                'TenantId': this.cachingService.read('TenantId'),
                'Language': this.cachingService.readPersistant('Language'),
                'Bearer': this.cachingService.read('Token')
            }};
        this.baseUrl = this.config.baseUrl;
    }
    
    login(loginRequest: LoginRequest): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/Authentication/Login', loginRequest,{
            headers: {
                'Content-Type': 'application/json',
                'TenantId': this.cachingService.read('TenantId'),
                'Language': this.cachingService.readPersistant('Language'),
                'Token': this.cachingService.read('Token')
            }});
    }
    logout(): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/Authentication/Logout',this._axiosRequestConfig);
    }

    register(registerRequest: any): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/Authentication/Register', registerRequest, this._axiosRequestConfig)
    }

    getAccessControlList(id: number): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/Authorization/GetAccessControlList/' + id, this._axiosRequestConfig);
    }
    saveAccessControlList(accessControlList: AccessControlList): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/Authorization/SaveAccessControlList', accessControlList, this._axiosRequestConfig);
    }
    listSecurityRights(entityId: string): AxiosPromise<Response> {
        return axios.get(this.baseUrl + '/api/Authorization/ListSecurityRights/' + entityId, this._axiosRequestConfig);
    }
    breakInheritance(entityRecordId: EntityRecordId): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/Authorization/BreakInheritance', entityRecordId, this._axiosRequestConfig);
    }
    inheritPermissions(inheritPermissions: InheritPermissions): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/Authorization/InheritPermissions', inheritPermissions, this._axiosRequestConfig);
    }
}