import { Record, LinkRelatedEntitiesRecordsDto, ListRecordsCriteria, Records, WorkflowHistory } from '../Models';
import axios, { AxiosPromise, AxiosRequestConfig } from 'axios';
import { CachingService } from ".";
import { CorrespondenceTransfer } from 'src/Models/CorrespondenceTransfer';
import { CorrespondenceReply } from 'src/Models/CorrespondenceReply';
import { RecordSignature } from 'src/Models/RecordSignature';

export class RecordService {
    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'),
                'Authorization': 'Bearer ' + cachingService.read('Token'),
                'UserId': cachingService.read('UserId')
            }
        };
        this.baseUrl = this.config.baseUrl;
    }

    getRecordsCount(criteria: ListRecordsCriteria): Promise<Records> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/CustomizedEntityRecord/GetRecordsCount', criteria, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Records);
            }, (err) => {
                reject(err);
            });
        });
    }
    listRecords(criteria: ListRecordsCriteria): Promise<Records> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/CustomizedEntityRecord/ListRecords', criteria, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Records);
            }, (err) => {
                reject(err);
            });
        });
    }

    deleteRecord(entityId: number, id: number): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/DeleteRecord', { "entityId": entityId, "id": id }, this._axiosRequestConfig)
    }

    getRecord(entityId: number, id: number): Promise<Record> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/AAAID/GetRecord', { "entityId": entityId, "id": id }, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Record);
            }, (err) => {
                reject(err);
            });
        });
    }

    saveRecord(record: Record): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/saveRecord', record, this._axiosRequestConfig)
    }
    getEmptyRecord(entityId: number): Promise<Record> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/AAAID/GetEmptyRecord', entityId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Record);
            }, (err) => {
                reject(err);
            });
        });
    }
    getEmptyRecordForMeeting(entityId: number, templateId: number): Promise<Record> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/AAAID/GetEmptyRecordForMeeting?entityId=' + entityId + '&templateId=' + templateId,
                this._axiosRequestConfig).then((response) => {
                    resolve(response.data as Record);
                }, (err) => {
                    reject(err);
                });
        });
    }
    getEmptyCorrespondenceRecordForReply(entityId: number): Promise<Record> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/AAAID/GetEmptyCorrespondenceRecordForReply', entityId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Record);
            }, (err) => {
                reject(err);
            });
        });
    }
    linkRelatedEntitiesRecords(dto: LinkRelatedEntitiesRecordsDto): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/CustomizedEntityRecord/linkRelatedEntitiesRecords', dto, this._axiosRequestConfig)
    }
    unlinkRelatedEntitiesRecords(dto: LinkRelatedEntitiesRecordsDto): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/CustomizedEntityRecord/unlinkRelatedEntitiesRecords', dto, this._axiosRequestConfig)
    }
    executeAction(actionId: number, entityId: number, record: Record): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/CustomizedEntityRecord/executeAction?entityId=' + entityId + '&actionId=' + actionId, record, this._axiosRequestConfig);
    }
    getWorkflowHistory(entityId: number, recordId: number): Promise<WorkflowHistory[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/CustomizedEntityRecord/GetWorkflowHistory?entityId=' + entityId + '&recordId=' + recordId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowHistory[]);
            }, (err) => {
                reject(err);
            });
        });
    }

    getHistory(entityId: number, recordId: number): Promise<Record[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/AAAID/GetHistory?entityId=' + entityId + '&recordId=' + recordId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Record[]);
            }, (err) => {
                reject(err);
            });
        });
    }

    transferCorrespondence(record: CorrespondenceTransfer): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/TransferCorrespondence', record, this._axiosRequestConfig)
    }
    moveDraftCorrespondence(record: CorrespondenceTransfer): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/MoveDraftCorrespondence', record, this._axiosRequestConfig)
    }
    approveCorrespondence(record: CorrespondenceReply): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/approveCorrespondence', record, this._axiosRequestConfig)
    }
    replyWithNewCorrespondence(record: CorrespondenceReply): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/replyWithNewCorrespondence', record, this._axiosRequestConfig)
    }
    rejectCorrespondence(record: CorrespondenceReply): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/rejectCorrespondence', record, this._axiosRequestConfig)
    }
    closeCorrespondence(record: CorrespondenceReply): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/closeCorrespondence', record, this._axiosRequestConfig)
    }
    reOpenCorrespondence(record: CorrespondenceReply): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/ReOpenCorrespondence', record, this._axiosRequestConfig)
    }
    archiveRecord(entityId: number, id: number): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/ArchiveCorrespondence', { "entityId": entityId, "id": id }, this._axiosRequestConfig)
    }
    signRecord(entityId: number, id: number): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/SignDocument', { "entityId": entityId, "id": id }, this._axiosRequestConfig)
    }
    unSignRecord(entityId: number, id: number): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/UnSignDocument', { "entityId": entityId, "id": id }, this._axiosRequestConfig)
    }
    exportExternal(entityId: number, id: number): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/ExportExternal', { "entityId": entityId, "id": id }, this._axiosRequestConfig)
    }
    updateLetterDetails(record: Record): AxiosPromise<Response> {
        return axios.post(this.baseUrl + '/api/AAAID/UpdateLetterDetails', record, this._axiosRequestConfig)
    }

    getRecordSignatures(entityId: number, recordId: number): Promise<RecordSignature[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/CustomizedEntityRecord/GetRecordSignatures?entityId=' + entityId + '&recordId=' + recordId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as RecordSignature[]);
            }, (err) => {
                reject(err);
            });
        });
    }

    getCorrespondenceApprovalEntries(correspondenceId: number): Promise<RecordSignature[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/AAAID/GetCorrespondenceApprovalEntries?correspondenceId=' + correspondenceId, this._axiosRequestConfig)
                .then((response) => {
                    resolve(response.data as RecordSignature[]);
                }, (err) => {
                    reject(err);
                });
        });
    }
}