class DiningCharge < ApplicationRecord
  belongs_to :table
  belongs_to :room

  def self.amount_calculate(dining_charges_obj, checkin , checkout)
    # note :: the first Charge Block will cost all, the Time rounding block will included in 2nd Charge Block
    if !checkin.nil? && !checkout.nil? && !dining_charges_obj.nil?
      block_count    = 0
      price          = 0
      minutes        = DiningCharge.time_diff(checkout, checkin)
      free_time      = DiningCharge.convert_to_minutes(dining_charges_obj.minimum_free_time.utc.strftime('%H:%M'))
      dining_minutes = minutes #- free_time # stayminutes - free minutes
      if dining_minutes <= free_time
        price = 0
      else
        charge_type = dining_charges_obj.charge_type
        if charge_type == 'hr'
          block_count, price = DiningCharge.charges(dining_charges_obj, dining_minutes, 'hr')
        elsif charge_type == 'day'
          block_count, price = DiningCharge.charges(dining_charges_obj, dining_minutes, 'day')
        end
      end
      return block_count, price
    else
      return 0, 0
    end
  end

  # dining charges calculate
  def self.charges(chargesObj, dining_minutes, type)
    solid_price = 0

    charge_block = DiningCharge.convert_to_minutes(chargesObj.charge_block.utc.strftime('%H:%M'))

    result = dining_minutes / charge_block
    rounding_time = DiningCharge.convert_to_minutes(chargesObj.time_rounding_block.utc.strftime('%H:%M'))
    if result.to_i < 1
      # for dining minute is under charge_block
      if dining_minutes > rounding_time
        rounding_block = dining_minutes / rounding_time

        solid_price = rounding_block * chargesObj.time_rounding_block_price
        return 1, solid_price,chargesObj.unit_price

      else
        return 1, result.to_i,chargesObj.unit_price
      end
    elsif result.to_i >= 1

       solid_price = result * chargesObj.unit_price

       remain_value = dining_minutes % charge_block

       # rounding_time = DiningCharge.convert_to_minutes(chargesObj.time_rounding_block.utc.strftime('%H:%M'))

       roundingblock = remain_value / rounding_time

       extra_minutes = remain_value % rounding_time

       if roundingblock.to_i < 1
          # no time rounding block
          return result.to_i, DiningCharge.check_rounding(chargesObj, solid_price, extra_minutes)
       else
          solid_price += (roundingblock * chargesObj.time_rounding_block_price)

          return result.to_i, DiningCharge.check_rounding(chargesObj, solid_price, extra_minutes)
       end
    end
  end

  # check for rounding and calculate with rounding price
  def self.check_rounding(chargesObj,solid_price, extra_minutes)
    # rounding_block_remain = roundingblock / 1
    free_time      = DiningCharge.convert_to_minutes(chargesObj.minimum_free_time.utc.strftime('%H:%M'))
    rounding_block_remain = extra_minutes - free_time

    if chargesObj.time_rounding == "down"
     return solid_price
    else
      # check and calc for time rounding block for up
      if rounding_block_remain > 0
        return solid_price += chargesObj.time_rounding_block_price
      else
        return solid_price
      end
    end
  end

  def self.time_diff(start_time, end_time)
    seconds_diff = (start_time - end_time).to_i.abs
    hours = seconds_diff / 3600
    seconds_diff -= hours * 3600

    minutes = seconds_diff / 60
    seconds_diff -= minutes * 60

    seconds = seconds_diff
    return hours * 60 + minutes
  end

  def self.convert_to_minutes(time)
    arr = time.split(":")
    hour = arr[0].to_i * 60
    return hour + arr[1].to_i
  end
end
