import csv
import sys
import matplotlib.pyplot as plt
import numpy as np
plt.rcdefaults()
name = []
sal=[]
try:
    with open('lab4-input-emp.txt') as csv_file:
        csv_reader = csv.reader(csv_file, delimiter=',')
        line_count = 0
        for row in csv_reader:
            try:
                if line_count == 0:
                    print('\t%s %s %s %s %s' % (row[0], row[1], row[2], row[3], row[4]))
                    line_count += 1
                else:
                    print('\t%s %s %s %s %s'%(row[0],row[1],row[2],row[3],row[4]))
                    line_count += 1
                    sal.append(row[4])
                    name.append(row[1])
                    try:
                        with open('lab4-output-emp.txt', mode='a') as output:
                            employee_writer = csv.writer(output, delimiter=',', quotechar='"',
                                                         quoting=csv.QUOTE_MINIMAL)
                            employee_writer.writerow(
                                [line_count - 1, row[1], float(row[4]) * 0.2, float(row[4]) * 0.1, float(row[4]) * 1.3])
                    except:
                        print("unable to append to output file")
            except:
                    print("unable to read row")
        print('Processed %s lines.'%(line_count-1))
        print(name)
        print(sal)
        salint = list(map(int, sal))
        #plotting method 1
        # objects = name
        # y_pos = np.arange(len(objects))
        # plt.bar(y_pos, salint, align='center', alpha=0.5)
        # plt.xticks(y_pos, objects)
        # plt.ylabel('salary')
        # plt.title('income')
        #
        # plt.legend(loc='upper left')
        # plt.show()

        #plotting multicolor
        n_groups = 3
        fig, ax = plt.subplots()
        index = np.arange(n_groups)
        bar_width = 0.35
        opacity = 0.8
        basic = [x * 1 for x in salint]
        hra = [x * 0.2 for x in salint]
        da = [x * 0.1 for x in salint]
        rects1 = plt.bar(index, basic, bar_width,
                         alpha=opacity,
                         color='r',
                         label='basic')
        rects2 = plt.bar(index + bar_width, hra, bar_width,
                         alpha=opacity,
                         color='g',
                         label='HRA')
        rects3 = plt.bar(index + bar_width+ bar_width, da, bar_width,
                         alpha=opacity,
                         color='b',
                         label='DA')
        plt.xlabel('name')
        plt.ylabel('salary')
        plt.title('income')
        plt.xticks(index + bar_width, ('A', 'B', 'C'))
        plt.legend()

        plt.tight_layout()
        plt.show()
except:
    print("Unable to open input file", sys.exc_info()[0], "occured.")