#
# DEPRECATED
#
# ditching this for my own custom code
# uses GExiv2, and the C to Python bindings. but pain in the ass to install and config
#   very limited read/wriging...cannot create a new application comment block. writing my own
import logging
import json
import gi
gi.require_version('GExiv2', '0.10')
from gi.repository import GExiv2

#
# { 'pub_key':b"\x8f\xab\xfe\xd1\xd5\xb4\xea\xbb\xfa", 'sig':b"\x8f\xab\xfe\xd1\xd5\xb4\xea\xbb\xfa" }
#
#
class ImgExif:
  
  sig_h  = None
  exif_c = None
  img_fn = None

  #
  def __init__(self, img_fn):
    self.useImg(img_fn)

  #
  def useImg(self, img_fn):
    result = False
    try:
      self.exif_c = GExiv2.Metadata()
      ret = self.exif_c.open_path(img_fn)
      if (ret):
        result = True
    except Exception as ex:
      logging.error(ex)
      raise
    self.img_fn = img_fn
    self.sig_h = {}

  #
  def addKey(self, key):
    self.sig_h['pub_key'] = key

  #
  def addSig(self, sig):
    self.sig_h['sig'] = sig

  #
  def toJSON(self):
    return json.dumps(self.sig_h)

  #
  def extractKey(self):
    pemData = self.exif_c.get_comment()

  #
  def saveFile(self):
    logging.info("Saving image as {}...".format(self.img_fn))
    self.exif_c.set_comment(json.dumps(self.sig_h))
    self.exif_c.save_file(self.img_fn)

  #
  def saveAsFile(self, fn):
    #fn = self.img_fn + "_PICSEAL.jpg"
    logging.info("Saving image as {}...".format(fn))
    self.exif_c.set_comment(json.dumps(self.sig_h))
    self.exif_c.save_file(fn)

#
# TESTING
#
#
def _test():
  img = ImgExif("test/bmw_rim_640_backup.jpg")
  img.addKey("-/-THIS IS A KEY-/-")
  img.addSig("-/-THIS IS A SIG-/-")
  print("JSON: \n{}".format(img.toJSON()))
  img.saveFile()


if __name__ == '__main__':
  _test()
