-- encoding: utf-8
-- 功能比较简单,只提供日期,时间,数字转大写的快速输入
-- 2021年2月24日 10:48 by 环环  深山红叶优化
--      请在方案配置文件比如wubi86.schema.yaml的engine\translators:下面添加滤镜引用：
--        - lua_translator@date_translator
--        - lua_translator@number_translator

function date_translator(input, seg)
--调用外部程序，实现快速运行。无关风月修改。以下为示例，请自行修改，路径中不要包含中文字符！不带空格和非ASCII符号！
   if (input == "/set") then  --对小狼毫进行配置
    strProgram = '"d:\\Rime\\WeaselDeployer.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/jsq") then  --打开计算器
    strProgram = '"C:\\Windows\\System32\\calc.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/cal") then  --打开计算器
    strProgram = '"C:\\Windows\\System32\\calc.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/kzmb") then  --打开控制面板
    strProgram = '"C:\\Windows\\System32\\control.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/rrds") then  --打开控制面板
    strProgram = '"C:\\Windows\\System32\\control.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/con") then  --打开控制面板
    strProgram = '"C:\\Windows\\System32\\control.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/cmd") then  --打开命令行窗口
    strProgram = '"C:\\Windows\\System32\\cmd.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/task") then  --打开任务管理器
    strProgram = '"C:\\Windows\\System32\\Taskmgr.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/reg") then  --打开注册表编辑器
    strProgram = '"C:\\Windows\\System32\\regedit.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/sx") then  --打开搜狗手写
    strProgram = '"d:\\Rime\\HandInput\\handinput.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/hw") then  --打开搜狗手写
    strProgram = '"d:\\Rime\\Tools\\HandInput\\handinput.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/zt") then  --打开抓图工具
    strProgram = '"d:\\Rime\\Tools\\WinSnap.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/zp") then  --打开抓图工具
    strProgram = '"d:\\Rime\\Tools\\WinSnap.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/jt") then  --打开抓图工具
    strProgram = '"d:\\Rime\\Tools\\WinSnap.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/jp") then  --打开抓图工具
    strProgram = '"d:\\Rime\\Tools\\WinSnap.exe"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/bd") then  --打开百度
    strProgram = '"https://www.baidu.com/"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/ss") then  --打开百度
    strProgram = '"https://www.baidu.com/"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/tb") then  --打开淘宝
    strProgram = '"https://www.taobao.com/"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end
   if (input == "/ya") then  --打开yaml在线查验网
    strProgram = '"http://www.bejson.com/validators/yaml/"' 
    strCmd = 'start "" '..strProgram 
    os.execute(strCmd)
   end

	  --date1 2020年02月04日
    date1=os.date("%Y年%m月%d日")
	  date_y=os.date("%Y") --取年
	  date_m=os.date("%m") --取月
	  date_d=os.date("%d") --取日

	  --date2 2020年2月4日
	  num_m=os.date("%m")+0
	  num_m1=math.modf(num_m)
	  num_d=os.date("%d")+0
	  num_d1=math.modf(num_d)
	  date2=os.date("%Y年")..tostring(num_m1).."月"..tostring(num_d1).."日"

    --date3 二〇二〇年十一月二十六日
   date_y=date_y:gsub("%d",{
       ["1"]="一",
       ["2"]="二",
       ["3"]="三",
       ["4"]="四",
       ["5"]="五",
       ["6"]="六",
       ["7"]="七",
       ["8"]="八",
       ["9"]="九",
       ["0"]="〇",
--       ["0"]="零",
      })
   date_y=date_y.."年"

   date_m=date_m:gsub("%d",{
       ["1"]="一",
       ["2"]="二",
       ["3"]="三",
       ["4"]="四",
       ["5"]="五",
       ["6"]="六",
       ["7"]="七",
       ["8"]="八",
       ["9"]="九",
       ["0"]="",
      })
   date_m=date_m.."月"
   if num_m1==10 then date_m="十月" end
   if num_m1==11 then date_m="十一月" end
   if num_m1==12 then date_m="十二月" end

   date_d=date_d:gsub("%d",{
       ["1"]="一",
       ["2"]="二",
       ["3"]="三",
       ["4"]="四",
       ["5"]="五",
       ["6"]="六",
       ["7"]="七",
       ["8"]="八",
       ["9"]="九",
       ["0"]="",
      })
   date_d=date_d.."日"
   date3=date_y..date_m..date_d

    --date4 2020年6月6日星期六6点6分6秒
    date4=os.date(date2)..os.date(day_w1)..os.date("%H点%M分%S秒")

    --date5 2020-06-06㊅06:06:06
    date5=os.date("%Y-%m-%d")..os.date(day_w2)..os.date("%H:%M:%S")

    --date6 2020.02.04
    date6=os.date("%Y.%m.%d")

    --date7 Sat Jun  6 06:06:06 2020
    date7=os.date(ChineseDate)

    --date8 2020/02/04
    date8=os.date("%Y/%m/%d")

    --date9 2020年6月6日星期六
    date9=os.date(date2)..os.date(day_w1)

    --date20 2020年2月4日06:06:06
	  date20=os.date("%Y年")..tostring(num_m1).."月"..tostring(num_d1).."日"..os.date("%H点%M分%S秒")

    --date21 2020年2月4日06:06:06
    date21=os.date("%Y年")..tostring(num_m1).."月"..tostring(num_d1).."日"..os.date(" %H:%M")

	  --星期
	 day_w=os.date("%w")
	 day_w1=""
	if day_w=="0" then day_w1="星期日" end
	if day_w=="1" then day_w1="星期一" end
	if day_w=="2" then day_w1="星期二" end
	if day_w=="3" then day_w1="星期三" end
	if day_w=="4" then day_w1="星期四" end
	if day_w=="5" then day_w1="星期五" end
	if day_w=="6" then day_w1="星期六" end

	  --星期
	 day_w3=os.date("%w")
	 day_w2=""
	if day_w3=="0" then day_w2="㊐" end
	if day_w3=="1" then day_w2="㊀" end
	if day_w3=="2" then day_w2="㊁" end
	if day_w3=="3" then day_w2="㊂" end
	if day_w3=="4" then day_w2="㊃" end
	if day_w3=="5" then day_w2="㊄" end
	if day_w3=="6" then day_w2="㊅" end

    --星期
   day_w5=os.date("%w")
   day_w4=""
  if day_w5=="0" then day_w4="周日" end
  if day_w5=="1" then day_w4="周一" end
  if day_w5=="2" then day_w4="周二" end
  if day_w5=="3" then day_w4="周三" end
  if day_w5=="4" then day_w4="周四" end
  if day_w5=="5" then day_w4="周五" end
  if day_w5=="6" then day_w4="周六" end

   if (input == "date" or input == "iuja" or input == "rejj" or input == "pedl" or input == "/now" or input == "/qrq" or input == "/rq" or input == "/nl"  or input == "/nyr" or input == "/date" or input == "/iuja" or input == "/rejj" or input == "/jjad") then
      yield(Candidate("date", seg.start, seg._end, date2, ""))
      yield(Candidate("date", seg.start, seg._end, date3, ""))
      yield(Candidate("date", seg.start, seg._end, date9, ""))
      yield(Candidate("date", seg.start, seg._end, os.date("%Y-%m-%d"), ""))
      yield(Candidate("date", seg.start, seg._end, date6, ""))
      yield(Candidate("date", seg.start, seg._end, date8, ""))
      yield(Candidate("date", seg.start, seg._end, os.date("%Y%m%d"), ""))
--      yield(Candidate("date", seg.start, seg._end, date5, ""))
--      yield(Candidate("date", seg.start, seg._end, date7, ""))
   end

   if (input == "time" or input == "/time" or input == "iuju" or input == "jwti" or input == "/sj" or input == "/sfm" or input == "sfm" or input == "dqsj") then
      yield(Candidate("time", seg.start, seg._end, os.date("%H:%M"), "  "))
      yield(Candidate("time", seg.start, seg._end, os.date("%H点%M分"), "  "))
      yield(Candidate("time", seg.start, seg._end, os.date("%H:%M:%S"), ""))
      yield(Candidate("time", seg.start, seg._end, os.date("%H点%M分%S秒"), ""))
   end

   if (input == "week" or input == "jamt" or input == "/week" or input == "/xq" or input == "/jtad" or input == "/jaj" or input == "/xqj") then
      yield(Candidate("week", seg.start, seg._end, os.date(day_w1), "  "))
--      yield(Candidate("week", seg.start, seg._end, os.date(day_w2), "  "))
--      yield(Candidate("week", seg.start, seg._end, os.date(day_w4), "  "))
   end

   if (input == "now" or input == "/now" or input == "jaju") then
      yield(Candidate("now", seg.start, seg._end, os.date("%Y-%m-%d %H:%M:%S"), ""))
      yield(Candidate("now", seg.start, seg._end, date21, ""))
      yield(Candidate("now", seg.start, seg._end, date4, ""))
   end
end

--      斜杠后阿拉伯数字转大写，比如 /12
local confs = {
   {
      comment = " 大写",
      number = { [0] = "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" },
      suffix = { [0] = "", "拾", "佰", "仟" },
      suffix2 = { [0] = "", "万", "亿", "万亿", "亿亿" }
   },
   {
      comment = " 小写",
      number = { [0] = "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" },
      suffix = { [0] = "", "十", "百", "千" },
      suffix2 = { [0] = "", "万", "亿", "万亿", "亿亿" }
   },
}

local function read_seg(conf, n)
   local s = ""
   local i = 0
   local zf = true

   while string.len(n) > 0 do
      local d = tonumber(string.sub(n, -1, -1))
      if d ~= 0 then
   s = conf.number[d] .. conf.suffix[i] .. s
   zf = false
      else
   if not zf then
      s = conf.number[0] .. s
   end
   zf = true
      end
      i = i + 1
      n = string.sub(n, 1, -2)
   end

   return i < 4, s
end

local function read_number(conf, n)
   local s = ""
   local i = 0
   local zf = false

   n = string.gsub(n, "^0+", "")

   if n == "" then
      return conf.number[0]
   end

   while string.len(n) > 0 do
      local zf2, r = read_seg(conf, string.sub(n, -4, -1))
      if r ~= "" then
   if zf and s ~= "" then
      s = r .. conf.suffix2[i] .. conf.number[0] .. s
   else
      s = r .. conf.suffix2[i] .. s
   end
      end
      zf = zf2
      i = i + 1
      n = string.sub(n, 1, -5)
   end
   return s
end

function number_translator(input, seg)
   if string.sub(input, 1, 1) == "/" then
      local n = string.sub(input, 2)
      if tonumber(n) ~= nil then
   for _, conf in ipairs(confs) do
      local r = read_number(conf, n)
      yield(Candidate("number", seg.start, seg._end, r, conf.comment))
   end
      end
   end
end

--- single_char_filter,单字优先模式
function single_char_filter(input)
   if (word == 1) then
    local L = {}
    for cand in input:iter() do
       if (utf8.len(cand.text) == 1) then
	  yield(cand)
       else
	  table.insert(L, cand)
       end
    end
    for i, cand in ipairs(L) do
      yield(cand)
    end
   else
	local L = {}
    for cand in input:iter() do
	  yield(cand)
    end
    
   end
end
