import * as React from "react";
import HistoryTimeline from "../../Controls/HistoryTimeline";
import { HistoryTimeLineEvent } from "src/Models/HistoryTimelineEvent";
import { RecordQuickView } from "./RecordQuickView";
import { CachingService } from "../../Services";

export class RecordsHistoryTimeline extends React.Component<any, any> {
    private _loggedInUser: any;

    constructor(props: any) {
        super(props);

        let cachingService: CachingService = new CachingService();
        this._loggedInUser = cachingService.read('LoggedInUser');

        this.state = {
            events: this.generateTimelineEvents()
        }
    }

    private generateTimelineEvents(): HistoryTimeLineEvent[] {
        let events: HistoryTimeLineEvent[] = [];

        if (this.props.Items) {
            this.props.Items.forEach((element: any) => {
                let _iconProps: any;

                if (element.FromDepartment == this._loggedInUser.department) {
                    element.Direction = 11;
                }
                else {
                    if (element.ToDepartments) {
                        element.ToDepartments.forEach((dep: any) => {
                            if (dep.ToDepartment == this._loggedInUser.department) {
                                element.Direction = 10;
                                element.CorrespondenceId = dep.CorrespondenceId;
                            }
                        })
                    }
                }

                if (element.Status == 22) {
                    _iconProps = { className: 'iconsmind-Mail-Add' };
                }
                else if (element.Direction == 11) {
                    _iconProps = { className: 'iconsmind-Mail-Outbox' };
                }
                else {
                    _iconProps = { className: 'iconsmind-Mail-Inbox' };
                }

                events.push({
                    createdAt: <div>{element.CreatedDate}</div>,
                    iconProps: _iconProps,
                    content: <RecordQuickView Record={element}></RecordQuickView>
                })
            });
        }

        return events;
    }

    public render(): JSX.Element {
        return (<HistoryTimeline events={this.state.events}></HistoryTimeline>)
    }
}