/**
 * Created with JetBrains WebStorm.
 * User: dfwang
 * Date: 14-6-28
 * Time: 上午10:53
 * To change this template use File | Settings | File Templates.
 */
define(function(require, exports) {
    var commands =   require('editor/assert/js/utils/commands').commands;
    var AppContext=require('../../../../share/assert/js/appcontext').AppContext;
    var isUpdate = false;
    /**
     * 显示编辑框
     * @param opt
     * @param finishcallback
     */
    fabric.TextField.prototype.showEditDialog=function(opt, finishcallback){
        var _that = this,phtml='',tablecot=null;
        //opt &&(this.path = opt);
        finishcallback && (this.callback = finishcallback);
        _that.callback.call(_that,{})
    }
    /**
     * 显示编辑按钮
     * @param opt
     * @param finishcallback
     */
    fabric.TextField.prototype.showEditButton = function(_slide){
        var _that = this;
        commands.usePropertyCtrlArea('textfield',false,true);

        setButtonState();

        EventBase.off('fontSelect');
        EventBase.on('fontSelect',function(){
            Global.isVariety = true;
            var _opt = arguments[1];
            //找到父对象
            var _fdiv = $(arguments[1][0]).closest('div').find('button:nth-child(1)');
            var _value = _opt[0].text || _fdiv.text;
            _fdiv.text(_value);

            switch (_opt[1])
            {
                case "font-family":
                    _that.setFontFamily(_value);
                    break;
                case "font-size":
                    _that.setFontSize(_value);
                    break;
            }
            _that.canvas.renderAll();
        });
        //设置颜色
        /*EventBase.off('fontColorSelect')
        EventBase.on('fontColorSelect',function(){
            var _opt = arguments[1].value;
            $('#font_color_group').css('background-color',_opt);
            setSelectionStyle({fill:_opt},'setColor');
            arguments[1].value = null;
        });*/
        $('#font_color_group').off();
        $('#font_color_group').on('click',function(e){
            commands.closeToolsPop('openColor_div');
        })
        $('#openColor_div ul>li').off();
        $('#openColor_div ul>li').on('click', function (e) {
            var _dom = $(this).find('a').attr('style');
            if (!_dom) return;
            Global.isVariety = true;
            var _style = _dom.split(';')[0];
            commands.saveCtrlPath('object');
            if (_style.indexOf('background-color') >= 0) {
                _style = _style.match(/\#.+/)[0];
            }else{
                _style ="#cccccc";
            }
            setSelectionStyle({fill:_style},'setColor');
            $('#font_color_group').css('background-color',_style);
        })
        /**
         * 设置选择对象/区样式
         * @param obj
         */
        function setSelectionStyle(obj,method)
        {
            Global.isVariety = true;
            //setActiveStyle(styleName, value, object) {
                var object = Global.CurrentSlide.c.getActiveObject();
                if (!object)
                    return;
                if (object.setSelectionStyles && object.isEditing) {
                    var style = {};
                    //style[styleName] = value;
                    object.setSelectionStyles(obj);
                    object.setCoords();
                }
                else {
                    var _key =Object.keys(obj)[0];
                    var _value = obj[_key];
                    object[_key] = _value;
                }

                object.setCoords();
                _that.canvas.renderAll();
           // }
            return
            if(_that.isEditing)
            {
                if(!obj.fill) obj.fill='#000000';
                var obj = $.extend(_that.getSelectionStyles(),obj);
                var _te =false;
                for(var i in obj)
                {
                    if(i != 'fill' && obj[i]) _te =true;
                }
                if(!_te) delete obj.fill;
                _that.setSelectionStyles(obj);
                _that.setCoords();
            }else
            {
                var _key =Object.keys(obj)[0];
                var _value = obj[_key];
                eval(('_that.'+method+'("'+_value+'")'));
                //删除所选对象中对应的值
                var _style = JSON.stringify(_that.styles);
                var t = new RegExp('"'+_key+'":"#(\\w){6}"(,)*',"g");
                _style = _style.replace(t,'');
               _that.styles = JSON.parse(_style);
            }
            _that.canvas.renderAll();
        }
        var _fontFamily = $('#'+PROPERTY_BUTTON.font.family);
        var _fontSize = $('#'+PROPERTY_BUTTON.font.size);
        var _fontColor = $('#'+PROPERTY_BUTTON.font.color);
        var _fontStyle = $('#'+PROPERTY_BUTTON.font.style);
        var _fontAlign = $('#'+PROPERTY_BUTTON.font.align);

        //粗体
        _fontStyle.find('a:nth-of-type(1)').off();
        _fontStyle.find('a:nth-of-type(1)').on('click',function(e){
             var btn = $(this);
            var _active = btn.button('toggle');
            var _str = _active.attr('class').indexOf('active')>=0?'bold':'';
            setSelectionStyle({fontWeight:_str},'setFontWeight');
        });
        //斜体
        _fontStyle.find('a:nth-of-type(2)').off();
        _fontStyle.find('a:nth-of-type(2)').on('click',function(e){
            var btn = $(this);
            var _active = btn.button('toggle');
            var _str = _active.attr('class').indexOf('active')>=0?'italic':'';
            setSelectionStyle({fontStyle:_str},'setFontStyle');
        });
        //中横杆
        _fontStyle.find('a:nth-of-type(3)').off();
        _fontStyle.find('a:nth-of-type(3)').on('click',function(e){
            var btn = $(this);
            var _active = btn.button('toggle');
            var _str = _active.attr('class').indexOf('active')>=0?'line-through':'';
            setSelectionStyle({textDecoration:_str},'setTextDecoration');
        });
        //下横杆
        _fontStyle.find('a:nth-of-type(4)').off();
        _fontStyle.find('a:nth-of-type(4)').on('click',function(e){
            var btn = $(this);
            var _active = btn.button('toggle');
            var _str = _active.attr('class').indexOf('active')>=0?'underline':'';
            setSelectionStyle({textDecoration:_str},'setTextDecoration');
        });
        //左对齐
        _fontAlign.find('a:nth-of-type(1)').off();
        _fontAlign.find('a:nth-of-type(1)').on('click',function(e){
            clearState()
            Global.isVariety = true;
            var btn = $(this);
            var _active = btn.button('toggle');
            var _str = _active.attr('class').indexOf('active')>=0?'left':'left';
            _that.setTextAlign(_str);
            _that.canvas.renderAll();
        });
        //中对齐
        _fontAlign.find('a:nth-of-type(2)').off();
        _fontAlign.find('a:nth-of-type(2)').on('click',function(e){
            clearState()
            Global.isVariety = true;
            var btn = $(this);
            var _active = btn.button('toggle');
            var _str = _active.attr('class').indexOf('active')>=0?'center':'left';
            _that.setTextAlign(_str);
            _that.canvas.renderAll();
        });
        //右对齐
        _fontAlign.find('a:nth-of-type(3)').off();
        _fontAlign.find('a:nth-of-type(3)').on('click',function(e){
            clearState()
            Global.isVariety = true;
            var btn = $(this);
            var _active = btn.button('toggle');
            var _str = _active.attr('class').indexOf('active')>=0?'right':'left';
            _that.setTextAlign(_str);
            _that.canvas.renderAll();
        });
        //两边对齐
        _fontAlign.find('a:nth-of-type(4)').off();
        _fontAlign.find('a:nth-of-type(4)').on('click',function(e){
            clearState()
            Global.isVariety = true;
            var btn = $(this);
            var _active = btn.button('toggle');
            var _str = _active.attr('class').indexOf('active')>=0?'justify':'left';
            _that.setTextAlign(_str);
            _that.canvas.renderAll();
        });

        function clearState(){
            var _a = _fontAlign.find('a');
            for(var i=0;i<_a.length;i++)
            {
                $(_a[i]).removeClass('active');
            }
        }
        /**
         * 清除dom
         */
        function hideProperty(){
            commands.usePropertyCtrlArea(null);
            //$('#propertyArea').html('');
        }

        /**
         * 预设按钮状态
         * 只做部分
         */
        function setButtonState(){
            $('#font_family_group>button')[0].innerText = _that.fontFamily;
            $('#font_size_group>button')[0].innerText = _that.fontSize;
            $('#font_color_group').css('background-color',_that.fill);
        }
        Global.CurrentSlide.c.activeObject = _that;


        //设置动画按钮位置            ------------这里是弹出框的设置方式，先注释掉    luxing  2014-12-05
//        var _b = $('#animate-btn');
//        for (var s = 0; s < _b.length; s++) {
//            $(_b[s]).remove();
//        }
//        var _slide = _slide;
//        var input = $('<a href="javascript:void(0)" unselectable="none"  type="button" value="动画" id="animate-btn" class="btn_common"  style="position: absolute;">动画</a>');
//        $('body').append(input);
//
//        function getAbsoluteCoords(object) {
//            var _off = _slide._canvas.upperCanvasEl.getBoundingClientRect();
//            var _soffw = (object.width*object.scaleX + object.left) * DEFAULT_STAGE.scale;
//            var _soffh = (object.height + object.top) * DEFAULT_STAGE.scale;
//            return {
//                left: _off.left + _soffw,
////                left: object.left + _slide._canvas._offset.left,
//                top: _off.top + _soffh,
//                width: object.width * object.scaleX * Number(DEFAULT_STAGE.scale),
//                height: object.height * DEFAULT_STAGE.scale,
//                angle: object.angle
//            };
//        };
//        function setCss() {
//            var absCoords = getAbsoluteCoords(_that);
//            var _l = (absCoords.left - 60);
//            var _t = (absCoords.top - 30 - absCoords.height);
//            if (_l <= 10 || _l > window.outerWidth - 100 || _t <= 130) {
//                input.css('display', 'none');
//                return;
//            }
//            input.css('display', '');
//            input.css('left', _l + 'px');
//            input.css('top', _t + 'px');
//        };
//
//        _that.on('moving', function () {
//            setCss();
//        });
//        _that.on('scaling', function () {
//            setCss();
//        });
//        $('#animate-btn').off();
//        $('#animate-btn').on('click', function (e) {
//            Global.isVariety = true;
//            Global.isEditing = true;
//            Global.CurrentSlide.c && Global.CurrentSlide.c._canvas.discardActiveObject();
//            _that.showAnimateDialog();
//        });
//        setCss();
//    };
//
//    fabric.TextField.prototype.showAnimateDialog = function () {
//        isUpdate = false;
//        var isFadeInBtnChecked = false;
//        var slide = Global.CurrentSlide.c;
//        var aniInfo = slide.getAnimationInfo(this.id);
//        if (aniInfo) {
//            isUpdate = true;
//        }
//        var _self = this;
//
//        if ($('#modal-content')) {
//            $('#modal-content').remove();
//        }
//        var dlg = $('<div id="modal-content" style="padding-top: 75px;" class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog">' +
//            '<div class="modal-dialog">' +
//            '<div class="test_temp" style="width: 300px;height:220px;margin-left: 150px;">' +
//            '<div class="pop_tit">' +
//            '<span class="fl">动画设置</span>' +
//            '<div class="w_func fr"><a href="javascript:;" title="关闭" data-dismiss="modal" class="w_func_close"></a></div>' +
//            '</div>' +
//            '<div class="modal-body" style="height: 156px;">' +
//            '<div class="pop_con">' +
//            '<p class="pop_anim_con">' +
//            '<span id="btnNoAnimation" class="an_con_btn">' +
//            '<a href="#" title="无动画"><img src="share/assert/pop/images/ami-btn01.png" alt=""></a>' +
//            '<em>无动画</em>' +
//            '</span>' +
//            '<span id="btnFadeInAnimation" class="an_con_btn">' +
//            '<a href="#" title="淡入动画"><img src="share/assert/pop/images/ami-btn02.png" alt=""></a>' +
//            '<em>淡入动画</em>' +
//            '</span>' +
//            '</p>' +
//            '</div>' +
//            '</div>' +
//            '<div class="pop_btn_box">' +
//            '<a href="javascript:void(0)" class="pop_btn fr mrmt" id="btnFinish" title="完成"><span class="pop_btn_s2">完成</span></a>' +
//            '</div>' +
//            '</div>' +
//            '</div>' +
//            '</div>');
//        dlg.css("paddingTop", "75px");
//        $('body').append(dlg);
//
//        /**
//         * 关闭按钮
//         * 或使用modal('hide')关闭弹框时的回调
//         */
//        dlg.on('hidden.bs.modal', function (e) {
//            Global.isEditing = false;
//        });
//
//
//        /**
//         * 编辑完成
//         */
//        $("#btnFinish").click(function () {
//            var slide = Global.CurrentSlide.c;
//            if (isFadeInBtnChecked) {
//                aniInfo = {"id": _self.id, type: "fadeIn", "trigger": "mousedown", "attr": "opacity", "start": 0.02, "end": 1, "duration": 1000, "delay": 0};
//                slide.addAnimationInfo(aniInfo);
//            }else{
//                slide.delAnimationInfo(_self.id);
//            }
//            dlg.modal('hide');
//            setTimeout(function () {
//                dlg.remove();
//                $("body").find(".modal-backdrop.fade.in").remove();
//            }, 500);
//        });
//        dlg.modal({show: true, keyboard: false, backdrop: "static"});
//        if (isUpdate) {
//            if (aniInfo.type === "fadeIn") {
//                $("#btnFadeInAnimation").find("a").addClass("an_con_btn_checked");
//                isFadeInBtnChecked = true;
//            }
//        }else{
//            $("#btnNoAnimation").find("a").addClass("an_con_btn_checked");
//            isFadeInBtnChecked = false;
//        }
//
//        $("#btnNoAnimation").click(function(){
//            $(".pop_anim_con").find("a").removeClass("an_con_btn_checked");
//            $("#btnNoAnimation").find("a").addClass("an_con_btn_checked");
//            isFadeInBtnChecked = false;
//        });
//
//        $("#btnFadeInAnimation").click(function(){
//            $(".pop_anim_con").find("a").removeClass("an_con_btn_checked");
//            $("#btnFadeInAnimation").find("a").addClass("an_con_btn_checked");
//            isFadeInBtnChecked = true;
//        });
        //----------------------------------------------动画弹出框实现方式
    }

    /**
     *  隐藏设置按钮
     */
    fabric.TextField.prototype.hidePropertyButton = function(){
        commands.usePropertyCtrlArea(null);
    }

    fabric.TextField.prototype.hideEditButton = function () {
        this.off('moving');
        var _b = $('#animate-btn');
        for(var s=0;s<_b.length;s++)
        {
            $(_b[s]).remove();
        }
        this.hidePropertyButton && this.hidePropertyButton();
    }
});