local P = {}

P.name = "PSD t@C exo "

P.priority = 0

function P.ondragenter(files, state)
  for i, v in ipairs(files) do
    local ext = v.filepath:match("[^.]+$"):lower()
    if ext == "psd" or ext == "psb" then
      -- t@C̊gq psd  psb ̃t@C珈łȂ̂ true
      return true
    end
  end
  return false
end

function P.ondragover(files, state)
  -- ondragenter ŏłȂ̂ ondragover łłȂ̂Œׂ true
  return true
end

function P.ondragleave()
end

function P.encodelua(s)
  s = GCMZDrops.convertencoding(s, "sjis", "utf8")
  s = GCMZDrops.encodeluastring(s)
  s = GCMZDrops.convertencoding(s, "utf8", "sjis")
  return s
end

function P.ondrop(files, state)
  for i=1, #files do
    local v = files[i]
    if v == nil then
      -- Ĕ͈͊OɂȂ
      break
    end
    -- t@C̊gq psd  psb 
    local ext = v.filepath:match("[^.]+$"):lower()
    if ext == "psd" or ext == "psb" then
      local filepath = v.filepath
      local filename = filepath:match("[^/\\]+$")

      -- ꏏ pfv t@C͂łȂׂ
      local psddir = filepath:sub(1, #filepath-#filename)
      for i2, v2 in ipairs(files) do
        if v2.filepath:match("[^.]+$"):lower() == "pfv" then
          local pfv = v2.filepath:match("[^/\\]+$")
          local pfvdir = v2.filepath:sub(1, #v2.filepath-#pfv)
          if psddir == pfvdir then
            -- tH_[ pfv t@Cꏏɓł̂ŘA
            filepath = filepath .. "|" .. pfv
            --  pfv t@C̓hbvt@C͎菜Ă
            table.remove(files, i2)
            if i2 < i then
              -- ̃t@COɂt@C菜̂ŃCfbNX𒲐
              i = i - 1
            end
            break
          end
        end
      end

      -- t@C𒼐ړǂݍޑ exa t@CgݗĂ
      math.randomseed(os.time())
      local tag = math.floor(math.random()*0x7fffffff + 1)
      local jp = not GCMZDrops.englishpatched()
      local exa = [[
[vo.0]
_name=]] .. (jp and [[eLXg]] or [[Text]]) .. "\r\n" .. [[
]] .. (jp and [[TCY]] or [[Size]]) .. [[=1
]] .. (jp and [[\x]] or [[vDisplay]]) .. [[=0.0
]] .. (jp and [[ɌʃIuWFNg]] or [[1char1obj]]) .. [[=0
]] .. (jp and [[ړWɕ\]] or [[Show on motion coordinate]]) .. [[=0
]] .. (jp and [[XN[]] or [[Automatic scrolling]]) .. [[=0
B=0
I=0
type=0
autoadjust=0
soft=0
monospace=0
align=4
spacing_x=0
spacing_y=0
precision=0
color=ffffff
color2=000000
font=]] .. (jp and [[MS UI Gothic]] or [[Segoe UI]]) .. "\r\n" .. [[
text=]] .. GCMZDrops.encodeexotext("<?-- " .. filename .. " \r\n\r\no={ -- IvVݒ\r\nlipsync = 0    ,-- pÑC[ԍ\r\nmpslider = 0    ,-- ړIXC_[̃C[ԍ\r\nscene = 0    ,-- V[ԍ\r\ntag = " .. tag .. "    ,-- ʗp^O\r\nsendguard = 1    ,-- uv둗Mی\r\n\r\n-- pÑftHgݒ\r\nls_locut = 100    ,-- [Jbg\r\nls_hicut = 1000    ,-- nCJbg\r\nls_threshold = 20    ,-- l\r\nls_sensitivity = 1    ,-- x\r\n\r\n-- ȉ͏Ȃł\r\nptkf=" .. P.encodelua(filepath) .. ",ptkl=\"\"}PSD,subobj=require(\"PSDToolKit\").PSDState.init(obj,o)?>") .. "\r\n" .. [[
[vo.1]
_name=]] .. (jp and [[Aj[V]] or [[Animation effect]]) .. "\r\n" .. [[
track0=-1.00
track1=100.00
track2=0.00
track3=0.00
check0=100
type=0
filter=2
name=`@PSD
param=
[vo.2]
_name=]] .. (jp and [[W`]] or [[Standard drawing]]) .. "\r\n" .. [[
X=0.0
Y=0.0
Z=0.0
]] .. (jp and [[g嗦]] or [[Zoom%]]) .. [[=100.00
]] .. (jp and [[x]] or [[Clearness]]) .. [[=0.0
]] .. (jp and [[]\]] or [[Rotation]]) .. [[=0.00
blend=0
]]

      -- PSDToolKit EBhEɃhbvꂽt@Cǉ
      -- ꎞI package.cpath  PSDToolKitBridge.dll ǂݍ addfile Ă
      local origcpath = package.cpath
      package.cpath = GCMZDrops.scriptdir() .. "..\\script\\PSDToolKit\\?.dll"
      require('PSDToolKitBridge').addfile(GCMZDrops.convertencoding(filepath, "sjis", "utf8"), tag)
      package.cpath = origcpath

      local filepath = GCMZDrops.createtempfile("psd", ".exa")
      f, err = io.open(filepath, "wb")
      if f == nil then
        error(err)
      end
      f:write(exa)
      f:close()
      debug_print("["..P.name.."]  " .. v.filepath .. "  exa t@Cɍւ܂B̃t@C orgfilepath Ŏ擾ł܂B")
      files[i] = {filepath=filepath, orgfilepath=v.filepath}
    end
  end
  -- ̃Cxgnh[ɂ̂ł͏ false
  return false
end

return P
