import * as React from "react";
import { ExecuteActionButton as CoreExecuteActionButton } from "../../Controls/ExecuteActionButton";
import { ManageAttendees } from "./ManageAttendees";
import { ManageSubjects } from "./ManageSubjects";
import { ManageRooms } from "./ManageRooms";
import { EditMeetingDetails } from "./EditMeetingDetails";
import { ManageTasks } from "./ManageTasks";
import { Dialog, DialogType, DialogFooter, DefaultButton } from "office-ui-fabric-react";
import { CachingService, ImportExportService } from "../../../Services";
import { FormattedMessage } from "react-intl";
import { RecordSignature } from "src/Models/RecordSignature";

export class ExecuteActionButton extends React.Component<any, any> {
    private _cachingService = new CachingService();
    private _ImportExportService: ImportExportService = new ImportExportService();
    private _loggedInUserId: number;

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

        let cachingService: CachingService = new CachingService();
        this._loggedInUserId = cachingService.read('UserId');

        this.state = {
            isAttendeesPanelVisible: false,
            isRoomsPanelVisible: false,
            isSubjectsPanelVisible: false,
            isEditMeetingDetailsPanelVisible: false,
            isTasksPanelVisible: false,
            hideDownloadingPDFDialog: true
        }
    }

    private executeManageAttendeesAction() {
        this.setState({ isAttendeesPanelVisible: true })
    }

    private closeAttendeesPanel() {
        this.setState({ isAttendeesPanelVisible: false })
    }

    private executeManageRoomsAction() {
        this.setState({ isRoomsPanelVisible: true })
    }

    private closeRoomsPanel() {
        this.setState({ isRoomsPanelVisible: false })
    }

    private executeManageSubjectsAction() {
        this.setState({ isSubjectsPanelVisible: true })
    }

    private closeSubjectsPanel() {
        this.setState({ isSubjectsPanelVisible: false })
    }

    private executeEditMeetingDetailsAction() {
        this.setState({ isEditMeetingDetailsPanelVisible: true })
    }

    private closeEditMeetingDetailsPanel() {
        this.setState({ isEditMeetingDetailsPanelVisible: false })
    }

    private executeManageTasksAction() {
        this.setState({ isTasksPanelVisible: true })
    }

    private closeManageTasksPanel() {
        this.setState({ isTasksPanelVisible: false })
    }

    private executeDownloadPDFAction() {
        var name = "";
        this.props.Record.fields.forEach((field: any) => {
            if (field.id == 294) {
                name = field.data;
            }
        })

        this._ImportExportService.downloadFile(this.props.Record.id, 33, name);

        this.setState({ hideDownloadingPDFDialog: false })
    }

    private _closeDownloadingPDFDialog() {
        this.setState({ hideDownloadingPDFDialog: true });
    }

    private _onRenderButtonText = (item: any): JSX.Element => {
        return <FormattedMessage id={item.text} />;
    };

    private validateSignButton(){
        if (this.props.Record.signatures) {
            var userSignatureExists = false;

            this.props.Record.signatures.forEach((sign: RecordSignature) => {
                if (sign.signatureCreatedById == this._loggedInUserId)
                    userSignatureExists = true;
            });

            return !userSignatureExists;
        }

        return true;
    }

    private validateUnSignButton(){
        if (this.props.Record.signatures) {
            var userSignatureExists = false;

            this.props.Record.signatures.forEach((sign: RecordSignature) => {
                if (sign.signatureCreatedById == this._loggedInUserId)
                    userSignatureExists = true;
            });

            return userSignatureExists;
        }

        return false;
    }

    public render(): JSX.Element {

        if (this.props.workflowAction && this.props.workflowAction.id == 4204 && !this.validateSignButton()) {
            return <div></div>;
        }

        if (this.props.workflowAction && this.props.workflowAction.id == 4216 && !this.validateUnSignButton()) {
            return <div></div>;
        }

        if (this.props.workflowAction && this.props.workflowAction.id == 4208) {
            this.props.workflowAction.actionToExecute = this.executeManageAttendeesAction.bind(this)
        }

        if (this.props.workflowAction && this.props.workflowAction.id == 4210) {
            this.props.workflowAction.actionToExecute = this.executeManageSubjectsAction.bind(this)
        }

        if (this.props.workflowAction && this.props.workflowAction.id == 4209) {
            this.props.workflowAction.actionToExecute = this.executeManageRoomsAction.bind(this)
        }

        if (this.props.workflowAction && this.props.workflowAction.id == 4206) {
            this.props.workflowAction.actionToExecute = this.executeEditMeetingDetailsAction.bind(this)
        }

        if (this.props.workflowAction && this.props.workflowAction.id == 4214) {
            this.props.workflowAction.actionToExecute = this.executeManageTasksAction.bind(this)
        }

        if (this.props.workflowAction && this.props.workflowAction.id == 4205) {
            this.props.workflowAction.actionToExecute = this.executeDownloadPDFAction.bind(this)
        }

        return (
            <div style={{ display: 'inherit' }}>
                <CoreExecuteActionButton {...this.props}></CoreExecuteActionButton>
                {this.state.isAttendeesPanelVisible ?
                    <ManageAttendees onExecute={this.props.onExecute} open={this.state.isAttendeesPanelVisible}
                        meetingId={this.props.recordId}
                        closePanel={this.closeAttendeesPanel.bind(this)}></ManageAttendees>
                    : ""}
                {this.state.isSubjectsPanelVisible ?
                    <ManageSubjects onExecute={this.props.onExecute} open={this.state.isSubjectsPanelVisible}
                        meetingId={this.props.recordId}
                        closePanel={this.closeSubjectsPanel.bind(this)}></ManageSubjects>
                    : ""}
                {this.state.isRoomsPanelVisible ?
                    <ManageRooms onExecute={this.props.onExecute} open={this.state.isRoomsPanelVisible}
                        meetingId={this.props.recordId}
                        closePanel={this.closeRoomsPanel.bind(this)}></ManageRooms>
                    : ""}
                {this.state.isEditMeetingDetailsPanelVisible ?
                    <EditMeetingDetails onExecute={this.props.onExecute} open={this.state.isEditMeetingDetailsPanelVisible}
                        recordId={this.props.recordId}
                        closePanel={this.closeEditMeetingDetailsPanel.bind(this)}></EditMeetingDetails>
                    : ""}
                {this.state.isTasksPanelVisible ?
                    <ManageTasks onExecute={this.props.onExecute} open={this.state.isTasksPanelVisible}
                        meetingId={this.props.recordId}
                        closePanel={this.closeManageTasksPanel.bind(this)}></ManageTasks>
                    : ""}


                <Dialog
                    hidden={this.state.hideDownloadingPDFDialog}
                    onDismiss={this._closeDownloadingPDFDialog.bind(this)}
                    dialogContentProps={{
                        type: DialogType.normal,
                        title:
                            this._cachingService.readPersistant("Language") == 0
                                ? "Download PDF"
                                : "تحميل ك PDF",
                        subText:
                            this._cachingService.readPersistant("Language") == 0
                                ? "PDF is getting downloaded. Please wait for a few seconds."
                                : "الرجاء الانتظار. يتم تحميل المستند بصيغة PDF"
                    }}
                    modalProps={{
                        titleAriaId: "myLabelId",
                        subtitleAriaId: "mySubTextId",
                        isBlocking: false,
                        containerClassName: "ms-dialogMainOverride"
                    }}
                >
                    {
                        null /** You can also include null values as the result of conditionals */
                    }
                    <DialogFooter>
                        <DefaultButton
                            onClick={this._closeDownloadingPDFDialog.bind(this)}
                            text="Close"
                            onRenderText={this._onRenderButtonText}
                        />
                    </DialogFooter>
                </Dialog>
            </div>
        );
    }
}