--[[  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 sDrawImage(spacecr,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(spacecr)
    cairo_translate (spacecr, xx, yy)
    cairo_scale (spacecr, ww/w_img, hh/h_img)
    cairo_set_source_surface (spacecr, img, xx + 6, yy + 3)
    cairo_paint_with_alpha (spacecr, 1)
    cairo_surface_destroy (img)
    collectgarbage ()
    cairo_restore (spacecr)
  return 'finished'
end



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



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

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

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


    -- Executes 'RUN_BASH' script
    -- In this script you can run other bash scripts that gets executed at every conky_update
    os.execute("./scripts/RUN_BASH GO &")


    sDrawImage(spacecr,srealpath .."/current/current.png",xxx,yyy,www,hhh,ttt)

    cairo_surface_destroy(spacecs)
    cairo_destroy(spacecr)
  return ''
end
