class Desktopapp::DesktopApp < Sinatra::Base
	
	#set :views, settings.root+'/desktop/'
	
	get "/desktop/desktop" do 
		erb :desktop
	end

	get "/desktop/app_manager" do
		erb :app_manager 
	end

	get "/desktop/sections" do
		erb :sections
	end

	get "/desktop/settings" do
		erb :settings
	end

	post "/desktop/save_desktop_settings" do
		@desktop = Desktop.find(params["desktopid"])
	    @savewhat = params["save"]
	    case @savewhat
	    when "theme"
	      @desktop.update_attributes(:theme => params["theme"])
	      @desktop.update_attributes(:wallpaper => nil)
	    when "desktopnames"
	      @sections = @desktop.sections
	      x = 0;
	      @sections.each do |section|
	        @desktopnewnames = params["desktopnms"]
	        section.update_attributes(:name => @desktopnewnames[x] )
	        x = x+1
	      end
	    when "appnewsection"
	      @section = Section.find(params["newsectionid"])
	      @groups = @section.groups
	      @app = Tile.find(params["appid"])
	      @groups.each do |group|
	        @tiles = group.tiles.where(:data_category.all => ["app"])
	        if @tiles.length < 12
	          @app.update_attributes(:group_id => group.id)
	          break
	        end
	      end
	      when "customtheme"
	        @desktop.update_attributes(:theme => "custom")
	        @desktop.update_attributes(:customtheme => params['theme'])
	      when "wallpaper"
	        @desktop.update_attributes(:wallpaper => params["wallpapernm"])
	      end
	     a = Array.new
	     a << {"success"=>"true"}
	     a.to_json
	end

	get "/desktop/get_desktop_settings" do
		@desktop = Desktop.find(params["desktopid"])
	     @getwhat = params["get"]
	     case @getwhat
	      when "desktop"
	        	 @desktop.to_json
	      when "sectionnames"
	        secnames = Array.new
	        @sections = @desktop.sections
	        @sections.each do |section|
	          secnames << section.name
	        end
	        secnames.to_json
	      when "theme"
	        @theme = @desktop.theme
	       	@theme.to_json
	     end
	end

	get "/desktop/getgroups" do
		 @section = Section.find(params["sectionid"])
	    @groups = @section.groups
	    a = Array.new
	    @groups.each do |group|
	      a << group.tiles
	    end
	    a.to_json
	end

	get "/desktop/getsectionlist" do
		@desktop = Desktop.find(params["desktopid"])
  		@sections = @desktop.sections
    	@sections.to_json 
	end

	get "/desktop/settingthemes" do
		 @themes = DesktopTheme.all
		 erb :'settings/themes'
	end

	get "/desktop/settingsections" do
		erb :'settings/sections'
	end

	get "/desktop/getapplist" do
		@desktop = Desktop.find(params["desktopid"])
	     @sections = @desktop.sections
	     a = Array.new
	     @sections.each do |section|
	       @groups = section.groups
	       @groups.each do |group|
	         @tiles = group.tiles.where(:data_category.all => ["app"])
	         a << @tiles
	       end
	     end
	     a.to_json
	end

	post "/desktop/newpositions" do
		@newpositions = params["newpos"]
	    @section = Section.find(params["sectionid"])
	    @groupids = params["groupids"]
	    @groups = @section.groups
	     z = 0
	    @newpositions.each do |grp|
	      x = 1
	      grp.each do |tileid|
	        if x != 1
	          y = 1
	          tileid.each do |id|
	            @tile = Tile.find(id)
	            @tile.update_attributes({:position => y})
	            if @tile.group_id != @groupids[z]
	              @tile.update_attributes({:group_id => @groupids[z]})
	            end
	             y = y + 1
	          end
	          z = z + 1
	        end
	        x = x + 1
	      end
	    end
	    b = Array.new
	    b << {"success"=>"true"}
	    b.to_json
	end

	get "/desktop/settingconnection" do
		erb :'settings/connections'
	end
end