import * as React from "react";
import { BrowserRouter, Switch, Route } from "react-router-dom"
import { OrganizationUnits } from "./Modules/Organization/OrganizationUnits"
import { Workspace } from "./Modules/Entities/WorkSpace"
import { EntityList } from "./Modules/Entities/EntityList"
import { Header, SideBarLeft } from "../../Controls"
import { EntityDesigner } from "./Modules/Entities/EntityDesigner";
import { EntityService} from "../../Services";
import { INavLink,INavLinkGroup} from 'office-ui-fabric-react/lib/Nav';
import { initializeIcons } from 'office-ui-fabric-react/lib/Icons';
import WorkflowDesigner from "./Modules/Workflow/WorkflowDesigner";
import { UserList } from "../../Views/Admin/Modules/Organization/UsersList";
import { PositionList } from "../../Views/Admin/Modules/Organization/PositionsList";
import { GroupList } from "../../Views/Admin/Modules/Organization/GroupsList";
import { OrganizationUnitList } from "../../Views/Admin/Modules/Organization/OrganizationsList";
import WorkflowManager from "./Modules/Workflow/WorkflowManager";

export class Main extends React.Component<any,any> {
    private _entityService:EntityService = new EntityService();

    constructor(props:any) {
        super(props);
        initializeIcons(undefined, { disableWarnings: true });

        this.state = {
            groups: new Array<INavLinkGroup>()
        };
        this.loadLinks();
    }
    

    private loadLinks(){        
        let links:INavLink[] = new Array<INavLink>();
        
        if(true){
            let link:INavLink = { name: 'WorkSpaces', url: '/workspace', key: 'workspace',isExpanded: true }; 
            
            this._entityService.listWorkspaces()
            .then((result)=>{
                link.links = new Array<INavLink>();
                for (let workspace of result) {
                    link.links.push({ name: workspace.displayName, url: '/entitylist/' + workspace.id, key: 'workspace'+workspace.id });
                }
                this.setState({groups:this.state.groups});
            });
            
            links.push(link);
        }

        if(true){
            let link:INavLink = { name: 'Workflow', url: '/workflow', key: 'workflow',isExpanded: true }; 

            links.push(link);
        }
   
        this.state.groups.push({links});
    }

    public render(): JSX.Element {
    return (
        <BrowserRouter>
            <div>
                <div>
                    <Header />
                </div>
                <div className="ms-Grid">
                    <div className="ms-Grid-row">
                    <div style={{ display: 'inline-block', float: 'left' }}> <SideBarLeft Groups={this.state.groups} /> </div>

                        <div className="ms-Grid-col ms-sm2 ms-md4 ms-lg10">
                            <Switch>
                                <Route path="/workflow" component={WorkflowManager} exact></Route>
                                <Route path="/workflowDesigner/:id/:versionId" component={WorkflowDesigner} exact></Route>
                                <Route path="/workspace" component={Workspace} exact></Route>
                                <Route path="/organizationunits" component={OrganizationUnits} exact></Route>
                                <Route path="/UserList" component={UserList} exact></Route>
                                <Route path="/organizationunitsList" component={OrganizationUnitList} exact></Route>
                                <Route path="/PositionList" component={PositionList} exact></Route>
                                <Route path="/GroupList" component={GroupList} exact></Route>
                                <Route path="/entitylist/:id" component={EntityList} exact></Route>
                                <Route path="/entityDesigner" component={EntityDesigner} exact></Route>

                                <Route path="/" component={Workspace} exact></Route>
                            </Switch>
                        </div>
                    </div>
                </div>
            </div>
        </BrowserRouter>
    )
}
}

export default Main;