require("starship"):setup()

require("git"):setup()
th.git = th.git or {}
th.git.modified_sign = "M"
th.git.deleted_sign = "D"

require("restore"):setup({
    position = { "center", w = 70, h = 40 },
    show_confirm = true })

local pref_by_location = require("pref-by-location")
pref_by_location:setup({
  -- Disable this plugin completely.
  -- disabled = false -- true|false (Optional)

  -- Hide "enable" and "disable" notifications.
  -- no_notify = false -- true|false (Optional)

  -- You can backup/restore this file. But don't use same file in the different OS.
  -- save_path =  -- full path to save file (Optional)
  --       - Linux/MacOS: os.getenv("HOME") .. "/.config/yazi/pref-by-location"
  --       - Windows: os.getenv("APPDATA") .. "\\yazi\\config\\pref-by-location"

  -- You don't have to set "prefs". Just use keymaps below work just fine
  prefs = { -- (Optional)
    -- location: String | Lua pattern (Required)
    --   - Support literals full path, lua pattern (string.match pattern): https://www.lua.org/pil/20.2.html
    --     And don't put ($) sign at the end of the location. %$ is ok.
    --   - If you want to use special characters (such as . * ? + [ ] ( ) ^ $ %) in "location"
    --     you need to escape them with a percent sign (%) or use a helper funtion `pref_by_location.is_literal_string`
    --     Example: "/home/test/Hello (Lua) [world]" => { location = "/home/test/Hello %(Lua%) %[world%]", ....}
    --     or { location = pref_by_location.is_literal_string("/home/test/Hello (Lua) [world]"), .....}

    -- sort: {} (Optional) https://yazi-rs.github.io/docs/configuration/yazi#mgr.sort_by
    --   - extension: "none"|"mtime"|"btime"|"extension"|"alphabetical"|"natural"|"size"|"random", (Optional)
    --   - reverse: true|false (Optional)
    --   - dir_first: true|false (Optional)
    --   - translit: true|false (Optional)
    --   - sensitive: true|false (Optional)

    -- linemode: "none" |"size" |"btime" |"mtime" |"permissions" |"owner" (Optional) https://yazi-rs.github.io/docs/configuration/yazi#mgr.linemode
    --   - Custom linemode also work. See the example below

    -- show_hidden: true|false (Optional) https://yazi-rs.github.io/docs/configuration/yazi#mgr.show_hidden

    -- Match any folder with name "downloads"
    { location = ".*/downloads", sort = { "mtime", reverse = true, dir_first = false }, linemode = "time_and_size" },
  },
})

Status:children_add(function()
    local h = cx.active.current.hovered
    local elements = {}

    local btime_formatted = nil
    if h and h.cha and h.cha.btime then
      local timestamp_num = tonumber(h.cha.btime)
      if timestamp_num and timestamp_num > 0 then
        btime_formatted = os.date("%Y-%m-%d %H:%M", math.floor(timestamp_num))
      end
    end

    local mtime_formatted = nil
    if h and h.cha and h.cha.mtime then
      local timestamp_num = tonumber(h.cha.mtime)
      if timestamp_num and timestamp_num > 0 then
        mtime_formatted = os.date("%Y-%m-%d %H:%M", math.floor(timestamp_num))
      end
    end

    if btime_formatted then
      table.insert(elements, ui.Span("B:"):fg("cyan"))
      table.insert(elements, ui.Span(btime_formatted.. " "):fg("blue"))
    end

    if mtime_formatted then
      table.insert(elements, ui.Span("M:"):fg("cyan"))
      table.insert(elements, ui.Span(mtime_formatted.. " "):fg("blue"))
    end

    return ui.Line(elements)
end, 500, Status.RIGHT)

Status:children_add(function()
    local h = cx.active.current.hovered
    if h == nil or ya.target_family() ~= "unix" then
      return ""
    end

    return ui.Line {
      ui.Span(ya.user_name(h.cha.uid) or tostring(h.cha.uid)):fg("magenta"),
      ":",
      ui.Span(ya.group_name(h.cha.gid) or tostring(h.cha.gid)):fg("magenta"),
      " ",
    }
end, 500, Status.RIGHT)

Status:children_add(function(self)
    local h = self._current.hovered
    if h and h.link_to then
      return " -> " .. tostring(h.link_to)
    else
      return ""
    end
end, 3300, Status.LEFT)


function Linemode:time_and_size()
  local time = math.floor(self._file.cha.mtime or 0)
  if time == 0 then
    time = ""
  elseif os.date("%Y", time) == os.date("%Y") then
    time = os.date("%b %d %H:%M", time)
  else
    time = os.date("%b %d  %Y", time)
  end

  local size_str
  local size = self._file:size()
  if size then
    size_str = ya.readable_size(size)
  else
    -- file count for dirs
    local folder = cx.active:history(self._file.url)
    size_str = folder and tostring(#folder.files) or "-"
  end
  local size_formatted = string.format("%6s", size_str)

  return string.format("%s  %s", time, size_formatted)
end
