import time




from pywinauto.application import Application
# app = Application(backend="uia").start("notepad.exe")
# app['未命名 - 記事本'].type_keys("%FX")


#from pywinauto.timings import Timings
#Timings.window_find_timeout = 50.0

def legacy_properties(ctrl):
    d = {}
    for c in ctrl.children():
        p = c.legacy_properties()
        d[p['Name']] = p['Value']
        
    return d



# app = Application().start(r"C:\Program Files (x86)\AGFA\IMPAX Client\AppStart.exe")

try:
    app = Application().connect(path=r"C:\Program Files (x86)\AGFA\IMPAX Client\6.5.2.114\impax-client-main.exe")
except:
    app = Application().start(r"C:\Program Files (x86)\AGFA\IMPAX Client\AppStart.exe")
    app = Application().connect(path=r"C:\Program Files (x86)\AGFA\IMPAX Client\6.5.2.114\impax-client-main.exe")

app2 = Application('uia', allow_magic_lookup=False).connect(path=r"C:\Program Files (x86)\AGFA\IMPAX Client\6.5.2.114\impax-client-main.exe")


# print( app.exists(['IMPAX 6.5.2.114 Enterprise Unlimited']))
# exit()


# if not app['IMPAX 6.5.2.114 Enterprise Unlimited'].exists('重新設定'):

def login():
    time.sleep(1)
    # app['IMPAX 6.5.2.114'].print_ctrl_ids(filename='ids_login.txt')
    app['IMPAX 6.5.2.114']['使用者識別碼:Edit'].type_keys('004552')
    time.sleep(1)
    app['IMPAX 6.5.2.114']['密碼:Edit'].type_keys('n122119493')
    app['IMPAX 6.5.2.114']['登入'].click()
    time.sleep(20)

def fetch(patient_id, modality='MR'):


    # print(dir(app['IMPAX 6.5.2.114 Enterprise Unlimited']))
    
    try:
        if app['IMPAX 6.5.2.114 Enterprise Unlimited'].exists('登出'):
            pass
    except:
        login()

    #print(dir(app['IMPAX 6.5.2.114 Enterprise Unlimited']['登出'].parent()))
    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['登出'].parent().click_input()

    #try:
    #    app['IMPAX 6.5.2.114 Enterprise Unlimited']['登出'].parent().click_input()

    #except:
    #    login()

    #app['IMPAX 6.5.2.114 Enterprise Unlimited'].wait('visible')
   
    if not app['IMPAX 6.5.2.114 Enterprise Unlimited'].exists('重新設定'):
        app['IMPAX 6.5.2.114 Enterprise Unlimited']['登出'].parent().click_input()


    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['重新設定'].wait('enabled')
    
    #app['IMPAX 6.5.2.114 Enterprise Unlimited'].window(auto_id="resetButton").click()

    #app['IMPAX 6.5.2.114 Enterprise Unlimited'].print_ctrl_ids(depth=3, filename='ids_uia.txt')
    app['IMPAX 6.5.2.114 Enterprise Unlimited'].print_ctrl_ids(filename='ids_uia.txt')

    app['IMPAX 6.5.2.114 Enterprise Unlimited']['重新設定'].click()
    app['IMPAX 6.5.2.114 Enterprise Unlimited']['病歷號碼Edit'].type_keys(patient_id)
    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['影像檢查設備ComboBox'].click_input()
    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['影像檢查設備ComboBox'].select(modality)
    app['IMPAX 6.5.2.114 Enterprise Unlimited']['搜尋Button'].click()

    #print(dir(app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_images']))
    #print(app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_images'].children())

    #print(app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].get_properties())
    #print(app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].legacy_properties())
    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].print_control_identifiers()
    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].capture_as_image().save('1.png')
    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].click_input()
    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].capture_as_image().save('2.png')
    #app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].print_control_identifiers()
    #print(app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'])
    #print(app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].get_properties())
    #print(app['IMPAX 6.5.2.114 Enterprise Unlimited']['study.num_imagesHeader'].legacy_properties())

    study_list = []
    #app2 = Application('uia').connect(path=r"C:\Program Files (x86)\AGFA\IMPAX Client\6.5.2.114\impax-client-main.exe")

    app2['IMPAX 6.5.2.114 Enterprise Unlimited'].print_ctrl_ids(filename='ids_uia.txt')

    #print(dir(app2['IMPAX 6.5.2.114 Enterprise Unlimited']))
    #for c in app2['IMPAX 6.5.2.114 Enterprise Unlimited']['描述內容Pane'].children():
    #for c in app2['IMPAX 6.5.2.114 Enterprise Unlimited'].child_window(auto_id="grid", control_type="Pane").children():
    
    for c in app2['IMPAX 6.5.2.114 Enterprise Unlimited'].child_window(title="描述內容", control_type="Pane").children():
    
        # print(c)
        p = legacy_properties(c)
        if 'patient.patient_id' in p and p['patient.patient_id'] == patient_id:
            # print(dir(c))
            # print(c.legacy_properties())
            print(c.texts()[0], p['View'], p['study.accession_number'], p['study.modality'], p['study.study_date'], p['study.num_images'], p['study.study_description'])
            # exit()

            if int(p['study.num_images'])>10:
                study_list.append((c.texts()[0], p))

    exit()

# Available
# VIEWABLE
# VIEWING
# nearline

    for c, p in study_list:
        print(c)

        ctrl = app['IMPAX 6.5.2.114 Enterprise Unlimited'][c].children()[0]

        # print(ctrl.texts())
        # print(dir(ctrl))
        app['IMPAX 6.5.2.114 Enterprise Unlimited'][c].children()[0].double_click_input()
        # time.sleep(10)
        # app['IMPAX 6.5.2.114 Enterprise Unlimited'].print_ctrl_ids(filename='ids_view.txt')
        exit()


fetch('3009684')
#fetch('2228813')
#fetch('2541371', 'CT')
