define(["ember","mm","fm","core/utilities/oss.helper","core/utilities/path.helper","core/utilities/math.helper","core/components/bauble-editor/models/page"],function(a,b,c,d,e,f,g){return{fetchPages:function(b,c,d){var e=this,f=c.get("Term"),g=c.get("Session");["finalTemplatePage","finalRelateBase","finalStaticRelate","finalTopic"].forEach(function(a){b.unloadAll(a+(d?"":"Local"))});var h="~/%@-%@".fmt(f,g),i=[];return i=d?[b.find("finalTemplatePage",{schoolyear:f,semester:g,templateid:c.get("id")}),b.find("finalRelateBase",{schoolyear:f,templateid:c.get("id")}),b.find("finalStaticRelate",{schoolyear:f,templateid:c.get("id")})]:[b.find("finalTemplatePageLocal",{_path_:h,TemplateID:c.get("id")}),b.find("finalRelateBaseLocal",{_path_:h,TemplateID:c.get("id")}),b.find("finalStaticRelateLocal",{_path_:h,TemplateID:c.get("id")})],a.RSVP.Promise.all(i).then(function(c){return a.RSVP.Promise.all(c[0].sortBy("Pageindex").map(function(a){return e.fetchPage(b,a,f,g,d)}))})},fetchPage:function(b,d,e,f,h){var i=this,j=g.create({mode:"preview"});if(!d)return a.RSVP.Promise.resolve(j);var k=e+"-"+f;j.setProperties({id:d.get("id"),title:d.get("Pagetitle"),background:d.get("Background")}),d.get("Background")&&j.createShape("background",{data:{resource:h?d.get("Background"):c.achieveSync({folder:k,url:d.get("Background")})}});var l="~/%@-%@".fmt(e,f),m=[];return m=h?[b.find("finalTopic",{schoolyear:e,templateid:d.get("TemplateID"),pageid:d.get("id")})]:[b.find("finalTopicLocal",{_path_:l})],a.RSVP.Promise.all(m).then(function(){return b.all("finalRelateBase"+(h?"":"Local")).filter(function(a){return d.get("TemplateID")==a.get("TemplateID")&&d.get("id")==a.get("PageID")}).forEach(function(a){var c=b.getById("finalTopic"+(h?"":"Local"),a.get("TopicID"));if(c){var d=i.convertData(c.get("TopictypeCode"),c.get("JsonData"));h||(d=i.getLocalData(e+"-"+f,c.get("TopictypeCode"),d)),j.createShape("topic",{id:c.get("id"),properties:c,data:d,style:{id:a.get("id"),x:a.get("Posx"),y:a.get("Posy"),width:a.get("Width"),height:a.get("Height"),rotate:a.get("Rotate"),zIndex:a.get("Zindex"),color:a.get("FontColor"),fontFamily:a.get("FontFamily"),fontSize:a.get("FontSize"),fontWeight:a.get("FontWeight"),lineHeight:a.get("LineHeight")}})}}),b.all("finalStaticRelate"+(h?"":"Local")).filter(function(a){return d.get("TemplateID")==a.get("TemplateID")&&d.get("id")==a.get("PageID")}).forEach(function(b){j.createShape(b.get("StatictopictypeCode").toLowerCase(),{id:b.get("id"),properties:a.Object.create({id:b.get("StatictopicID")}),data:a.Object.create({resource:h?b.get("Text"):"Icon"==b.get("StatictopictypeCode")?c.achieveSync({folder:k,url:b.get("Text")}):b.get("Text")}),style:{id:b.get("id"),x:b.get("Posx"),y:b.get("Posy"),width:b.get("Width"),height:b.get("Height"),rotate:b.get("Rotate"),zIndex:b.get("Zindex"),color:b.get("FontColor"),fontFamily:b.get("FontFamily"),fontSize:b.get("FontSize"),fontWeight:b.get("FontWeight"),lineHeight:b.get("LineHeight")}})}),j})},getLocalData:function(b,d,e){var f=e.get("resource");if(!a.isEmpty(f))switch(d){case"Image":f.set("ImageUrl",c.achieveSync({folder:b,url:f.get("ImageUrl")}));break;case"Video":f.set("VideoUrl",c.achieveSync({folder:b,url:f.get("VideoUrl")}));break;case"Aideo":f.set("AideoUrl",c.achieveSync({folder:b,url:f.get("AideoUrl")}));break;case"MultImage":f.forEach(function(a){a.set("ImageUrl",c.achieveSync({folder:b,url:a.get("ImageUrl")}))})}return e},convertData:function(b,c){console.log(c);var d=JSON.parse(c),e=null;d.Data&&(e=d.Data,delete d.Data);var f=a.Object.create(a.$.extend({resource:null},d));if(!a.isEmpty(e)){switch(b){case"Text":e=e[0].Text;break;case"Image":case"Video":case"Aideo":e=a.Object.create(e[0]);break;case"MultImage":case"TableScore":case"TableAssess":case"TableMultiple":e=e.map(function(b){return a.Object.create(b)})}console.log(e),f.set("resource",e)}return f}}});