module CustomsReceipt
  module Generate
    def run(receiptable)
      options = {
        process_timeout: 30,
        browser_options: {
          "no-sandbox" => nil,
        },
      }

      options = options.merge(browser_path: "/usr/bin/chromium") if Rails.env.production?

      FerrumPdf.browser(**options)

      FerrumPdf.render_pdf(
        html: CustomsReceiptTemplate.new(receiptable).call,
        pdf_options: {
          print_background: true,
        },
      )
    end

    def write_mock_receipt
      File.write("foo.html", CustomsReceiptTemplate.new(mock_receiptable).call)
    end

    module_function :write_mock_receipt, :run

    class CustomsReceiptTemplate < Phlex::HTML
      include ActiveSupport::NumberHelper

      def initialize(receiptable)
        @r = receiptable
      end

      def cell(align = :left, style = {})
        div class: "cell", align:, style: do
          yield
        end
      end

      def row
        div class: "row" do
          yield
        end
      end

      def info(text = "")
        div class: "info" do
          plain text if text.present?
          yield if block_given?
        end
      end

      def view_template
        doctype
        html do
          head do
            meta charset: "UTF-8"
            style do
              #
              raw safe <<~CSS
                         body {
                             padding: 16px;
                             color: black;
                             font-family: sans-serif;
                         }
                         h4 {
                             font-weight: 400
                         }
                         .table {
                             display: grid;
                             grid-template-columns: 1fr auto auto;
                             width: 100%
                         }
                         .table > .header {
                             display: contents;
                         }
                         .table .cell {
                             padding: .5px 8px
                         }
                         .table > .header > .cell {
                             background: #000;
                             color: #fff;
                             font-weight: 400;
                             text-align: left;
                         }
                         .table > .row {
                             display: contents;
                         }
                         .table > .row:nth-child(odd) *
                         {
                             background-color: #ececec;
                         }
                         td {
                             border: none;
                         }
                         img.emoji {
                             margin: 0 .05em 0 .1em;
                             vertical-align: -0.1em;
                         }
                         div.info {
                             margin-top: .54em;
                         }
                         .footer {
                             font-size: 10pt;
                             color: #999999;
                             position: fixed;
                             bottom: 0px;
                             width: 100%;
                             white-space: nowrap;
                         }
                         pre {
                             white-space: pre-wrap;
                             font-family: sans-serif;
                             margin-top: 4px;
                         }
                       CSS
            end
            title { "Customs Receipt – #{@r.order_number}" }
          end
          body do
            img height: 64, style: { display: "inline-block", float: "left" }, src: HC_LOGO, class: "emoji"
            div style: { display: "inline-block", padding_left: "10px" } do
              b { "Hack Club" }
              br
              plain "15 Falls Rd "
              br
              plain "Shelburne, VT 05482 "
              br
              plain "United States"
            end
            br
            br
            br
            b { "Shipment ID:" }
            plain " " + @r.order_number
            br
            br
            div style: { display: "inline-block" } do
              b { "Ship to: " }
              pre { @r.recipient_address }
            end
            br
            br
            b { "Contents:" }
            div class: "table" do
              div class: "header" do
                cell { "Item" }
                cell(:right) { "Quantity" }
                cell(:right) { "Value/pc" }
              end
              @r.contents.each do |item|
                row do
                  cell { item.name }
                  cell(:right) { item.quantity }
                  cell(:right) { number_to_currency item.value }
                end
              end
              row do
                cell(:right, grid_column: "span 3") do
                  "Total: #{number_to_currency @r.total_value}"
                end
              end
            end
            if @r.shipping_cost.present?
              info "Shipping cost: #{number_to_currency(@r.shipping_cost)}" do
                plain " via #{@r.carrier}" if @r.carrier.present?
              end
            end
            if @r.tracking_number.present?
              info "Original tracking number: #{@r.tracking_number}"
            end
            if @r.additional_info.present?
              br
              info raw(safe(@r.additional_info))
            end
            info do
              plain "All amounts are given in USD."
              br
              br
              b { "N.B." }
              plain "This shipment is a gift. Valuations are for customs purposes only."
            end
            p(class: "footer") { "Hack Club is a 501(c)(3) public charity. Our nonprofit EIN is 81-2908499." }
          end
        end
      end

      HC_LOGO = "data:image/png;base64,[encoded data, 32308 characters]"
    end
  end
end
