json.id menu.id
json.name menu.name
json.is_ordering menu.is_ordering
json.is_active menu.is_active
json.valid_time_from menu.valid_time_from.strftime("%H:%M")
json.valid_time_to menu.valid_time_to.strftime("%H:%M")

if (menu.menu_categories)
  order_by = Lookup.find_by_lookup_type("order_by")
  # if !order_by.nil? && order_by.value == "name"
  #   categories = MenuCategory.unscope(:order).where("menu_id ='#{menu.id}'").order("name asc")
  # else
  #   categories = menu.menu_categories
  # end
  categories = menu.menu_categories
  json.categories  categories do |category|
    if category.is_available
      json.sub_category category.children.present?

      valid_time = category.valid_time
      json.valid_time valid_time
      json.id category.id
      json.code category.code
      json.order_by category.order_by
      json.name category.name
      json.alt_name category.alt_name
      json.order_by category.order_by
      json.parent_id category.menu_category_id
      json.is_available category.is_available

      if !order_by.nil? && order_by.value == "name"
        menu_items = category.menu_items.sort_by(&:name)
      else
        menu_items = category.menu_items
      end

      if category.menu_items
        json.items menu_items do |item|
          if item.is_available
            # Format for attributes json
            attr_format = []
            # Format for attributes json
            if item.item_attributes.count > 0
              item_attributes = item.item_attributes.map(&:to_s)
              attr_format = @item_attributes.select { |x| item_attributes.include?(x.id.to_s) }.group_by {|att| att.attribute_type.strip }.map { |type, values| {type: type, values: values.map { |x| x.name.strip } } }
            end

            # Format for option json
            opt_format = []
            # Format for attributes json
            if item.item_options.count > 0
              item_options = item.item_options.map(&:to_s)
              opt_format = @item_options.select { |x| item_options.include?(x.id.to_s) }.group_by {|opt| opt.option_type.strip }.map { |type, values| {type: type, values: values.map { |x| x.name.strip } } }
            end

            #Menu Item Information
            json.id           item.id
            json.code         item.item_code
            json.name         item.name
            json.alt_name     item.alt_name
            json.image        item.image_path.url
            json.description  item.description
            json.information  item.information
            json.type         item.type
            json.account_id   item.account_id
            json.min_qty      item.min_qty
            json.is_available item.is_available
            json.is_sub_item  item.is_sub_item
            json.unit         item.unit

            # Item Sets of Menu Item
            json.item_sets item.item_sets do |its|
              json.id                 its.id
              json.name               its.name
              json.alt_name           its.alt_name
              json.min_selectable_qty its.min_selectable_qty
              json.max_selectable_qty its.max_selectable_qty
              json.instances          its.menu_item_instances.map { |i| {id: i.id} }
            end

            json.attributes attr_format
            json.options opt_format

            json.instances item.menu_item_instances do |is|
              if is.is_available
                # Convert id to name for attributes
                item_attributes = is.item_attributes.map(&:to_s)
                instance_attr = @item_attributes.select{ |x| item_attributes.include?(x.id.to_s) }.pluck(:name)

                json.id is.id
                json.code is.item_instance_code
                json.name is.item_instance_name
                json.price is.price
                json.is_available is.is_available
                json.is_default is.is_default
                json.is_on_promotion is.is_on_promotion
                json.promotion_price is.promotion_price
                json.out_of_stock is.is_out_of_stock
                json.values instance_attr
              end
            end

          end
        end
      end
    end
  end
end
