define(["ember","mm","fm","appuser"],function(a,b,c,d){return{clearLocalGrowth:function(b,c,d,e){var f=e.get("id"),g="~/%@-%@".fmt(c,d);return b.find("finalTemplateLocal",{_path_:g,_id:f}).then(function(c){return a.RSVP.Promise.all(c.map(function(a){return a.set("_path_",g),a.destroyRecord()})).then(function(){return a.RSVP.Promise.all([b.find("finalStaticRelateLocal",{_path_:g,TemplateID:f}),b.find("finalRelateBaseLocal",{_path_:g,TemplateID:f}),b.find("finalTemplatePageLocal",{_path_:g,TemplateID:f}),b.find("finalTopicLocal",{_path_:g,TemplateID:f})]).then(function(b){var c=[];return b.forEach(function(a){c.addObjects(a)}),a.RSVP.Promise.all(c.map(function(a){return a.set("_path_",g),console.log(a.toJSON()),a.destroyRecord()}))})})})},downloadGrowthFiles:function(b,c,d,e,f,g,h){var i=this,j={Category:"growth",Tag1:e.get("id")},k=[];return e.get("Coverimg")&&k.addObject(i._downloadFile(b,c,d,e.get("Coverimg"),a.$.extend({FileType:"image",Tag2:"tmplBG"},j))),e.get("Backgroundsound")&&k.addObject(i._downloadFile(b,c,d,e.get("Backgroundsound"),a.$.extend({FileType:"audio",Tag2:"tmplBGS"},j))),k.addObjects(f.map(function(e){return e.get("Background")?i._downloadFile(b,c,d,e.get("Background"),a.$.extend({FileType:"image",Tag2:"pageBG",Tag3:e.get("id")},j)):void 0})),k.addObjects(g.map(function(e){return"Icon"==e.get("StatictopictypeCode")?i._downloadFile(b,c,d,e.get("Text"),a.$.extend({FileType:"image",Tag2:"icon",Tag3:e.get("id")},j)):void 0})),h.forEach(function(f){var g=JSON.parse(f.get("JsonData")),h={Category:"growth",Tag1:e.get("id"),Tag2:"topic",Tag3:f.get("id")};g.AideoUrl&&k.addObject(i._downloadFile(b,c,d,g.AideoUrl,a.$.extend({FileType:"audio"},h))),a.isEmpty(g.Data)||("Image"==f.get("TopictypeCode")||"MultImage"==f.get("TopictypeCode")?k.addObjects(g.Data.map(function(e){return i._downloadFile(b,c,d,e.ImageUrl,a.$.extend({FileType:"image"},h))})):"Video"==f.get("TopictypeCode")?k.addObjects(g.Data.map(function(e){return i._downloadFile(b,c,d,e.VideoUrl,a.$.extend({FileType:"video"},h))})):"Aideo"==f.get("TopictypeCode")&&k.addObjects(g.Data.map(function(e){return i._downloadFile(b,c,d,e.AideoUrl,a.$.extend({FileType:"audio"},h))})))}),a.RSVP.Promise.all(k)},downloadGrowth:function(b,c,d,e){var f=this,g=e.get("id"),h=[];return f.clearLocalGrowth(b,c,d,e).then(function(){return a.RSVP.Promise.all([b.find("finalTemplatePage",{schoolyear:e.get("Term"),semester:e.get("Session"),templateid:g}).then(function(d){return a.RSVP.Promise.all(d.map(function(a){return b.find("finalTopic",{schoolyear:c,templateid:g,pageid:a.get("id")}).then(function(a){h.addObjects(a.toArray())})})).then(function(){return d})}),b.find("finalStaticRelate",{schoolyear:c,templateid:g}),b.find("finalRelateBase",{schoolyear:c,templateid:g}),b.find("growthHistory")]).then(function(a){var g=a[3].find(function(a){return e.get("Term")==a.get("Term")&&e.get("Session")==a.get("Session")});g&&e.set("lastpublishtime",g.get("LastUpdatedDate"));var i=[].concat(h).concat(a[0].toArray()).concat(a[1].toArray()).concat(a[2].toArray());return f._pushRecords(b,c,d,i).then(function(){return f.downloadGrowthFiles(b,c,d,e,a[0],a[1],h)}).then(function(){return f._pushRecord(b,c,d,e)})})})},downloadPhotos:function(b,c,d,e){var f=this,g=e.map(function(a){return a.get("Album")});return g.addObjects(e),this._pushRecords(b,c,d,g).then(function(){return a.RSVP.Promise.all(e.map(function(a){return f._downloadFile(b,c,d,a.get("filepath"),{FileType:"image",Category:"album",Tag1:a.get("album.id")})}))})},downloadVideos:function(a,b,c,d){var e=this;return this._pushRecords(a,b,c,d).then(function(){e._downloadFiles(a,b,c,d.map(function(a){return a.get("FilePath")}),{FileType:"video",Category:"classvideo",Tag1:""})})},_pushRecords:function(b,c,d,e){var f=this;return a.RSVP.Promise.all(e.map(function(a){return f._pushRecord(b,c,d,a)}))},_pushRecord:function(b,c,d,e){var f=a.get(e,"constructor").typeKey+"Local",g=a.$.extend({},e.toHash(),{id:e.get("id"),_path_:"~/%@-%@".fmt(c,d)}),h=b.getById(f,e.get("id"));return h?void 0:b.createRecord(f,g).save()},_downloadFiles:function(b,c,d,e,f){var g=this;return a.RSVP.Promise.all(e.map(function(a){return g._downloadFile(b,c,d,a,f)}))},_downloadFile:function(e,f,g,h,i){var j=e.getById("fileLocal",h);if(j)return"fetched"!=j.get("Status")&&b.post({url:j.get("id")},{module:"fileManager",action:"resume"}),j;var k=new Date;e.createRecord("fileLocal",a.$.extend({id:h,_path_:"~/%@-%@".fmt(f,g),SchoolYear:f,Semester:g,PersonID:d.get("PersonID"),Status:"init",_createdby:d.get("PersonID"),_created_date:k,_updatedby:d.get("PersonID"),_updated_date:k},i)).save().then(function(){return c.downloadFM({folder:f+"-"+g,url:h}).then(function(){console.log("downloadFM success: "+h)}),j})}}});