#-*- coding: utf-8 -*-

require 'prawn'
require 'prawn/measurement_extensions'

module NosOignons
  class Receipt
    attr_reader :member, :amount

    def initialize(member, amount)
      @member = member
      unless member.up_to_date?
        raise ArgumentError.new("membership fee has not been paid for this year")
      end
      @amount = amount
      @now = Time.now
    end

    def filename
      dir = ENV['NOS_OIGNONS_RECEIPTS_DIR'] || 'receipts'
      "#{dir}/receipt-#{@member.member_id}-#{@member.membership_fee_paid_on.strftime("%Y-%m-%d")}.pdf"
    end

    WINDOW_LEFT = 110.mm
    WINDOW_TOP = 50.mm
    WINDOW_RIGHT = 25.mm
    WINDOW_BOTTOM = 80.mm
    WINDOW_WIDTH = 85.mm
    BODY_LEFT = 25.mm
    BODY_WIDTH = 210.mm - (BODY_LEFT * 2)
    FOOTER_HEIGHT = 25.mm
    DARK_LOGO_COLOR = '57075f'
    FONT_PATH = File.expand_path("../../../share/fonts/OpenSans/OpenSans.ttf", __FILE__)
    LOGO_PATH = File.expand_path("../../../share/images/nos-oignons.png", __FILE__)

    def generate_header(pdf)
      header_height = pdf.bounds.height / 6
      pdf.bounding_box([0, pdf.cursor], :width => pdf.bounds.width, :height => header_height) do
        logo_width = 2 * pdf.bounds.width / 3
        pdf.bounding_box([0, pdf.bounds.top], :width => logo_width, :height => pdf.bounds.height) do
          pdf.image LOGO_PATH, :width => pdf.bounds.width, :align => :center, :vposition => :center
        end
        pdf.bounding_box([logo_width, pdf.bounds.top], :width => pdf.bounds.width / 3, :height => pdf.bounds.height) do
          pdf.text 'Nœuds de sortie Tor financés par la communauté', :align => :center, :valign => :center
        end
      end
      pdf.bounding_box([WINDOW_LEFT - pdf.bounds.absolute_left, pdf.bounds.absolute_top - WINDOW_TOP], :width => WINDOW_WIDTH, :height => WINDOW_BOTTOM - WINDOW_TOP) do
        pdf.text member.name + "\n" + member.address, :valign => :bottom
      end
    end

    def generate_body(pdf)
      body_height = pdf.cursor - (pdf.bounds.height / 6)
      pdf.move_down 30
      pdf.bounding_box([BODY_LEFT, pdf.cursor], :width => BODY_WIDTH, :height => body_height) do
        pdf.font_size(16) do
          pdf.pad(25) do
            pdf.text "Objet : reçu de cotisation"
          end
          margin = pdf.bounds.width / 6
          pdf.pad_bottom(30) do
            pdf.bounding_box([WINDOW_LEFT - pdf.bounds.absolute_left, pdf.cursor], :width => WINDOW_WIDTH) do
              pdf.text "Le #{@now.strftime("%d/%m/%Y")}"
            end
          end
          pdf.text <<-EOT.gsub(/^            /, '').gsub(/\n/, ' ')
            Nous avons bien enregistré la cotisation annuelle de #{member.name}
            à l'association Nos oignons reçue à la date du
            #{member.membership_fee_paid_on.strftime("%d/%m/%Y")} pour un montant
            de #{amount}€.
          EOT
          pdf.pad_top(30) do
            pdf.bounding_box([margin, pdf.cursor], :width => 5 * margin) do
              pdf.text "Le Conseil d'Administration"
            end
          end
        end
      end
    end

    def generate_footer(pdf)
      pdf.bounding_box([0, FOOTER_HEIGHT + pdf.bounds.bottom], :width => pdf.bounds.width, :height => FOOTER_HEIGHT) do
        pdf.pad_bottom(5) do
          pdf.stroke_color DARK_LOGO_COLOR
          pdf.dash 5
          pdf.stroke_horizontal_rule
        end
        pdf.font_size(10) do
          pdf.column_box([0, pdf.cursor], :width => pdf.bounds.width, :columns => 2) do
            pdf.text NosOignons::CONTACT_INFO, :align => :center, :valign => :center
            pdf.bounds.move_past_bottom
            pdf.text NosOignons::POSTAL_ADDRESS, :align => :center, :valign => :center
          end
        end
      end
    end

    def create!
      raise "File exists!" if File.exists?(filename)
      Prawn::Document.generate(filename) do |pdf|
        pdf.font(FONT_PATH) do
          generate_header(pdf)
          generate_body(pdf)
          generate_footer(pdf)
	end
      end
    end
  end
end
