### Simulation Code Writing ###
from tkinter import *
import random
import math

def makeModel(data): 
    data["cx"] = 200 
    data["cy"] = 200 
    data["size"] = 50 
    data["color"] = "red"

def makeView(data, canvas): 
    color = data["color"]
    canvas.create_oval(data["cx"] - data["size"], data["cy"] - data["size"], data["cx"] + data["size"], data["cy"] + data["size"], fill=color)

#FILL THIS OUT
def runRules(data,call):
    pass


### Debugging ###
"""
obtainOdds takes in a matrix as a 2d list and returns a list of every element in the matrix that is odd
"""
def obtainOdds(L):
    odds = []
    for row in range(len(L)):
        for col in range(len(L[0])):
            if L[row][col] % 2 == 1:
                odds.append(L[col][row])
    return odds

"""
createDiagMatrix takes in a matrix M as a 2d list and returns a square matrix that has every odd element of matrix M on the diagonal. All non diagonal elements are equal to the sum of the odds. For example:

createDiagMatrix([[1,3]]) => [[1, 4], [4, 3]]
Where 1 and 3 are on the diagonals and 1+3=4 on the off diagonals

"""

def createDiagMatrix(M):
    mat = []
    odds = obtainOdds(M)
    oddSum = sum(odds)
    for i in range(len(odds)):
        tempLst = [oddSum] * len(odds)
        tempLst[0] = odds[i] 
        mat.append(tempLst)
    return mat

# Test Cases #
tst =   [
            [1,2,3],
            [4,5,6],
            [7,8,9]
        ]

assert(createDiagMatrix(tst) == 
    [
        [1,25,25,25,25], 
        [25,3,25,25,25],
        [25,25,5,25,25],
        [25,25,25,7,25],
        [25,25,25,25,9]
    ])
    
assert(createDiagMatrix([]) == [])
assert(createDiagMatrix([[1,2,3]]) == [[1,4],[4,3]])
print("Tests passed!")


### Working with Data ###

import csv 

def readCSVFile(path):
    pass

"""
Write a function to create a dictionary from the input 2D list, 
where the department_name for each inner list is the key and the value 
is a 2 element list of the corresponding location_id and department_expenses
for that inner list.
Note that department_name is at index 1 of each row of data, 
location_id is at index 2, and department expenses is at index 3
"""

def departmentNameDict(data):
    pass

"""
Write a function that given a dictionary like the one created above and a list
of departments, finds the mean expenses across the given departments and the 
most common location among the departments (hint: you can use mode on 
non-numerical data) and prints them to the user.
You may want to import a package to help you with this function!
"""

def departmentInfo(d, departments):
    pass


# Write a line of code below to call readCSVFile and store the result in 
# a variable called data! Then uncomment the following lines of code to run the test cases!


'''
departmentDictionary = departmentNameDict(data[1:])
departments1 = ["Purchasing", "Recruiting", "Payroll", "Marketing", "IT", "Construction"]
departments2 = ["Control And Credit", "Government Sales", "Shareholder Services", "Executive", "NOC"]
departmentInfo(departmentDictionary, departments1)
departmentInfo(departmentDictionary, departments2)
'''

########################### DO NOT ALTER: SIMULATION FRAMEWORK CODE ##########################

def timeLoop(data, canvas, call):
    runRules(data, call)
    canvas.delete(ALL)
    makeView(data, canvas)
    canvas.update()
    canvas.after(data["timeRate"], timeLoop, data, canvas, call + 1)

    
def runSimulation(w, h, timeRate):
    data = { }
    data["timeRate"] = int(timeRate * 1000) # call will be in ms
    makeModel(data)
    
    root = Tk()
    canvas = Canvas(root, width=w, height=h)
    canvas.configure(bd=0, highlightthickness=0)
    canvas.pack()
    makeView(data, canvas)
    
    canvas.after(data["timeRate"], timeLoop, data, canvas, 1)
    
    root.mainloop()


runSimulation(500,500, 0.3)
