class EncodeImage
  @@password = ""
  @@message  = ""

  @@img = nil

  @@utils = nil


  def initialize(p, m, i)
    @@password = p
    @@message  = m

    @@utils = ImageUtils.new(@@password, @@message, i)

    @@table = @@utils.generateLookupTable
  end

  def encode
    msg_bits = @@utils.getImage.getBits(@@message)
    msg_bits = msg_bits << 8 | 0
    msg_bits = msg_bits << 16 | @@message.length

    s = @@utils.getImage.getSize

    original_img = @@utils.getImage.fuzzImage

    @@table.each do |loc|
      pixel = @@utils.getImage.getPixel(loc[:x], loc[:y])
      msg_bits = pixel.encodeBits(msg_bits)
    end

    @@utils.getImage.saveImage
  end


# Need to properly sever this at some point...

  def decode
puts "Hacked in decode..."
    s = @@utils.getImage.getSize

    msg_bits = 0

    @@table.reverse.each do |loc|
      pixel = @@utils.getImage.getPixel(loc[:x], loc[:y])
      msg_bits = (msg_bits << 6) | pixel.decodeBits
    end

#this trash needs to move somewhere...
puts "Try to convert the message..."
    len = msg_bits & 0xffff
    msg_bits = msg_bits >> 16

    # check the padding bits....
    if (msg_bits & 255) != 0 then
puts "Padding failed scrutiny! "
puts msg_bits.to_s(2)
      return false
    end

    msg_bits = msg_bits >> 8

puts "Padding passed scrutiny..."
    msg_chars = []

    while msg_bits > 0 do
      byte = msg_bits & 0xff
      msg_chars.push(byte.chr)
      msg_bits = msg_bits >> 8
    end

    msg = msg_chars.reverse.join

    return msg
  end
end