class MoveTableStarPdf < Prawn::Document
    include ActionView::Helpers::NumberHelper
    attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_width,:total_width,:item_description_width
    def initialize(printer_settings,to,from,shop_detail,date,type,moved_by,order_items)
    self.page_width = printer_settings.page_width
    self.page_height = printer_settings.page_height
    self.header_font_size = printer_settings.header_font_size.to_i
    self.item_font_size = printer_settings.item_font_size.to_i
    self.margin = 0
    self.price_width = 40 # No Need for item
    self.qty_width = 40
    self.total_width = 40  # No Need for item
    self.item_width = self.page_width - (self.qty_width - self.margin)
    self.item_height = 15
    self.item_description_width = self.page_width - (self.price_width + self.qty_width + self.total_width)
    self.label_width=90

    super(:margin => [self.margin, self.margin, self.margin, self.margin], :page_size => [self.page_width, self.page_height])

    # db font setup
    if printer_settings.font != ""
        font_families.update("#{printer_settings.font}" => {
            :normal => "public/fonts/#{printer_settings.font}.ttf",
            :italic => "public/fonts/#{printer_settings.font}.ttf",
            :bold => "public/fonts/#{printer_settings.font}.ttf",
            :bold_italic => "public/fonts/#{printer_settings.font}.ttf"
        })

        font "#{printer_settings.font}"
        fallback_fonts ["Courier", "Helvetica", "Times-Roman"]
    end

    header(printer_settings.name,type)

    call_move_table(to,from,date,type,moved_by)

    move_down 5
    stroke_horizontal_rule
    move_down 5

    add_lining_item(order_items, printer_settings.precision)

    move_down 5
    stroke_horizontal_rule
    move_down 5

  end

  def header (name,type)
    text "Move #{type}", :left_margin => -10, :size => self.header_font_size.to_i + 3,:align => :center
    # move_down self.item_height
    move_down 5
    stroke_horizontal_rule
    move_down 5
  end

  def call_move_table (to,from,date,type,moved_by)
    move_down 3
    text "Date Time : #{date.utc.getlocal.strftime("%Y-%m-%d %I:%M %p")}", :left_margin => -10, :size => self.header_font_size
    text "Change    [#{from}]   To  [#{to}]", :left_margin => -10, :size => self.header_font_size
    text "Moved By  : #{moved_by}", :left_margin => -10, :size => self.header_font_size
  end

  def add_lining_item(order_items, precision)
    y_position = cursor

    bounding_box([0,y_position], :width => self.item_width) do
        text "Item", :size => self.item_font_size,:align => :left
    end

    bounding_box([self.item_width,y_position], :width => self.qty_width) do
        text "Qty", :size => self.item_font_size,:align => :left
    end

    stroke_horizontal_rule
    move_down 5

    add_item(order_items, precision)
  end

    # Add order items under order info
    def add_item(order_items, precision)
        y_position = cursor

        move_down 5

        order_items.each do|odi|
          # check for item not to show
          # if odi.price != 0
            y_position = cursor

            # bounding_box([0,y_position], :width => self.item_width + 60, :height => self.item_height) do
            #     text "#{odi.item_code} - #{odi.item_name}", :size => self.item_font_size,:align => :left
            # end
            bounding_box([0,y_position], :width => self.item_width) do
                text "#{odi.item_code} - #{odi.item_name}", :size => self.item_font_size,:align => :left
            end

            bounding_box([self.item_width,y_position], :width => self.qty_width) do
                text "#{number_with_precision(odi.qty, :precision => precision.to_i)}", :size => self.item_font_size,:align => :left
            end

            bounding_box([0,y_position], :width => self.item_width) do
              text "#{odi.item_code} - #{odi.item_name}", :size => self.item_font_size,:align => :left
            end

            if !(odi.alt_name).empty?
                move_down 4
                # font("public/fonts/NotoSansCJKtc-Regular.ttf") do
                  text "(#{odi.alt_name})", :size => self.item_font_size,:align => :left, :inline_format => true
                # end
            end

        end
    end

end
