module Mccloud::Provider
  module Aws

    class Image
      attr_accessor :provider
      attr_accessor :name

      def raw
      end

      def package(selection=nil,options=nil)

        if selection.nil?
          env.ui.info "[Error] We need at least a name of a machine."
          exit
        end


        #f=Fog::Compute.new({ :region => "eu-west-1", :provider => "AWS"})
        # i=f.create_image("i-c1ac2bb7","name","description")
        # f.images.all({ "Owner" => "self"})
        # f.deregister_image("ami-796d5b0d")

        on_selected_machines(selection) do |id,vm|

          time=Time.new
          strtime=time.strftime("%Y-%m-%d-%H-%M-%S")
          name=""

          if options["name"].nil?
            name="#{vm.name}-#{strtime}"
          else
            name=options["name"]
          end

          description=""
          if options["description"].nil?
            description="Autogenerated snapshot from machine #{vm.name}"
          else
            description=options["description"]
          end

          filter=@environment.config.mccloud.filter
          fullname="#{@environment.config.mccloud.filter}#{name}"

          provider= @environment.config.providers[vm.provider]
          begin
            result=provider.create_image(id,name,description)
            imageId=result.body["imageId"]
            env.ui.info "[#{vm.name}] image #{imageId} with #{description} being created"

            unless options["dontwait"]
              sleep 3
              counter=0
              max_tries=5
              #Pending
              state="pending"
              until state!="pending" do
                print "."
                sleep 2
                image=provider.images.get(imageId)

                if image.nil?
                  sleep 3
                  print "."
                else
                  state=image.state
                end
              end
              env.ui.info ""
              env.ui.info "[#{vm.name}] creation done!"
            end
          rescue Fog::Service::Error => fogerror
            env.ui.info "[Error] #{fogerror}"

          end
          #      vm.instance.start
        end
      end

      def register
        #f=Fog::Compute.new({ :region => "eu-west-1", :provider => "AWS"})
        # i=f.create_image("i-c1ac2bb7","name","description")
        # f.images.all({ "Owner" => "self"})
        # f.deregister_image("ami-796d5b0d")

        env.ui.info "Looking for imageId: #{imageId}"
        @environment.config.providers.each do |name,provider|
          begin
            image=provider.images.get(imageId)
            if image.nil?
              env.ui.info "[#{name}] - ImageId #{imageId} not found"
            else
              env.ui.info "[#{name}] - ImageId #{imageId} found"
              env.ui.info "[#{name}] - Deregistering #{imageId} now"
              begin
                provider.deregister_image(imageId)
              rescue Fog::Service::Error => fogerror
                env.ui.info "[Error] #{fogerror}"
              end
            end
          end
        end
      end
    end
  end
end
