# -*- coding: utf-8 -*-
"""TOPSIS (Initial Draft).ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1q1ymedXxqKI7WQWvwpUAeIY6JCVl6hAP
"""
try:
    import pandas as pd
    import numpy as np  
    import sys


    arg_len = len(sys.argv)
    if(arg_len != 5):
      sys.exit("Incorrect number of parameters")
    path = sys.argv[1]
    weight_values = sys.argv[2]
    impact_values = sys.argv[3]
    # read_file = pd.read_excel ("data.xlsx")
    # print(read_file)
    # x = read_file.to_csv("101903693-data.csv")#, index = None, header=True)

    #df = pd.DataFrame(pd.read_csv("101903693-data.csv"))
    df = pd.DataFrame(pd.read_csv(path))
    #df.head(8)

    if(df.shape[1] < 3):
      sys.exit("Input file must contain at least 3 columns")


    df1 = df.iloc[:, 1:8]
    #df1.head(8)

    arr = df1.to_numpy(dtype = 'float64')
    #arr = np.delete(arr, (5,6,7), axis = 0)
    #print(arr)

    ssq = np.sum(arr**2, axis = 0)
    #print(ssq)

    norm_arr = arr/ssq
    #print(norm_arr)

    #weights = np.array([1,1,1,1,1])
    weights = np.array([])
    num1 = 0
    for i in weight_values:
      if(i == ","):
        num1 = num1 + 1
    if(num1 != 4):
      sys.exit("Weights not seperated by commas")
      
    weight_values = weight_values.replace(',', '')
    for i in weight_values:
      #print("type: ", type(i))
      #print("value: ", i)
      weights = np.append(weights, float(i))
    
    #weights = np.array([weight_values])
    #weights_float = weights.astype(float)

    arr2 = norm_arr * weights
    #print(arr2)

    
    impact = np.array([])
    num2 = 0
    for i in impact_values:
      if(i == ","):
        num2 = num2 + 1
    if(num2 != 4):
      sys.exit("Impacts not seperated by commas")

    impact_values = impact_values.replace(',', '')
    for i in impact_values:
      if(i == '+' or i == '-'):
        print()
      else:  
        sys.exit('Values of impact are not correct. They should either be "+" or "-" ')

      impact = np.append(impact, i)
    
    if(df.shape[1] -1 != len(impact) and df.shape[1] -1 != len(weights) and len(weights) != len(impact)):
      sys.exit("Number of weights, number of impacts and number of columns in file are not equal")

    data_maxes = np.array([])
    data_mins = np.array([])
    n = 0

    for i in impact:
      
      if(i == '+'):
        
        data_maxes = np.append(data_maxes, arr2.max(axis = 0)[n])
        data_mins = np.append(data_mins, arr2.min(axis = 0)[n])
        
      else:
        data_maxes = np.append(data_maxes, arr2.min(axis = 0)[n])
        data_mins = np.append(data_mins, arr2.max(axis = 0)[n])

      n = n+1

    #print(data_maxes)
    #print(data_mins)

    smax = np.array([])
    smin = np.array([])

    smax = (arr2 - data_maxes)**2
    smin = (arr2 - data_mins)**2
    #print(smax)

    #print(smin)

    pos_scores = np.array([])
    neg_scores = np.array([])

    pos_scores = np.sum(smax, axis = 1)
    #print(pos_scores)

    neg_scores = np.sum(smin, axis = 1)
    #print(neg_scores)

    total_sum = pos_scores + neg_scores
    #print(total_sum)

    topsis_score = neg_scores/total_sum
    print("TOPSIS Score: ", topsis_score)

    order = topsis_score.argsort()
    ranks = 8 - order.argsort()
    print("Ranks: ", ranks)

    df['Topsis Score'] = topsis_score
    df['Rank'] = ranks
    df.head(8)

    df.to_csv("101903693-result.csv")

    #101903693.py 101903693-data.csv "1,1,1,2,1" "+,-,-,-,+" 101903693-result.csv
except FileNotFoundError:
  print("File not found, please enter correct file name")
