import time
from fallback import fallback
from config import simplemodel
from template import xuanxiang
from fallback import fallback
chaogao9=['5m以上到8m以内', '8-12m', '12-16m', '16-20m']
chaogao10=['3.6m以上到8m以内', '8-12m', '12-16m', '16-20m']
chaogao11=['6到20m以内', '20-30m', '30-40m', '40-50m', '50-60m', '60-70m', '70-80m','80m以上']
def select_chaogao_11(
       B, #data
       aiclient,
             qwclient,
              sfclient
             ):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(chaogao11)):
        options.append("给定选项" + letters[i]+"，内容为"+chaogao11[i] )

    completion = aiclient.chat.completions.create(
        model="glm-4.5-air",
        #model="THUDM/GLM-Z1-9B-0414",
        #model="ernie-speed-128k",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "超高指的是操作物高度距离楼地面6m以上的工程."},
            {"role": "user", "content": "问题描述： 给定一段含有超高工作的内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(options) + "。请选择最合适的选项，并返回字母代号，例如，返回 A。如果内容中没有明确高度，请返回A"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": True},
        #stream=True
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    if len([x for x in json_string if x != ',' and x != '[' and x != ']' and x != ' ' and (x < 'A' or x > 'M')]) < 5:
        answer=[]
        if 'A' in json_string and len(chaogao11) > 0:
            answer.append('A')
        if 'B' in json_string and len(chaogao11) > 1:
            answer.append('B')
        if 'C' in json_string and len(chaogao11) > 2:
            answer.append('C')
        if 'D' in json_string and len(chaogao11) > 3:
            answer.append('D')
        if 'E' in json_string and len(chaogao11) > 4:
            answer.append('E')
        if 'F' in json_string and len(chaogao11) > 5:
            answer.append('F')
        if 'G' in json_string and len(chaogao11) > 6:
            answer.append('G')
        if 'H' in json_string and len(chaogao11) > 7:
            answer.append('H')
        return answer
    completion = sfclient.chat.completions.create(
        #model="glm-4.5-flash",
        model=simplemodel(),
        messages=xuanxiang(json_string),
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if 'A' in json_string and len(chaogao11) > 0:
        answer.append('A')
    if 'B' in json_string and len(chaogao11) > 1:
        answer.append('B')
    if 'C' in json_string and len(chaogao11) > 2:
        answer.append('C')
    if 'D' in json_string and len(chaogao11) > 3:
        answer.append('D')
    if 'E' in json_string and len(chaogao11) > 4:
        answer.append('E')
    if 'F' in json_string and len(chaogao11) > 5:
        answer.append('F')
    if 'G' in json_string and len(chaogao11) > 6:
        answer.append('G')
    if 'H' in json_string and len(chaogao11) > 7:
        answer.append('H')
    
    return answer
def select_chaogao_10(
       B, #data
       aiclient,
             qwclient,
              sfclient
             ):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(chaogao10)):
        options.append("给定选项" + letters[i]+"，内容为"+chaogao10[i] )

    completion = aiclient.chat.completions.create(
        model="glm-4.5-air",
        #model="THUDM/GLM-Z1-9B-0414",
        #model="ernie-speed-128k",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "超高指的是操作物高度距离楼地面3.6m以上的工程."},
            {"role": "user", "content": "问题描述： 给定一段含有超高工作的内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(options) + "。请选择最合适的选项，并返回字母代号，例如，返回 A。如果内容中没有明确高度，请返回A"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": True},
        #stream=True
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    if len([x for x in json_string if x != ',' and x != '[' and x != ']' and x != ' ' and (x < 'A' or x > 'M')]) < 5:
        answer=[]
        if 'A' in json_string and len(chaogao10) > 0:
            answer.append('A')
        if 'B' in json_string and len(chaogao10) > 1:
            answer.append('B')
        if 'C' in json_string and len(chaogao10) > 2:
            answer.append('C')
        if 'D' in json_string and len(chaogao10) > 3:
            answer.append('D')
        return answer
    completion = sfclient.chat.completions.create(
        #model="glm-4.5-flash",
        model=simplemodel(),
        messages=xuanxiang(json_string),
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if 'A' in json_string and len(chaogao10) > 0:
        answer.append('A')
    if 'B' in json_string and len(chaogao10) > 1:
        answer.append('B')
    if 'C' in json_string and len(chaogao10) > 2:
        answer.append('C')
    if 'D' in json_string and len(chaogao10) > 3:
        answer.append('D')
    
    return answer
def select_chaogao_9(
       B, #data
       aiclient,
             qwclient,
              sfclient
             ):
    options=[]
    letters = "ABCDEFGHIJKLMN"
    for i in range(len(chaogao9)):
        options.append("给定选项" + letters[i]+"，内容为"+chaogao9[i] )

    completion = aiclient.chat.completions.create(
        model="glm-4.5-air",
        #model="THUDM/GLM-Z1-9B-0414",
        #model="ernie-speed-128k",
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "超高指的是操作物高度距离楼地面5m以上的工程."},
            {"role": "user", "content": "问题描述： 给定一段含有超高工作的内容: " + B['label'] + " " +  B['mc'] + " " + B['tz'] +  "，".join(options) + "。请选择最合适的选项，并返回字母代号，例如，返回 A。如果内容中没有明确高度，请返回A"},
        ],
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": True},
        #stream=True
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    if len([x for x in json_string if x != ',' and x != '[' and x != ']' and x != ' ' and (x < 'A' or x > 'M')]) < 5:
        answer=[]
        if 'A' in json_string and len(chaogao9) > 0:
            answer.append('A')
        if 'B' in json_string and len(chaogao9) > 1:
            answer.append('B')
        if 'C' in json_string and len(chaogao9) > 2:
            answer.append('C')
        if 'D' in json_string and len(chaogao9) > 3:
            answer.append('D')
        return answer
    completion = sfclient.chat.completions.create(
        #model="glm-4.5-flash",
        model=simplemodel(),
        messages=xuanxiang(json_string),
        extra_body={"thinking": {"type": "disabled"}},
        #extra_body={"enable_thinking": False},
    )
    json_string = completion.choices[0].message.content
    print(json_string)
    answer=[]
    if 'A' in json_string and len(chaogao9) > 0:
        answer.append('A')
    if 'B' in json_string and len(chaogao9) > 1:
        answer.append('B')
    if 'C' in json_string and len(chaogao9) > 2:
        answer.append('C')
    if 'D' in json_string and len(chaogao9) > 3:
        answer.append('D')
    
    return answer

def feiyong_chaogao(data, selected, aiclient, qwclient, sfclient):
    a = [x for x in selected if x.startswith('4-')]
    if len(a) > 0:
        return ['4-F2!第四册']
    a = [x for x in selected if x.startswith('9-')]
    if len(a) > 0:
        t = select_chaogao_9(data, aiclient, qwclient, sfclient)
        if 'A' in t:
            return ['9-F2!第九册5m以上到8m以内']
        elif 'B' in t:
            return ['9-F2!第九册8-12m']
        elif 'C' in t:
            return ['9-F2!第九册12-16m']
        elif 'D' in t:
            return ['9-F2!第九册16-20m']
    a = [x for x in selected if x.startswith('10-')]
    if len(a) > 0:
        t = select_chaogao_10(data, aiclient, qwclient, sfclient)
        if 'A' in t:
            return ['10-F2!第十册3.6-8m以内']
        elif 'B' in t:
            return ['10-F2!第十册8-12m']
        elif 'C' in t:
            return ['10-F2!第十册12-16m']
        elif 'D' in t:
            return ['10-F2!第十册16-20m']
    a = [x for x in selected if x.startswith('11-')]
    if len(a) > 0:
        t = select_chaogao_11(data, aiclient, qwclient, sfclient)
        if 'A' in t:
            return ['11-F2!第十一册6-20m']
        elif 'B' in t:
            return ['11-F2!第十一册20-30m']
        elif 'C' in t:
            return ['11-F2!第十一册30-40m']
        elif 'D' in t:
            return ['11-F2!第十一册40-50m']
        elif 'E' in t:
            return ['11-F2!第十一册50-60m']
        elif 'F' in t:
            return ['11-F2!第十一册60-70m']
        elif 'G' in t:
            return ['11-F2!第十一册70-80m']
        elif 'H' in t:
            return ['11-F2!第十一册80m以上']
    return []

