# -*- coding: utf-8 -*-
"""PA4.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/11-S94GO-gTqtlSCTHYxVbaHmTGjqRzow
"""
def topsis(args):
  import pandas as pd
  import numpy as np
  import os
  import sys
  # args=sys.argv


  if(len(args)!=5):
    print('Incorrect number of arguments\n')
    exit(0)
  elif(not os.path.isfile('./'+args[1])):
    print('Input file not found\n')
    exit(0)
  elif(args[1][-4:]!='.csv'):
    print('Filetype mismatch\n')
    exit(0)


  df=pd.read_csv('./'+args[1])
  # print(df)
  if(len(df.columns) < 3):
    print('Column number not sufficient\n')
    exit(0)


  # df=pd.read_csv('101903036-data.csv')
  # df

  inputWeights=args[2]
  impacts=args[3]

  for c in inputWeights:
    if not(c.isnumeric() or c==','):
      print('Incorrect weights input\n')
      exit(0)

  for c in impacts:
    if not(c=='+' or c=='-' or c==','):
      print('Incorrect impacts input\n')
      exit(0)

  weights=inputWeights.split(',')
  weights=[int(i) for i in weights]
  # weights

  maxp=[]
  minp=[]
  pos=0
  for i in range(len(impacts)):
    if impacts[i]=='+':
      maxp+=[pos]
      pos+=1
    elif impacts[i]=='-':
      minp+=[pos]
      pos+=1

  reqLen=len(df.columns)-1
  if len(weights)!=reqLen:
    print(f'Incorrect weights: ')
    if reqLen>len(weights):
      print(f'add {reqLen-len(weights)}')
    else:
      print(f'remove {len(weights)-reqLen}')
    exit(0)

  if len(maxp)+len(minp)!=reqLen:
    print(f'Incorrect impacts: ')
    if reqLen>len(maxp)+len(minp):
      print(f'add {reqLen-len(impacts)}')
    else:
      print(f'remove {len(impacts)-reqLen}')
    exit(0)

  if(args[4][-4:]!='.csv'):
    print('Output filetype incorrect')
    exit(0)


  sum=0
  for i in df.iloc[:, 1]:
    sum+=(i**2)
  sum=sum**0.5
  # print(sum)
  newdf=pd.DataFrame()
  newdf['P1']=df['P1']/sum
  newdf['P1']=newdf['P1']*weights[0]
  # print(newdf)

  sum=0
  for i in df.iloc[:, 2]:
    sum+=(i**2)
  sum=sum**0.5
  # print(sum)
  newdf['P2']=df['P2']/sum
  newdf['P2']=newdf['P2']*weights[1]
  # print(newdf)

  sum=0
  for i in df.iloc[:, 3]:
    sum+=(i**2)
  sum=sum**0.5
  # print(sum)
  newdf['P3']=df['P3']/sum
  newdf['P3']=newdf['P3']*weights[2]
  # print(newdf)

  sum=0
  for i in df.iloc[:, 4]:
    sum+=(i**2)
  sum=sum**0.5
  # print(sum)
  newdf['P4']=df['P4']/sum
  newdf['P4']=newdf['P4']*weights[3]
  # print(newdf)

  sum=0
  for i in df.iloc[:, 5]:
    sum+=(i**2)
  sum=sum**0.5
  # print(sum)
  newdf['P5']=df['P5']/sum
  newdf['P5']=newdf['P5']*weights[4]
  # print(newdf)

  vp=[0, 0, 0, 0, 0]
  vn=[0, 0, 0, 0, 0]

  for i in maxp:
    vp[i]=newdf.iloc[:,i].max()
    vn[i]=newdf.iloc[:,i].min()

  for i in minp:
    vn[i]=newdf.iloc[:,i].max()
    vp[i]=newdf.iloc[:,i].min()

  vp=np.array(vp)
  vn=np.array(vn)

  # print(vp)
  # print(vn)

  sp=np.zeros(8)
  sn=np.zeros(8)
  for i in range(8):
    x=np.array(newdf.iloc[i, :5])
    sp[i]=(((x-vp)**2).sum())**0.5
    sn[i]=(((x-vn)**2).sum())**0.5

  newdf['Sp']=sp
  newdf['Sn']=sn
  newdf['Topsis Score']=newdf['Sn']/(newdf['Sp']+newdf['Sn'])

  # print(newdf)

  newdf['Ranks']=8-(newdf['Topsis Score'].argsort().argsort())
  # print(newdf)

  output=df
  output['Topsis Score']=newdf['Topsis Score']
  output['Ranks']=newdf['Ranks']
  print(output)

  output.to_csv(args[4], index=False)

  # tmp=pd.read_csv('output.csv')
  # tmp

# if __name__=='__main__':
#   topsis()