import pandas as pd
import sys

def main():
            
    infile = sys.argv[1]
    weights = sys.argv[2]
    w = weights.split(",")
    impact = sys.argv[3]
    im = impact.split(",")
    outfile = sys.argv[4]

    topsis = pd.read_csv(infile)
            
    s = topsis.copy(deep = True)
    n = topsis.shape[0]
    m = topsis.shape[1]

    d = ((topsis.iloc[:,1:] ** 2).sum()) ** 0.5
    for i in range(n):
        s.iloc[i,1:] = s.iloc[i,1:] / d
            
    for i in range(m-1):
        s.iloc[:,i+1] = s.iloc[:,i+1] * float(w[i])
            

    vp = []
    vn = []
    for i in range(m-1):
        if im[i] == "+":
            vp.append(s.iloc[:,i+1].max())
            vn.append(s.iloc[:,i+1].min())
        else:
            vp.append(s.iloc[:,i+1].min())
            vn.append(s.iloc[:,i+1].max())

    sp = []
    for i in range(n):
        sp.append((((s.iloc[i,1:] - vp) ** 2).sum()) ** 0.5 )

    sn = []
    for i in range(n):
        sn.append((((s.iloc[i,1:] - vn) ** 2).sum()) ** 0.5 )

    p = []
    for i in range(n):
        p.append(sn[i] / (sn[i] + sp[i]))

    topsis['Topsis Score'] = p

    topsis['Rank'] = topsis['Topsis Score'].rank(ascending = 0)
    print(topsis)
    topsis.to_csv(outfile)

if __name__ == '__main__':
    main()