--[[

    Enregistre les EXIF  - plugin for Darktable
	Vitesse, ouverture,ISO

    Copyright (C) 2025 Michel Fiocchi.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
]]
--[[
    Print EXIFs  data from selected image(s)

    ......

    ADDITIONAL SOFTWARE NEEDED FOR THIS SCRIPT
    * None

    USAGE
    * require this script from your main lua file
    * select an image or images
    * click the shortcut, exif
    * the exif datas from the file Image_name.* are written in a file Image_name.txt
    BUGS, COMMENTS, SUGGESTIONS
    * Send to 

    CHANGES
]]

local dt = require "darktable"
local du = require "lib/dtutils"

local gettext = dt.gettext.gettext 

local function _(msgid)
    return gettext(msgid)
end

-- return data structure for script_manager

local script_data = {}

script_data.metadata = {
  name = _("exif"),
  purpose = _("print Exifs from selected image(s)"),
  author = "Fichel Fiocchi",
  help = ""
}

script_data.destroy = nil -- function to destory the script
script_data.destroy_method = nil -- set to hide for libs since we can't destroy them commpletely yet
script_data.restart = nil -- how to restart the (lib) script after it's been hidden - i.e. make it visible again
script_data.show = nil -- only required for libs since the destroy_method only hides them


du.check_min_api_version("7.0.0", "exif") 

local image


--****** Sous Programmes
-- Fonction pour extraire les données EXIF et écrire dans un fichier texte

local function write_exif_to_file(image)
-- Obtenir les métadonnées EXIF
local shutter_speed
local speed = image.exif_exposure
if speed < 1 then 
	shutter_speed = string.format("1/%.0f",1/speed)
	else
	shutter_speed = string.format("%.0f",speed)
 end
local iso = string.format("%.0f",image.exif_iso)
local aperture = string.format("%.1f",image.exif_aperture)

-- Créer le nom du fichier texte
local imagePath = image.path.."\\"..image.filename
local outputPath = image.path.."\\"..image.filename:match("^[^.]*")..".txt" 

---mise au point
local file = io.open(outputPath, "w")
file:write("ISO: " .. iso .. "\n")
file:write("Ouverture: " .. aperture .. "\n")
file:write("Vitesse: " .. shutter_speed .. "\n")
file:close()
dt.print("Données EXIF écrites dans: " .. outputPath.."\r")


end




--****** module principal

local function exif(images)


if #images == 0 then
 dt.print("Aucune image sélectionnée.")
 return
end

for _, image in ipairs(images) do

   write_exif_to_file(image)

end
dt.print ("end")
end


 --****************************************************************
-- gestion du script

local function destroy()
  dt.destroy_event("exif", "shortcut")
  dt.gui.libs.image.destroy_action("exif")
end

script_data.destroy = destroy

dt.gui.libs.image.register_action(
  "exif", _("print Exif data"),
  function(event, images) exif(images) end,
  _("print Exif data from selected images")
)

dt.register_event(
  "exif", "shortcut",
  function(event, shortcut) exif(dt.gui.action_images) end,
  _("print Exif data from selected images")
)

return script_data
