--[[  v1.0
2025-12-10 Scripted by Koentje  (remon@cobrasoft.nl)


                                          ________________ vertical position in pixels
                                         |  ______________ horizontal position in pixels
                                         | |  ____________ width of image
                                         | | |    ________ height of image
                                         | | |   |    ____ Transparency (1-100)
                                         | | |   |   |   _ r for reversed animation (leave empty for normal)
                                         | | |   |   |  |
usage:  ${lua animation ./pathtopictures 2 2 300 300 50 r}

]]

require 'cairo'
require "imlib2"

-- path to anicalc BASH script
ANICALC="./scripts/anicalc"


-- Function to draw image
function aDrawImage(acr,path,xx,yy,ww,hh,tt)
    local img =  cairo_image_surface_create_from_png(path)
    local w_img = cairo_image_surface_get_width(img)
    local h_img = cairo_image_surface_get_height(img)
    local tt = tonumber(tt)
    if tt == nil then tt = 100 end
    if tt <= 100 then
      if tt >= 0 then
        -- Convert from [1-100] to [0-1.0]
        alph = (tt / 100)
      else
        alph = 1
      end
    else
     alph = 1
    end
    cairo_save(acr)
    cairo_translate (acr, xx, yy)
    cairo_scale (acr, ww/w_img, hh/h_img)
    cairo_set_source_surface (acr, img, xx + 6, yy + 3)
    cairo_paint_with_alpha (acr, alph)
    cairo_surface_destroy (img)
    collectgarbage ()
    cairo_restore (acr)
  return 'finished'
end



-- Function to check if path exists
function isdir(apath)
   if io.open(apath, "r") == nil then
      print("\27[31mERROR:\27[m Could not find folder to animation files: '".. apath .."' !") os.exit()
   else
      -- Check if path exist and if trailing slash. If so remove trailing slash
      local ahandle = io.popen("if [ -d \"".. apath .."\" ]; then echo \"".. apath .."\" | sed 's:/*$::'; fi")
      local output = ahandle:read("*a")
      local aresult = output:gsub('[\n\r]', '')
      ahandle:close()
      -- Return the correct path without trailing slash
      return aresult
   end
end



-- Main function
function conky_animation(apath,xxx,yyy,www,hhh,ttt,rrr)
    if conky_window==nil then return '' end
    local acs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)
    local acr=cairo_create(acs)
    local sort = ""
    if rrr == nil then sort = "" end
    if rrr == "r" or rrr == "reverse" then sort = "reverse" end

    if apath == nil then print("\27[31mERROR:\27[m Path is empty! Check your conky text section.\27[m") os.exit() end
    local realpath = isdir(apath)
    -- Run anicalc bash script to get the next PNG file
    os.execute(ANICALC .." \"".. realpath .."\" \"".. sort .."\"")

    -- Uncomment below line for debug on terminal
--     os.execute("echo \"".. realpath .."\" \t = $(cat \"".. realpath .."\"/current/conky_slideshow_index)")

    aDrawImage(acr,realpath .."/current/current.png",xxx,yyy,www,hhh,ttt)

    cairo_surface_destroy(acs)
    cairo_destroy(acr)
  return ''
end
