define([
    'dojo/_base/declare',
    'xide/factory',
    'xide/types',
    'xapp/manager/ManagerBase',
    'xfile/views/RemoteEditor'

], function (declare,
             factory,
             types,
             ManagerBase,RemoteEditor)
{
    /***
     *
     * VERY EXPERIMENTAL : PLEASE IGNORE this plug-in !
     */
    return declare("ImageEdit.xfile.ImageEditManager", [ManagerBase],
        {
            mainView:null,
            ctx:null,
            config:null,
            panelManager:null,
            fileManager:null,
            imageEditView:null,
            currentItem:null,
            imageEditorPane:null,
            imageEditorPaneContainer:null,
            featherEditor:null,
            onEditorClose:function(editor){
                this.featherEditor=editor;
                this.imageEditorPaneContainer.removeChild(this.imageEditorPane);
            },
            onImageSaved:function(newUrl){
                var fileManager = this.fileManager || xfile.getContext().getFileManager();
                if(fileManager){
                    fileManager.downloadTo(newUrl,this.currentItem.path);
                }
            },
            getFileName:function(){
                var imageUrl = this.config.REPO_URL + '/' +this.currentItem.path;
                imageUrl=imageUrl.replace('./','/');
                return imageUrl;
            },
            getMainView:function(){
              return this.mainView || this.panelManager.rootView;
            },
            onItemSelected:function(eventData){
                this.currentItem = eventData.item;
            },
            openEditor:function(item){

                var mainView = this.getMainView();
                if(!mainView){
                    return;
                }
                var dstContainer = mainView.getNewAlternateTarget();
                if(!dstContainer){
                    return;
                }
                this.imageEditorPaneContainer=dstContainer;

                var imageUrl = this.config.REPO_URL + '/' +item.path;
                imageUrl=imageUrl.replace('./','/');

                var _container = new dijit.layout.ContentPane({
                    title:item.name,
                    closable:true,
                    style:'padding:0px;margin:0px;overflow:hidden;'
                },dojo.doc.createElement('div'));

                dstContainer.addChild(_container);
                dstContainer.selectChild(_container);
                dstContainer.resize();
                mainView.resize();

                dstContainer=_container;
                this.imageEditorPane=_container;

                /*this.imageEditView=null;*/
                if (this.imageEditView){
                    xapp.utils.destroyWidget(this.imageEditView);
                    this.imageEditView=null;
                }
                if (!this.imageEditView) {
                    this.imageEditView = new RemoteEditor({
                        selected:true,
                        delegate:this,
                        options:{},
                        config:this.config,
                        frameUrl:require.toUrl("ImageEdit/xfile/templates/Aviary.html"),
                        editUrl:imageUrl,
                        parentContainer:dstContainer
                    },dojo.doc.createElement('div'));

                    dstContainer.containerNode.appendChild(this.imageEditView.domNode);
                }
                this.imageEditView.startup();
            },
            openPixlrEditor:function(item){

                var mainView = this.getMainView();
                if(!mainView){
                    return;
                }
                var dstContainer = mainView.getNewAlternateTarget();
                if(!dstContainer){
                    return;
                }
                this.imageEditorPaneContainer=dstContainer;

                var imageUrl = this.config.REPO_URL + '/' +item.path;
                imageUrl=imageUrl.replace('./','/');

                var thiz=this;
                var _container = new dijit.layout.ContentPane({
                    title:item.name,
                    closable:true,
                    style:'padding:0px;margin:0px;overflow:hidden;'
                },dojo.doc.createElement('div'));

                dstContainer.addChild(_container);
                dstContainer.selectChild(_container);
                dstContainer.resize();
                mainView.resize();

                dstContainer=_container;
                this.imageEditorPane=_container;

                if (this.imageEditView){
                    xapp.utils.destroyWidget(this.imageEditView);
                    this.imageEditView=null;
                }
                if (!this.imageEditView) {
                    this.imageEditView = new RemoteEditor({
                        selected:true,
                        delegate:this,
                        options:{},
                        config:this.config,
                        frameUrl:require.toUrl("ImageEdit/xfile/templates/Pixlr.html"),
                        editUrl:imageUrl,
                        saveUrl:this.config.FILE_SERVICE_FULL + '?service=Xapp_FileService.fileUpdate&callback=asdf&srcPath='+item.path,
                        parentContainer:dstContainer,
                        title:item.name
                    },dojo.doc.createElement('div'));

                    dstContainer.containerNode.appendChild(this.imageEditView.domNode);
                }
                this.imageEditView.startup();

            },
            onMainViewReady:function(){
                var thiz=this;
                factory.publish(types.EVENTS.REGISTER_EDITOR,{
                    name:'Aviary',
                    extensions:'jpeg|jpg|gif|png',
                    onEdit:function(){thiz.openEditor(thiz.currentItem)},
                    iconClass:'el-icon-brush',
                    owner:thiz
                },thiz);

                factory.publish(types.EVENTS.REGISTER_EDITOR,{
                    name:'Pixlr',
                    extensions:'jpeg|jpg|gif|png',
                    onEdit:function(){thiz.openPixlrEditor(thiz.currentItem)},
                    iconClass:'el-icon-brush',
                    owner:thiz
                },thiz);
            },
            _registerListeners:function () {
                this.inherited(arguments);
                factory.subscribe(types.EVENTS.ITEM_SELECTED,this.onItemSelected,this);
                var thiz=this;
                factory.subscribe(types.EVENTS.ON_MAIN_VIEW_READY,function(){
                    thiz.onMainViewReady();
                },thiz);
            },
            constructor:function () {
                this._registerListeners();
            }
        });
});