import axios, { AxiosRequestConfig } from 'axios';
import { CachingService } from ".";
import {Workflow ,WorkflowVersion, WorkflowState, WorkflowAction, HandlerParameter,WorkflowEventHandler} from '../Models/WorkflowDesigner';

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

    listWorkflows(): Promise<Workflow[]> {        
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/ListWorkflows', this._axiosRequestConfig).then((response) => {
                resolve(response.data as Workflow[]);
            }, (err) => {
                reject(err);
            });
         });
    }

    saveWorkflow(workflow:Workflow): Promise<Workflow> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/WorkflowDesigner/SaveWorkflow',workflow, this._axiosRequestConfig).then((response) => {
                resolve(response.data as Workflow);
            }, (err) => {
                reject(err);
            });
         });
    }

    saveWorkflowVersion(workflowVersion:WorkflowVersion): Promise<WorkflowVersion> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/WorkflowDesigner/SaveWorkflowVersion',workflowVersion, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowVersion);
            }, (err) => {
                reject(err);
            });
         });
    }
    getWorkflowVersions(workflowId:number): Promise<WorkflowVersion[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/GetWorkflowVersions?id=' + workflowId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowVersion[]);
            }, (err) => {
                reject(err);
            });
         });
    }
    createWorkflowState(workflowState:WorkflowState): Promise<WorkflowState> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/WorkflowDesigner/CreateWorkflowState',workflowState, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowState);
            }, (err) => {
                reject(err);
            });
         });
    }
    createWorkflowAction(workflowAction:WorkflowAction): Promise<WorkflowAction> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/WorkflowDesigner/CreateWorkflowAction',workflowAction, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowAction);
            }, (err) => {
                reject(err);
            });
         });
    }
    deleteWorkflowAction(actionId:number): Promise<void> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/DeleteWorkflowAction?actionId=' + actionId, this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
         });
    }
    setDefaultStateForWorkflowVersion(stateId:number): Promise<void> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/SetDefaultStateForWorkflowVersion?stateId=' + stateId, this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
         });
    }
    addActionToState(actionId:number,stateId:number): Promise<void> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/AddActionToState?actionId=' + actionId + '&stateId=' + stateId, this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
         });
    }
    addHandlerToBeforeStateChanged(handlerId:number,stateId:number,parameters:HandlerParameter[]): Promise<WorkflowState> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/WorkflowDesigner/AddHandlerToBeforeStateChanged?handlerId=' + handlerId + '&stateId=' + stateId,parameters, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowState);
            }, (err) => {
                reject(err);
            });
         });
    }

    addHandlerToAfterStateChanged(handlerId:number,stateId:number,parameters:HandlerParameter[]): Promise<WorkflowState> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/WorkflowDesigner/AddHandlerToAfterStateChanged?handlerId=' + handlerId + '&stateId=' + stateId,parameters, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowState);
            }, (err) => {
                reject(err);
            });
         });
    }

    addHandlerToAction(handlerId:number,actionId:number,parameters:HandlerParameter[]): Promise<WorkflowAction> {
        return new Promise((resolve, reject) => {
            axios.post(this.baseUrl + '/api/WorkflowDesigner/AddHandlerToAction?handlerId=' + handlerId + '&actionId=' + actionId,parameters, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowAction);
            }, (err) => {
                reject(err);
            });
         });
    }

    removeActionFromState(actionId:number,stateId:number): Promise<void> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/RemoveActionFromState?actionId=' + actionId + '&stateId=' + stateId, this._axiosRequestConfig).then((response) => {
                resolve(response.data);
            }, (err) => {
                reject(err);
            });
         });
    }
    removeHandlerFromAction(handlerId:number,actionId:number): Promise<WorkflowAction> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/RemoveHandlerFromAction?handlerId=' + handlerId + '&actionId=' + actionId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowAction);
            }, (err) => {
                reject(err);
            });
         });
    }
    removeHandlerFromBeforeStateChanged(handlerId:number,stateId:number): Promise<WorkflowState> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/RemoveHandlerFromBeforeStateChanged?handlerId=' + handlerId + '&stateId=' + stateId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowState);
            }, (err) => {
                reject(err);
            });
         });
    }
    removeHandlerFromAfterStateChanged(handlerId:number,stateId:number): Promise<WorkflowState> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/RemoveHandlerFromAfterStateChanged?handlerId=' + handlerId + '&stateId=' + stateId, this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowState);
            }, (err) => {
                reject(err);
            });
         });
    }
    listWorkflowEventHandlers(): Promise<WorkflowEventHandler[]> {
        return new Promise((resolve, reject) => {
            axios.get(this.baseUrl + '/api/WorkflowDesigner/ListWorkflowEventHandlers?', this._axiosRequestConfig).then((response) => {
                resolve(response.data as WorkflowEventHandler[]);
            }, (err) => {
                reject(err);
            });
         });
    }
}