/**
 * Created with JetBrains WebStorm.
 * User: Cmma
 * Date: 7/23/14
 * Time: 12:41 PM
 * To change this template use File | Settings | File Templates.
 */

define(function (require, exports) {

    var userSvc = require('../../../../../../../share/assert/js/usersvc').userSvc;
    var uploadPackage = require('../../../../addon/upload').uploadPackage;
    var setFileID = require('../../../../addon/upload').setFileID;
    var finishDlg = require('./uploadfinish').UploadFinishDlg;
    var upload = require('./upload').upload;
    var LM = require('editor/assert/js/addon/logManager').LogManager;

    function UploadDlg() {

        this.showDialog = function (callback) {

            createDlg().done(function (dlg) {
//                var iframe = document.getElementById("uploadFrame");
//                if(iframe.attachEvent){
//                        console.log("start upload !");
//                        uploadPackage(function(taskid, progress) {
//                            console.log(progress);
//                            //iframe.contentWindow.setProgressValue(progress);
//                        }, function(taskid, httpCode, data) {
//                            console.log(data);
//                            setFileID(data.substr(1, data.length - 2));
//                            callback(httpCode);
//                            dlg.modal('hide');
//                            setTimeout(function () {
//                                dlg.remove();
//                                $("body").find(".modal-backdrop.fade.in").remove();
//                            }, 500);
//                        });
//
//                } else{
//                    iframe.onload = function(){
                dlg.modal({show: true, keyboard: false, backdrop: "static"});
                console.log("start upload !");
                uploadPackage(function (taskid, progress) {
                    console.log(progress);
                    if (progress > 1) {
                        progress = progress - 1;
                    }
                    upload.SetProValue(Math.round(progress));
                }, function (taskid, httpCode, data) {
                    console.log(data);
                    callback(httpCode);
                    if (httpCode == 200) {
                        setFileID(data.substr(1, data.length - 2));
                        upload.SetProValue(100);
                        upload.SetUploadedStatus();
                        // 用户统计
                        LM.statisticsMoudleTime('4103', 'trigger', 'upload_count');
                    } else {
                        if (httpCode == 500) {
                            upload.SetErrorMsg("服务器错误");
                        } else if (httpCode == 401) {
                            upload.SetErrorMsg("安全验证失败");
                        }
                        upload.SetFailedStatus();
                    }
                });
//                    };
//                }

                dlg.find(".w_func_close").bind('click', function () {
                    dlg.modal('hide');
                    setTimeout(function () {
                        dlg.remove();
                        $("body").find(".modal-backdrop.fade.in").remove();
                    }, 500);
                });
            });
        }
    }

    function createDlg() {
        if ($('#modal-content'))    $('#modal-content').remove();
        var def = jQuery.Deferred();
        seajs.use('editor/assert/js/plugins/table/dialogs/upload/upload.tpl', function (tpl) {
                var dlg = $(tpl);
                $('body').append(dlg);
                var clientHeight = document.body.clientHeight;
                dlg.find(".modal-content").css("margin-top", (clientHeight - 250) / 2);
                def.resolve(dlg);
            }
        );
        return def.promise();
    }

    exports.uploadDlg = UploadDlg;

})