﻿if (Poseidon.Controls === undefined) {
    Poseidon.Controls = {};
}

Poseidon.Controls.Print = Ext.extend(Ext.Window, {
    layout: 'fit',
    width: 420,
    minWidth: 420,
    height: 370,
    minHeight: 370,
    resizable: true,
    closable: true,
    closeAction: 'hide',
    autoScroll: true,
    modal: true,
    title: 'Print base type',
    printURL: '/Custom/CenterForRenhold/Webservices/Print.asmx/Print',

    initComponent: function () {

        // Call parent (required)
        Poseidon.Controls.Print.superclass.initComponent.apply(this, arguments);
    },

    /**
    * Private
    */
    executePrint: function (jsonData) {
        this.printCancelled = false;

        Ext.MessageBox.show({
            title: 'Status',
            msg: 'Bestiller udskrift...',
            width: 300,
            wait: true,
            waitConfig: { interval: 200 }
        });

        Ext.Ajax.request({
            url: '/Webservices/Print.asmx/PreparePrint',
            jsonData: {},
            method: 'POST',
            scope: this,
            success: function (response, opts) {
                // Set the print ID as a property on the window.
                this.printId = response = Ext.decode(response.responseText).d;
                jsonData.printId = this.printId;
                Ext.Ajax.request({
                    url: this.printURL,
                    jsonData: jsonData,
                    method: 'POST',
                    scope: this,
                    success: function (response, opts) {
                        response = Ext.decode(response.responseText).d;
                        this.showPrintStatus(response);
                    },
                    failure: function (response, opts) {
                        if (response.isTimeout) {
                            // Wait a bit more and then try again.
                            var task = new Ext.util.DelayedTask(function () {
                                this.getPrintStatus();
                            }, this);
                            task.delay(5000);
                        } else {
                            Ext.MessageBox.show({
                                msg: 'Der er opstået en fejl.',
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.ERROR
                            });
                        }
                    }
                });
            },
            failure: function (response, opts) {
                Ext.MessageBox.show({
                    msg: 'Udskriften kunne ikke påbegyndes.',
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.ERROR
                });
            }
        });
    },

    /**
    * Private
    */
    cancelPrint: function () {
        Ext.Ajax.request({
            url: '/Webservices/Print.asmx/CancelPrint',
            jsonData:
            {
                'printId': this.printId
            },
            method: 'POST',
            scope: this,
            success: function (response, opts) {
                response = Ext.decode(response.responseText).d;
                if (response) {
                    this.showPrintStatus(response);
                } else {
                    var task = new Ext.util.DelayedTask(function () {
                        this.getPrintStatus();
                    }, this);
                    task.delay(5000);
                }
            },
            failure: function (response, opts) {
                if (response.isTimeout) {
                    var task = new Ext.util.DelayedTask(function () {
                        this.getPrintStatus();
                    }, this);
                    task.delay(5000);
                } else {
                    Ext.MessageBox.show({
                        msg: 'Der er opstået en fejl.',
                        buttons: Ext.MessageBox.OK,
                        icon: Ext.MessageBox.ERROR
                    });
                }
            }
        });
    },

    /**
    * Private
    */
    getPrintStatus: function () {
        if (!this.printCancelled) {
            Ext.Ajax.request({
                url: '/Webservices/Print.asmx/GetPrintStatus',
                jsonData:
                {
                    'printId': this.printId
                },
                method: 'POST',
                scope: this,
                success: function (response, opts) {
                    response = Ext.decode(response.responseText).d;
                    if (response) {
                        this.showPrintStatus(response);
                    } else {
                        var task = new Ext.util.DelayedTask(function () {
                            this.getPrintStatus();
                        }, this);
                        task.delay(5000);
                    }
                },
                failure: function (response, opts) {
                    if (response.isTimeout) {
                        var task = new Ext.util.DelayedTask(function () {
                            this.getPrintStatus();
                        }, this);
                        task.delay(5000);
                    } else {
                        Ext.MessageBox.show({
                            msg: 'Der er opstået en fejl.',
                            buttons: Ext.MessageBox.OK,
                            icon: Ext.MessageBox.ERROR
                        });
                    }
                }
            });
        }
    },

    /**
    * Private: Show print status.
    */
    showPrintStatus: function (response) {
        // Determine status.
        switch (response.PrintStatusID) {
            case 1:
                Ext.MessageBox.show({
                    title: 'Status',
                    msg: 'Der er i øjeblikket kø og dit print er nummer ' + response.NumberInQueue + ' i køen. Vent venligst...',
                    width: 300,
                    wait: true,
                    buttons: Ext.Msg.CANCEL,
                    fn: function () {
                        this.cancelPrint();
                    },
                    scope: this,
                    waitConfig: { interval: 200 }
                });
                break;
            case 2:
                Ext.MessageBox.show({
                    title: 'Status',
                    msg: 'Dit print er ved at blive udskrevet. Vent venligst...',
                    width: 300,
                    wait: true,
                    waitConfig: { interval: 200 }
                });
                break;
            case 3:
                Ext.MessageBox.hide();
                this.showLinkDialog(response.PrintID);
                break;
            case 5:
                this.printCancelled = true;
                Ext.MessageBox.show({
                    title: 'Status',
                    msg: 'Din udskrift er blevet annulleret.',
                    width: 300,
                    //wait: true,
                    buttons: Ext.Msg.OK//,
                    //waitConfig: { interval: 200 }
                });
                break;
            case 4:
            default:
                Ext.MessageBox.show({
                    title: 'Status',
                    msg: 'Der opstod et problem under dannelsen af dit print. ' +
                         'Vi beklager de gener det måtte medføre og beder dig venligst om at kontakte os, så vi snarest muligt kan løse problemet.',
                    width: 300,
                    buttons: Ext.Msg.OK//,
                });
                break;
        }

        if (response.PrintStatusID === 1 || response.PrintStatusID === 2) {
            // Get new status update in 5 seconds.
            var task = new Ext.util.DelayedTask(function () {
                this.getPrintStatus();
            }, this);
            task.delay(5000);
        }
    },

    /**
    * Private
    */
    showLinkDialog: function (print_id) {
        var dialog = new Ext.Window({
            title: 'Udskrift færdig...',
            width: 400,
            height: 100,
            //autoHeight: true,
            layout: 'form',
            resizable: false,
            closable: false,
            closeAction: 'close',
            autoScroll: true,
            collapsible: false,
            modal: true,
            items: [
                {
                    xtype: 'box',
                    html: '<p>Din udskrift er nu klar. Tryk på knappen "Hent" for at hente udskriften. <br /></p>'
                }
            ],
            buttons: [
                {
                    xtype: 'linkbutton',
                    text: 'Hent',
                    href: '/fileHandler.ashx?fileid=' + print_id,
                    target: '_blank'
                }, {
                    xtype: 'button',
                    text: 'Luk',
                    handler: function (button, event) {
                        dialog.close();
                    },
                    scope: this
                }
            ]
        });

        dialog.show();
    }
});
