import importlib.util, json, time
spec=importlib.util.spec_from_file_location('mu','C:/MCSJAgent/mcsj-user.py')
m=importlib.util.module_from_spec(spec); spec.loader.exec_module(m)
p='payment.client.fx.EFXPwpaycodePanel'; e=m._proto
seen=set(); misc=[]
for n in range(120):
    st=m.agent18('FXSTATE')
    panel=next((x for x in st.get('panels',[]) if x.get('panelClass')==p),None)
    if not panel: break
    c=panel.get('controls',[])
    upp=[x for x in c if x.get('class')=='misc.components.fx.EFXUpperMaskField']
    txt=[x for x in c if x.get('class')=='misc.components.fx.EFXTextField']
    combos=[x for x in c if x.get('class')=='misc.components.fx.EFXComboBox']
    code=(upp[0].get('text') if upp else '').strip()
    desc=(txt[0].get('text') if txt else '').strip()
    ptype=next((x.get('text','').strip() for x in combos if x.get('text','').strip() in ('Miscellaneous','Invoice','Water','Sewer','Property Tax','Animal Licensing')), '')
    if code in seen: break
    seen.add(code)
    if ptype=='Miscellaneous':
        misc.append({'code':code,'description':desc,'upperMaskValues':[x.get('text','').strip() for x in upp if x.get('text','').strip()],'textValues':[x.get('text','').strip() for x in txt if x.get('text','').strip()],'combos':[{'class':x.get('class'),'text':x.get('text')} for x in c if 'ComboBox' in x.get('class','') and 'FakeFocus' not in x.get('class','')]})
        if len(misc)>=8: break
    r=m.agent18('FXFIREBUTTON '+e(p)+' '+e('Next')+' false')
    if r.get('status')!='OK': break
    time.sleep(.08)
print(json.dumps({'recordsScanned':len(seen),'miscellaneous':misc},indent=2))
