# -*- encoding: UTF-8 -*-
 
 
module Yakumo_yukari
  class Count_limit
    attr_accessor :start , :limit , :add
    attr_accessor :add_plus
    attr_accessor :start_init
    attr_accessor :type
    attr_accessor :loopwait 
    attr_accessor :loopwait_wait 
    attr_accessor :loopwait_ex   
    attr_accessor :loopwait_ex_wait   
    attr_accessor :c
    attr_accessor :hs
    attr_accessor :msg #  mijissou
    def initialize hs = Hash.new
      @hs               = hs
      @start            = hs[:start] || 0
      @limit            = hs[:limit] || 0
      @add              = hs[:add]   || 1
      @add_plus         = hs[:add_plus] || 0
      @loopwait         = 0
      @loopwait_wait    = hs[:loopwait] || 0
      @loopwait_ex      = hs[:loopwait_ex] || []
      @loopwait_ex_wait = hs[:loopwait_ex_wait] || 0
      @type             = hs[:type] || nil
      @msg              = []
      @c = hs[:start_init] || @start
 
      case @type
      when :loop_rev
        if @add < 0
           @start , @limit = @limit , @start 
        end
        @func =->{ count_loop_rev }
      when :loop
        @func =->{ count_loop }
      when :count_limit
        @func =->{ count_limit }        
      else
        p :err
        exit
      end
    end
    
    def call
      @func.call
    end
 
    # 0...1...2...3...4...wait4...wait4...wait4...3...2...1...wait0...wait0
    def count_loop_rev
      if @loopwait > 1
        @loopwait -= 1
        return @c
      end
      @c += @add
      @add += @add_plus
      @add *= -1 if @c >= @limit || @c <= @start
      @c = @limit if @c >= @limit
      @c = @start if @c <= @start
      if (@c >= @limit) or (@c <= @start)
         @loopwait = @loopwait_wait 
      end
      if @loopwait_ex.include? @c
        if @loopwait_ex.class == Hash
          @loopwait = @loopwait_ex[ @c ]
        else
          @loopwait = @loopwait_ex_wait
        end
      end
      return @c
    end
  end
 
  class << self

    def loop_rev_create hs = Hash.new
      Count_limit.new({ type: :loop_rev, }.merge( hs ))
    end  
 
  end
end
 
 
 
a = Yakumo_yukari.loop_rev_create({
start: 0 ,
#start_init: 2 ,
limit: 10 ,
add: 1 ,
#loopwait: 3 ,
#loopwait_ex_wait: 3,
loopwait_ex: ({ 5 => 2 , 7 => 4 }) ,
#add_plus: 1 ,
})
 
puts "----loop_rev loopwait_ex----"
40.times do |i|
  printf "%3d  " % [ a.call ]
end
 
 