from pypinyin import lazy_pinyin, Style

#用于把常用中文翻译成英文变量名，越前优先级越高, 如果不在表里，用拼音首字母
ProgramVariableNameList = [
    
    ["延时",     "Delay"],
    ["保护",     "Protect"],
    ["设置",     "Set"], 
    ["试验",     "Test"],   
    ["测试",     "Test"],   
    ["相位",     "Phase"], 
    ["比值",     "Percent"],
    ["定值",     "Value"], 
    ["压板",     "BitValue"], 
    ["类型",     "Type"], 
    ["模式",     "Mode"],
    
    ["双风机",     "SFJ"],
    ["双磁力",     "DCL"],
    ["移变高压侧",     "YBGY"],
    ["组合馈电",     "ZHKD"],
    ["双高爆",     "SGB"],
    ["双照明",     "DZM"],
    ["馈电",     "KD"],
    ["高爆",     "GB"],
    ["移变",     "YB"],
    ["磁力",     "CL"],
    ["照明",     "ZM"],
    
    ["互感器",   "Sensor"], 
    ["不平衡",   "Unbalance"],
    ["总开关",    "Top"],
    ["分开关",    "Bottom"],
    ["电压",     "Volt"],
    ["电流",     "Current"],
    ["额定",     "Rate"],
    ["无功电量", "KVar"],
    ["有功电量", "Kwh"],
    ["有功功率", "PowS"],
    ["无功功率", "PowU"],
    ["功率",     "Pow"],
    ["有功",     "S"],
    ["无功",     "U"],
    ["系统",     "System"],
    ["校正",     "Correct"],
    ["版本",     "Version"],
    ["短路",     "Short"],
    ["过流",     "OverCurrent"],
    ["欠压",     "VoltLow"],
    ["过压",     "VoltHigh"],
    ["设备",     "Device"],
    ["编号",     "ID"],
    ["故障",     "Alarm"], 
    ["查询",     "Query"], 
    ["值",       "Value"],

    ["测量",     "Measure"],
    ["检测",     "Measure"],    
    ["平均",     "Average"],
    ["断相",      "Break"],
    ["闭锁",      "Lock"],
    ["解锁",      "Delock"],
    ["动作",      "Action"], 
    ["开关",      "Switch"],  
    ["无校验",     "None"],
    ["奇校验",     "Odd"],
    ["偶校验",     "Even"],
    ["地址",      "Addr"],    
    ["波特率",     "Baud"],    
    ["校验",       "Parity"],    
    ["网关",       "Gate"],    
    ["掩码",       "Mask"],    
    ["工作",       "Work"],      
    ["控制",       "Control"],      
    ["触点",       "InputMode"],
    ["风电",       "WindMotor"],
    ["瓦斯",       "Gas"],
    ["维修",       "Repair"], 
    ["日期",       "Date"], 
    ["时间",       "Time"],     
    ["累计",       "Acc"],
    ["数据",       "Data"], 
    ["当前",       "Cur"], 
    ["软件",       "Soft"], 
    ["密码",       "Password"], 
    ["零序",       "ZeroSeq"], 
    ["负序",       "NegSeq"],   
    ["反馈",       "FeedBack"],
    ["电阻",       "Resistor"], 
    ["漏电",       "Insulation"],
    ["正反转",     "FWD_RVS"],    
    ["定时限",     "FixTime"],
    ["反时限",     "AntiTime"],
    ["过负荷",     "OverLoad"],   
    ["欠负荷",     "UnderLoad"],   
    ["启动",       "Start"],
    ["停止",       "Stop"],   
    ["以太网",     "Eth"],   
    ["模块",       "Module"],   
    ["型号",       "Select"],  
    ["选择",       "Select"],   
    ["欧姆",       "R"],   
    ["欧",         ""],   
    ["投入",       "On"],   
    ["串联",       "SeriesConn"],   
    ["通讯",       "Comm"],   
    ["水位",       "Water_Level"],    
    ["高",         "High"],    
    ["低",         "Low"],    

    ["正常",       "Normal"],   
    ["非常",       "Very"],   
    ["极端",       "Extreme"], 
    ["方向",       "Direct"], 
    ["正",         "Pos"],     
    ["负",         "Neg"],   
    ["非",         "Non"],   
    ["电量",       "Electric"],   
    ["门槛",       "Threshold"], 
    ["常数",       "Const"], 
    ["配置",       "Conf"],
    ["次数",       "Count"],   
    ["合闸",       "ON"],
    ["分闸",       "OFF"],   
    ["1段",        "1"],
    ["2段",        "2"],
    ["3段",        "3"],
    ["一段",       "1"],
    ["二段",       "2"],
    ["三段",       "3"],
    ["一代",       "V1"],
    ["二代",       "V2"],
    ["关闭",       "Disable"],
    ["打开",       "Enable"],
    ["常开",       "NormalOpen"],
    ["常闭",       "NormalClose"],
    ["模拟",       "Analog"],
    ["继电器",     "Relay"],
    ["就地",       "Local"],
    ["程控",       "Remote"],
    ["无效",       "Invalid"],
    ["无",         "None"],
]
    
def get_variable_name(combine_name_str : str, convert_list : list = None) :
    remain_name = combine_name_str
    variable_name = ""
    
    if convert_list == None :
        convert_list = ProgramVariableNameList

    for list_item in convert_list :
        if list_item[0] in remain_name:
            if variable_name != "" :
                variable_name += "_"
            variable_name = variable_name + list_item[1]
            remain_name = remain_name.replace(list_item[0], "")
    remain_name = remain_name.replace(" ", "")
    remain_name = remain_name.replace("/", "")
    remain_name = remain_name.replace("\\", "")
    
    remain_pinying = lazy_pinyin(remain_name, style=Style.FIRST_LETTER)
    first_letter_pinying = ""
    for each in remain_pinying :
        first_letter_pinying += each.capitalize()
        
    if len(remain_pinying) > 0 :
        if variable_name == "" :
            variable_name = first_letter_pinying
        else :
            variable_name = variable_name + "_" + first_letter_pinying
    
    return variable_name
    