import itertools


'''
options list is ordered by channel (0-9).
v2 used John G's chart as a reference.
v3 incorporates feedback from Ken J to add switch status and limit counter func.
v4 incorporates mV input channels from draft of RIO data sheet, and combines the
Form C options into one since the behavior is based on wiring, not
configuration.
v5 adds the form relay options back in based on input from Ken: We are
calculating how many I/O configurations we can support in the field, not just
how many ways you can configure RIO.
v6 Removes counter which is a feature, not a signal type, adds other TC types
so all signals are represented, and updates labels based on latest groov Manage
build
v7 generate output for inspection; readability improvements
v8 combined all TC types back into one option

A = AI 0-10 V
B = AI 0-20 mA
C = AI ICTD
D = AI TC 
E = DI (No feature)
F = DI (Counter) [deprecated]
G = AO 0-20 mA
H = AO 0-10 V
I = DO
J = Form C relay NO
K = Form C relay NC
L = DI Switch Status (No feature)
M = AI +/- 300 mV
N = AI TC E [deprecated]
O = AI TC J [deprecated]
P = AI TC K [deprecated]
Q = AI TC N [deprecated]
R = AI TC R [deprecated]
S = AI TC S [deprecated]
T = AI TC T [deprecated]
'''


def all_opts():
    '''calculate all possible I/O configurations,
    returning an iterator over the cross-product'''
    options = ['ABCDEILM',
               'ABCDEILM',
               'ABCDEILM',
               'ABCDEILM',
               'AEGHIL',
               'AEGHIL',
               'AEGHIL',
               'AEGHIL',
               'JK',
               'JK']
    return itertools.product(*options)

def unique_opts():
    '''calculate only unique I/O configurations by creating a
    sorted, hashable representation of each, then filtering
    duplicates using set exclusivity. returns an unsorted set.'''
    unique = set()
    for each_opt in all_opts():
        each_opt = sorted(each_opt)
        each_opt = ''.join(each_opt)
        unique.add(each_opt)
    return unique

def count_all():
    '''count all possible I/O configurations'''
    count = 0
    for each_opt in all_opts():
        count = count + 1
    return count


print("Calculating all I/O configurations...")
print(str(count_all()))

print("Calculating unique I/O configurations...")
unique = unique_opts()
print(str(len(unique)))

print("Writing configurations to file...")
with open('options.csv', 'w') as f:
    for each_opt in sorted(unique):
        f.write(each_opt + ',\n')

print("\nDone! Configurations stored at options.csv")
input()
