import * as React from 'react';
import GoJsChart from './GoJsChart';

import {WorkflowGoJsConverter} from "../../Utils";

import { WorkflowAction, WorkflowState, WorkflowVersion } from '../../Models/WorkflowManager';

export interface WorkflowChartDispatchProps {
    initHandler: (nodes:any) => void;
    updateNodeHandler: (node:any) => void;
    addNodeHandler: (node:any) => void;
}

export class WorkflowChart extends React.Component<any,any> {




    private _myDiagram :any;
    private _workflowGoJsConverter = new WorkflowGoJsConverter();
    private _initialModel = {
        nodeDataArray: [
            this._workflowGoJsConverter.createStartNode(null)                   
        ],
        linkDataArray: [
        ]
    };
    constructor(props:any)    
    {
        super(props);

       this.state={
           model : this._initialModel,
           shoActionHandlersPanel:false
       }    
    } 
    public addAction(action:WorkflowAction){        
        this.addNode(this._workflowGoJsConverter.createActionNode(action))
    }
    public addState(state:WorkflowState){
        this.addNode(this._workflowGoJsConverter.createStateNode(state));
        
    }
    public removeState(state:WorkflowState){
        this.removeNode(this._workflowGoJsConverter.createStateNode(state));
        
    }
    public setVersion(workflowVersion:WorkflowVersion){
        if(workflowVersion){
            let model= this._workflowGoJsConverter.convertWorkflowVersionToDiagramModel(workflowVersion);

            this.setState({model:model});
        }
    }
    public getVersion(workflowId:number,workflowVersionId:number) : WorkflowVersion | null {
        return this._workflowGoJsConverter.convertDiagramModelToWorkflowVersion(this.props.model,workflowId,workflowVersionId);
    }     

    private addNode(node:any){;
        this._myDiagram.addNodeData(node);        
    }

    private removeNode(node:any){;
        this._myDiagram.removeNodeDate(node);       
    }
    
    _ActionHandlers=(shoActionHandlersPanel:boolean) => {

        this.props.ActionHandlerss(shoActionHandlersPanel);
    };
    _actionPermissions=(shoActionHandlersPanel:boolean) => {

        this.props.ActionPermissions(shoActionHandlersPanel);
    };
    _actionProperties=(shoactionPropertiesPanel:boolean) => {

        this.props.ActionProperties(shoactionPropertiesPanel);
    };
    _actionRules=(showActionRulesPanel:boolean) => {

        this.props.ActionRules(showActionRulesPanel);
    };

    componentDidMount() {
        this.props.onRef(this)
    }
    
    componentWillUnmount() {
        this.props.onRef(undefined)
    }

    render() {
        return (
            <div style={{height:'600px'}}>
                <div style={{height:'100%',display: 'flex',flexFlow: 'column'}}>
                <GoJsChart
                 model ={this.state.model}
                 ActionHandlers={this._ActionHandlers}
                 actionPermissions={this._actionPermissions}
                 actionProperties={this._actionProperties}
                 ActionRules={this._actionRules}
                 ContextMenuItems={this.props.ContextMenuItems}
                 onRef={(ref:any) => (this._myDiagram = ref)}/>
                </div>
            </div>
        );
    }    
}

export default WorkflowChart

