#-------------------------------------------------------------------------------
# Name:        convert schema of circuit to PyAMS netlist
# Author:      d.fathi
# Created:     17/09/2021
# Copyright:   (c) PyAMS 2021
# Licence:     unlicense
#-------------------------------------------------------------------------------

global usedtest;
usedtest=True;

#-------------------------------------------------------------------------------
# def pyamscircuit: convert schema of circuit to PyAMS netlist
#-------------------------------------------------------------------------------
def pyamscircuit(path,result):
    netList=result[1]

    data=[]
    data=['from sys import path;'];
    data+=['path+=["'+path+'/lib/analysis"];'];

    elems=[];

    for i in range(len(netList)):
        s='path+=["'+path+'/symbols/'+netList[i]['directory']+'"];'
        if not(s in data):
            data+=[s];

    libs=['from PyAMS import strToFloat;']
    libs+=['from simu import AppPyAMS;']
    cir=[];
    parms=[];

    for i in range(len(netList)):
        pins='","'.join(netList[i]['pins']);
        pins='("'+pins+'");';

        elems+=[netList[i]['ref']+'='+netList[i]['modelname']+pins]
        cir+=['"'+netList[i]['ref']+'":'+netList[i]['ref']]

        s='from '+netList[i]['modelname']+' import '+netList[i]['modelname'];
        if not(s in libs):
            libs+=[s]

        p=netList[i]['params'];
        for k in range(len(p)):
            x = p[k].split('=');
            parms+=[netList[i]['ref']+'.'+x[0]+'+='+'strToFloat("'+x[1]+'");'];

    temp=result[0];
    probe=','.join(temp);

    data+=[];
    data+=[];
    data+=["#------------------------------------------------------------------"];
    for i in range(len(libs)):
        data+=[libs[i]]

    data+=[];
    data+=[];
    data+=["#------------------------------------------------------------------"];
    for i in range(len(elems)):
        data+=[elems[i]]

    data+=["#------------------------------------------------------------------"];
    for i in range(len(parms)):
        data+=[parms[i]]

    data+=[];
    data+=[];
    data+=["#------------------------------------------------------------------"];
    data+=['AppPyAMS.setOut('+probe+')'];
    data+=['AppPyAMS.circuit({'+','.join(cir)+'});'];
    print(data)
    return data





#-------------------------------------------------------------------------------
# def opAnalysis: operating Point Analysis
#-------------------------------------------------------------------------------

def opAnalysis(self,result):
    data=pyamscircuit(self.pathapp,result);
    data+=['AppPyAMS.analysis(mode="op",start=0.0,stop=0.1,step=0.1);'];
    data+=['AppPyAMS.run(True);'];


    file = open("test.py", "w", encoding="utf-8")
    for element in data:
        file.write(element + "\n")
    file.close();
    from sys import path;
    import importlib
    path+=["lib\\"];

    L='global test;import test; ';
    no_error=True;
    global usedtest;
    try:
       if usedtest:
        exec(L);
       else:
        importlib.reload(test);
       usedtest=False;
    except Exception as e: # work on python 3.x
       str_error='Error: '+ str(e);
       no_error=False;


    if  no_error:
      a=str(test.AppPyAMS.getOut());
      self.ui.m_webview.page().runJavaScript("ioSetProbeValue("+a+");");
    else:
      self.message('Error',str_error);





#-------------------------------------------------------------------------------
# def getListSignalsNodeParams: get list of signals from symboles in circuit.
#-------------------------------------------------------------------------------

def getListSignalsNodeParams(self,result):
    data=pyamscircuit(self.pathapp,result);
    n=len(data)
    data[n-2]='';

    file = open("test.py", "w", encoding="utf-8")
    for element in data:
        file.write(element + "\n")
    file.close();
    from sys import path;
    import importlib
    path+=["lib\\"];

    L='global test;import test; ';
    global usedtest;
    if usedtest:
      exec(L);
    else:
      importlib.reload(test);
    usedtest=False;

    id=1;
    data=[{'unique_id': 1, 'parent_id': 0, 'short_name': '', 'type': ' ', 'description': ' '}];
    v=list(test.AppPyAMS.elements.values())
    k=list(test.AppPyAMS.elements.keys())
    for i in range(len(v)):
       signals=v[i].getSignals();
       params=v[i].getParamaters();
       id=id+1;
       r=id
       data += [{'unique_id': id, 'parent_id': 1, 'short_name': k[i], 'type': ' ', 'description': ' '}]
       for j in range(len(signals)):
          id=id+1;
          data += [{'unique_id': id, 'parent_id': r, 'short_name': signals[j].name, 'type': 'signal', 'description': signals[j].Stype, 'dir': signals[j].dir}]
       for j in range(len(params)):
          id=id+1;
          data += [{'unique_id': id, 'parent_id': r, 'short_name': params[j].name, 'type': 'paramater', 'description': ''}]
    return data;

