# -*- coding: utf-8 -*-
import os
import sys
import re
import clr

EasyScript_dll = os.path.abspath('')+'\\Script\\Module\\dll\\EasyScript.dll'
Check_EasyScript_dll = os.path.isfile(EasyScript_dll)
if Check_EasyScript_dll==False:
    EasyScript_dll = os.path.abspath('..\\..\\')+'\\Script\\Module\\dll\\EasyScript.dll'
    Check_EasyScript_dll = os.path.isfile(EasyScript_dll)
    if Check_EasyScript_dll==True:
        EasyScript_dll = os.path.abspath('..\\..\\')+'\\Script\\Module\\dll'
else:
    EasyScript_dll = os.path.abspath('')+'\\Script\\Module\\dll'
sys.path.append(EasyScript_dll)
clr.AddReferenceToFile('EasyScript.dll')
from EasyScript import *
upl = EasyScript()
#UI.UpdateLog(b)

    

#多行顯示UI.UpdateLog
def Logs(*s):
    if s[len(s)-1] == ":0" or s[len(s)-1] == ":1" or s[len(s)-1] == ":2" or s[len(s)-1] == ":3" or s[len(s)-1] == ":4":
        color=s[len(s)-1][1:2]
        ss=[]
        for val in s:
            ss.append(str(val))
        ss.remove(ss[len(s)-1])
        for val in ss:
            UI.UpdateLog("%s"%(str(val)),color)
    else:
        for val in s:
            UI.UpdateLog("%s"%(str(val)))
			
#連結自動添加"^"並自動添加指令到UI.AddShareLink()
def ShareLink(*s):
    #UI.UpdateLog('%r'%s)
    arr=[]
    for link in s:
        arr.append(str(link))
    arr="^".join(arr)
    UI.UpdateLog('%s'%arr)
    UI.AddShareLink(arr)

#網頁解碼(需調用EasyScript.dll)
def UrlDecode(*s):
    data=s[0]
    data=upl.ReplaceVariable(data,'\\\\u','%u')
    data=upl.ReplaceVariable(data,'\\','')
    data=upl.ReplaceVariable(data,'+','%2B')
    data=upl.UrlDecode(data)
    if s[len(s)-1] != False:
        UI.UpdateLog('解碼後的資料 = %s'%(data),2)
    return data

def Matches(*s):
    html=s[0]
    sstr=s[1]
    lens=re.findall(sstr,html)
    UI.UpdateLog('符合Matches匹配共有%d個'%(len(lens)),1)
    ss=[]
    if s[len(s)-1]!=False:
        for i, v in enumerate(lens):
            ss.append(v)
            UI.UpdateLog('MatchCollection[%d] = %s'%(i,v))
    return lens

def Matches2(*s):
    html=s[0]
    sstr=s[1]
    patt=re.search(sstr,html)
    #Logs(patt)
    if patt!=None:
        patt1=patt.groups()
    else:
        return None
    ss=[]
    if s[len(s)-1]!=False:
        for i, v in enumerate(patt1):
            ss.append(v)
            UI.UpdateLog('MatchCollection[%d] = %s'%(i,v))
    return patt1