# frozen_string_literal: true

# Copyright 2017 Tyler Barker

#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at

#       http://www.apache.org/licenses/LICENSE-2.0

#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

require './config/grilld_emoji'
require 'date'
require 'time'

# Parser Helper Functions
module ParserHelpers
  def get_day(day, month)
    # Solution for users submitting rosters for incoming year
    current_month = DateTime.now.month
    year = DateTime.now.year
    year += 1 if current_month == 12 && month.to_i < 11
    shift_day = DateTime.new(year, month.to_i, day.to_i)

    shift_day.strftime('%a')
  end

  def unspecified?(station)
    case station
    when 'TM' then true
    when 'TL' then true
    when 'ARM' then true
    when 'RM' then true
    when 'TG' then true
    when 'SWAT' then true
    else false
    end
  end

  def get_station_emoji(station)
    return 'Unspecified' if unspecified?(station)
    return MULTI if station.include? '/'
    return SHOE if station.include? 'Floor'
    return CLOUD if station.include? 'Floater'
    return MONEY if station.include? 'Till'
    return DRESS if station.include? 'Dress'
    return GIFT if station.include? 'Wrap'
    return CHIPS_BREAD if station.include? 'Ch&Buns'
    return CHIPS if station.include? 'Chips'
    return BREAD if station.include? 'Buns'
    return BREAD if station.include? ' Bun' # strange edge case
    return FIRE if station.include? 'Grill'
    return HAND_WAVE if station.include? 'Host'
    return PLATE if station.include? 'Dish'
    return BEER if station.include? 'Bar'
    return DROPLET if station.include? 'Wash'
    return BENTO if station.include? 'Prep'
    return SPARKLE if station.include? 'Clean'
    return COMBI if station.include? 'Combi'
  end

  def get_type_emoji(type)
    case type
    when 'RM' then CROWN
    when 'ARM' then TROPHY
    when 'TL' then STAR
    when 'TM' then BURGER
    when 'TG' then BABY
    when 'SWAT' then POPO
    end
  end

  def calc_totals(s_times, e_times)
    size = s_times.length - 1
    breaks = 0
    total_hrs = 0
    all_totals = []
    (0..size).each do |i|
      s_times[i] = s_times[i].gsub(/:15/, '.25')
      s_times[i] = s_times[i].gsub(/:30/, '.50')
      s_times[i] = s_times[i].gsub(/:45/, '.75')
      e_times[i] = e_times[i].gsub(/:15/, '.25')
      e_times[i] = e_times[i].gsub(/:30/, '.50')
      e_times[i] = e_times[i].gsub(/:45/, '.75')
      # if shift exceeds 5.5hrs add break
      shift_length = e_times[i].to_f - s_times[i].to_f
      breaks += 0.5 if shift_length > 5
      total_hrs += shift_length
    end
    # calculate break total
    break_total = total_hrs - breaks

    # add totals to collection
    break_value = breaks.positive? ? break_total : 'N/A'
    all_totals << total_hrs
    all_totals << break_value
    all_totals
  end

  def calc_std_shifts(data, size)
    shifts = []
    (0..size).each do |i|
      day_date = "#{get_day(data.days[i], data.months[i])}"\
                 " #{data.days[i]}/#{data.months[i]}"
      start = Time.parse(data.s_times[i]).strftime('%I:%M%p')
      finish = Time.parse(data.e_times[i]).strftime('%I:%M%p')
      staff_emoji = get_type_emoji(data.types[i])
      shifts << "#{day_date} #{start}-#{finish} #{staff_emoji}"
    end
    shifts
  end

  def calc_long_shifts(data, size)
    shifts = []
    (0..size).each do |i|
      day_date = "#{get_day(data.days[i], data.months[i])}"\
                 " #{data.days[i]}/#{data.months[i]}"
      start = Time.parse(data.s_times[i]).strftime('%I:%M%p')
      finish = Time.parse(data.e_times[i]).strftime('%I:%M%p')
      # remove numbers in arguments
      emoji_value = data.stations[i].gsub(/[0-9]|[1][0-9]/, '')
      staff_emoji = get_type_emoji(data.types[i])
      station_emoji = get_station_emoji(emoji_value)
      shifts << "#{day_date} #{start}-#{finish}\n#{staff_emoji}"\
                " @#{data.stations[i].gsub(/A*[TR][MLG]@|A*[TR][MLG]/, ' ')}"\
                "#{station_emoji}"
    end
    shifts
  end

  def pad_time(time)
    if time.to_i < 10
      time.prepend('0')
      time << ':00'
    elsif time.to_i < 24
      time << ':00'
    end
  end

  def check_validity(months, days, location, s_times, e_times)
    return false if months[0].nil?
    return false if days[0].nil?
    return false if location[0].nil?

    s_times.length == e_times.length
  end
end
