define(["ember","app","broad","fm","../../helpers/download_manage","../../helpers/fetch_state"],function(a,b,c,d,e,f){return b.AlbumsIndexController=a.ObjectController.extend({needs:["photoViewer"],histories:a.computed.alias("session.histories"),schoolyear:null,online:null,albums:[],categories:[a.Object.create({key:"gardenPersonalAlbums",code:5,name:"在园相册",method:"getGardenPersonalAlbums",classH:""}),a.Object.create({key:"classAlbums",code:1,name:"班级相册",method:"getClassAlbums",classH:""}),a.Object.create({key:"personalAlbums",code:2,name:"个人相册",method:"getPersonalAlbums",classH:""})],activeCategory:null,didActiveCategoryChanged:function(){var a=this;this.get("categories").forEach(function(b){b.set("classH",b==a.get("activeCategory")?"active":"")})}.observes("activeCategory"),filterCount:0,reloadModel:function(){if(2<=this.get("filterCount")&&this.get("schoolyear")){console.log("reloadModel --"),c.trigger("loading");var b=this,e=this.get("activeCategory"),g="~/%@-%@".fmt(this.get("schoolyear.schoolyear"),this.get("schoolyear.semester"));["fileLocal","finalAlbum","finalPhoto","finalAlbumLocal","finalPhotoLocal"].forEach(function(a){b.store.unloadAll(a)});var h=[this.store.find("fileLocal",{_path_:g})];h.addObject(this.get("session.online")?this.store.find("finalAlbum",{method:e.get("method"),schoolyear:b.get("schoolyear.schoolyear"),semester:b.get("schoolyear.semester")}).then(function(c){return c.setEach("type",e.get("code")),a.RSVP.Promise.all(c.map(function(a){return b.store.find("finalPhoto",{albumid:a.get("id")}).then(function(b){return a.get("photos").addObjects(b),a})}))}):this.store.find("finalAlbumLocal",{_path_:g,type:e.get("code")}).then(function(c){return a.RSVP.Promise.all(c.map(function(a){return b.store.find("finalPhotoLocal",{_path_:g,albumid:parseInt(a.get("id"))}).then(function(b){return a.get("photos").addObjects(b),a})}))})),a.RSVP.Promise.all(h).then(function(e){var g=e[1].map(function(c){var d=a.Object.create({entity:c,downAll:!1,photos:c.get("photos").map(function(c){var d=e[0].findBy("id",c.get("filepath"));return a.Object.create({entity:c,state:f.create({state:d?d.get("Status"):b.get("session.online")?"online":"loaded",error:d||b.get("session.online")?"":"error1"})})}).sortBy("entity.intime")});return b.get("session.online")&&d.set("downAll",0!=d.get("photos").filterBy("state.isOnline",!0).get("length")),d});if(!b.get("session.online")){var h=b.get("schoolyear.schoolyear")+"-"+b.get("schoolyear.semester");g=g.filter(function(a){return a.set("photos",a.get("photos").filter(function(a){return a.set("entity.filepath",d.achieveSync({folder:h,url:a.get("entity.filepath")})),a.get("state.isFetched")})),0!=a.get("photos.length")})}c.trigger("loaded"),b.set("albums",g.sortBy("entity.updatetime"))},function(a){console.log(a),c.trigger("loaded"),b.get("dialogManager").notice(a.message||a.toString())})}this.incrementProperty("filterCount")}.observes("online","schoolyear","activeCategory"),actions:{switchCategory:function(a){console.log(a),this.set("activeCategory",a)},download:function(a){var b=this;c.trigger("loading"),e.downloadPhotos(this.store,this.get("schoolyear.schoolyear"),this.get("schoolyear.semester"),[a.get("entity")]).then(function(){a.set("state.state","init");var c=b.get("albums").findBy("entity",a.get("entity.Album"));c.set("downAll",0!=c.get("photos").filterBy("state.isOnline",!0).get("length"))})["finally"](function(){c.trigger("loaded")})},downloadAlbum:function(a){c.trigger("loading");var b=a.get("photos").filterBy("state.isOnline",!0);e.downloadPhotos(this.store,this.get("schoolyear.schoolyear"),this.get("schoolyear.semester"),b.mapBy("entity")).then(function(){b.setEach("state.state","init"),a.set("downAll",!1)})["finally"](function(){c.trigger("loaded")})},view:function(a){var b=this,c=this.get("controllers.photoViewer"),d=this.get("albums").findBy("entity",a.get("Album")).get("photos").map(function(a){return{id:a.get("entity.id"),name:a.get("entity.filename"),url:b.get("session.online")&&!b.get("schoolyear.subscribe")?a.get("entity.filepath")+"@800w_600h_100q":a.get("entity.filepath")}}),e=d.findBy("id",a.get("id")),f={width:830,height:676,title:"相册："+a.get("Album.sName"),photos:d,photo:e};this.get("dialogManager").custom("photo/viewer",c,f),c.set("dialogid",Math.random())}}})});